This decades old Linux function is now 4 times faster
Key Takeaways
The Linux function memchr() has been improved by four times with the latest patch, and this improvement can teach us about the importance of understanding hardware and software architecture in coding.
Full Transcript
an old function in linux called memchar has been improved recently with the latest patch of linux by four times but this is not really the most instructive thing because this function was introduced back in what 1991 but the most instructive thing here is how it was discovered and the way uh the the code is written back in the days versus taking advantage of modern systems and cpus and this is what what is fascinating to me this comes from phoronx i'm going to read it to you and then a little bit i'm going to discuss a little bit so optimized memchar implementations for linux kernel up to four times faster a set of proposed patches promises to make the linux kernel's memchar which is a function basically you guys that you give it a block of memory the start of the block of the memory you give it what character you want to find and it will give you where exactly in that memory that character exists so it's almost like a search you know contains if you will in tests carried out by the developer the new implementation can be nearly four times faster in large searches only in large searches so it's not really a big deal if you're searching in small binaries small strengths but in a large when you're searching large things yeah it can be beneficial but this is not really what i'm interested in i'm interested in the explanation here and how we can learn something from this from experts so this comes from one of the developers of linux kernel eugene chang who sent other patches let's read this and discuss a little bit the original version of mem char is implemented with a buy twice comparing technique which does not fully use the 64 or 32-bit registers in the cpu we that means them we use word wide comparing so that eight characters so eight bytes assuming uh one byte is one character that might not always be true for utf-8 but you get the point can be compared at the same time on the cpu this code is based on david lates implementation so this is and they they go on and explain the actual performance because you see if you compare a single byte that means you have to take it from memory copy to the cpu register and then compare it with the actual value that you will look for so if you're comparing byte by byte you're going to go back and forth from memory to the cpu what they those guys did it's like okay since we have plenty of room in the cpu register which is the closest cache to the cpu let's go p8 let's copy the whole 64-bit let's understand how much the register is using and then maybe change that they don't mention if if the cpus register is 32 they shift to a four board or four bytes or four characters i don't know but it's still comparing eight together you know it's much faster close to the cpu compared to one byte by one and uh this is the basically the the actual function this is the code right and as you can see it was written back in 1992 by linus trevor and the the the code is really simple you can read through it through it and really understand what's going on you know it it's this is the trick here the p here is the basically the storage where you're looping through each bite right so it's an inside character and you're looping through all the n size that's the memory size right and this is what you're looking for if you found it then exit right and this return the result otherwise return now that means you didn't find it didn't find it so we're searching bite by bite what those guys did is they changed this to award which is i believe it's a it's an eight eight byte so this is what fascinates me when i read this article i was like is it worth it to make a video on it for the benefits we might not get anything out of it as a as you as a user we might not feel it at all maybe if you use this function if it's exposed to you in the string.c which is the the c library that this is exposed to yeah you might use it if you're using really large string then you're going to start fitting it because you're going to start reading more and more you're going to utilize the cache of the cpu registers right but it doesn't really matter what this i think teaches us is how to look beyond the abstractions you know because if i'm look if i'm writing a code and it's looping through a character we don't really think in terms of software architecture and hardware architecture we really at least i don't personally you know but this kind of changes the game i believe those c the kernel guys think of this in this time all the time right but i personally don't you know because i i mostly write in high-level languages so i rarely think like this but this kind of this kind of news changes completely how i think you know and i think we can learn something of them there look at the code you're writing today and see can it really improve i believe every code can be improved but the goal here is we don't want to also go and improve everything right because it's like okay how much the gains are really worth it or not you have to think about it this way but just the fact that understanding what's going on gives you internal i don't know comfort of some sort at least to me that okay this is exactly how that code works instead of just i don't know it's just looping but what is it actually doing this trip between the memory and the cpu and it's less significant this trip between the cpu and memory when you're in a noma architecture where the cpu or or the the the new architecture with m1 or m2 the apple one where the cpu literally lives within the memory so there is no trap to go to a bus of some sort to read the memory there is no trap so this i believe will be even less significant right in in this kind of architecture where the things are so close to each other but i i find it just fascinating to as as you go through the code and understand what is exactly doing and by all means i i'm not really an expert it just makes me really think about this uh in details i don't know what do you guys think do do you feel the same or do you say yeah this is really just really nothing burger let me know in the comment section below gonna see in the next one quick news today
Original Description
memchr() is a function that searches in a memory block for a character, this has been recently improved in the latest linux patch and we can learn something from this improvement I think, lets discuss.
https://www.phoronix.com/scan.php?page=news_item&px=Linux-Kernel-Faster-memchr
https://github.com/torvalds/linux/blob/master/lib/string.c
https://man7.org/linux/man-pages/man3/memchr.3.html
Fundamentals of Networking for Effective Backends udemy course (link redirects to udemy with coupon)
https://network.husseinnasser.com
Fundamentals of Database Engineering udemy course (link redirects to udemy with coupon)
https://database.husseinnasser.com
Introduction to NGINX (link redirects to udemy with coupon)
https://nginx.husseinnasser.com
Python on the Backend (link redirects to udemy with coupon)
https://python.husseinnasser.com
Become a Member on YouTube
https://www.youtube.com/channel/UC_ML5xP23TOWKUcc-oAE_Eg/join
Arabic Software Engineering Channel
https://www.youtube.com/channel/UChWZsjdoRvZ0T9QWZOD6UpA
🔥 Members Only Content
https://www.youtube.com/playlist?list=UUMO_ML5xP23TOWKUcc-oAE_Eg
🏭 Backend Engineering Videos in Order
https://backend.husseinnasser.com
💾 Database Engineering Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQXjD0HOzN7P2tgzu7scWpl2
🎙️Listen to the Backend Engineering Podcast
https://husseinnasser.com/podcast
Gears and tools used on the Channel (affiliates)
🖼️ Slides and Thumbnail Design
Canva
https://partner.canva.com/c/2766475/647168/10068
Stay Awesome,
Hussein
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Hussein Nasser · Hussein Nasser · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
Channel Update - New Book, New Job, New Videos
Hussein Nasser
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
Javascript by Example - The Vook
Hussein Nasser
Vlog - Keep your servers close and your database closer
Hussein Nasser
Vlog - Client/Server Programming Languages
Hussein Nasser
Javascript By Example L1E01 - Getting Started
Hussein Nasser
Persistent Connections (Pros and Cons)
Hussein Nasser
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
Happy new Year from IGeometry!
Hussein Nasser
Synchronous v. Asynchronous
Hussein Nasser
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
Relational Database Atomicity Explained By Example
Hussein Nasser
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
What Comes First, User Experience or Software Architecture?
Hussein Nasser
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
Advice for New Software Engineers and Developers
Hussein Nasser
Why JSON is so Popular?
Hussein Nasser
Building Scalable Software - SLA, HS, VS
Hussein Nasser
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
Learn By Doing.
Hussein Nasser
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
My Story
Hussein Nasser
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
Can User Experience Help Build Better Rest API?
Hussein Nasser
Reverse engineering Instagram in flight mode
Hussein Nasser
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
The evolution from virtual machines to containers
Hussein Nasser
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
Canary Deployment (Explained by Example)
Hussein Nasser
No Excuses
Hussein Nasser
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
What is an Asynchronous service?
Hussein Nasser
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
Nodejs Express "Hello, World"
Hussein Nasser
Reverse Engineering Instagram feed
Hussein Nasser
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser
More on: Staying Current in AI
View skill →Related Reads
📰
📰
📰
📰
Additive column can 500 every login (Drizzle + Better Auth + preview deploys)
Dev.to · Omar Bni
Why Auth0 E2E Tests Fail After Login: The localStorage Race Condition
Dev.to · Anand Rathnas
Basic Git Command and What is Git
Medium · Programming
When to Use an ORM vs SQL and Query Builders
Dev.to · Doogal Simpson
🎓
Tutor Explanation
DeepCamp AI