This decades old Linux function is now 4 times faster

Hussein Nasser · Beginner ·🔧 Backend Engineering ·4y ago

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 Extending ArcObjects (IGeometry) - 01 - Getting Started
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
2 Extending ArcObjects  (IGeometry) - 02 - The Document, The Map and The Layers
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
3 Channel Update - New Book, New Job, New Videos
Channel Update - New Book, New Job, New Videos
Hussein Nasser
4 Learn Programming with VB.NET - 01 - Getting Started
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
5 Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
6 Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
7 Learn Programming with VB.NET - 04 - User Interface
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
8 Learn Programming with VB.NET - 05 - By Value v. By Reference
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
9 Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
10 Learn Programming with VB.NET - 07 - Conditional Statements
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
11 Learn Programming with VB.NET - 08 - Inheritance
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
12 Learn Programming with VB.NET - 09 - Strategy Design Pattern
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
13 Learn Programming with VB.NET - 10 -  How did I learn programming
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
14 IGeometry 2016 Retrospective - Channel Update
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
15 Javascript by Example - The Vook
Javascript by Example - The Vook
Hussein Nasser
16 Vlog - Keep your servers close and your database closer
Vlog - Keep your servers close and your database closer
Hussein Nasser
17 Vlog - Client/Server Programming Languages
Vlog - Client/Server Programming Languages
Hussein Nasser
18 Javascript By Example L1E01 - Getting Started
Javascript By Example L1E01 - Getting Started
Hussein Nasser
19 Persistent Connections (Pros and Cons)
Persistent Connections (Pros and Cons)
Hussein Nasser
20 Javascript By Example L1E02 - Building the Calculator Interface
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
21 Happy new Year from IGeometry!
Happy new Year from IGeometry!
Hussein Nasser
22 Synchronous v. Asynchronous
Synchronous v. Asynchronous
Hussein Nasser
23 Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
24 Show Your Work. Blog, Vlog, Write, Create and Develop!
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
25 Relational Database Atomicity Explained By Example
Relational Database Atomicity Explained By Example
Hussein Nasser
26 Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
27 What Comes First, User Experience or Software Architecture?
What Comes First, User Experience or Software Architecture?
Hussein Nasser
28 Javascript By Example L1E05 -  Evaluate the Calculator Expressions with eval
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
29 Fastest Way to Learn Programming Language or Technology
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
30 Javascript By Example L1E06 -  Fix Leading Zero Bug with Conditions
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
31 Stateful vs Stateless Applications (Explained by Example)
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
32 Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
33 Advice for New Software Engineers and Developers
Advice for New Software Engineers and Developers
Hussein Nasser
34 Why JSON is so Popular?
Why JSON is so Popular?
Hussein Nasser
35 Building Scalable Software - SLA, HS, VS
Building Scalable Software - SLA, HS, VS
Hussein Nasser
36 Vlog (Istanbul) - Datacenter Proximity
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
37 Should Software Engineers Learn Bleeding-Edge Technologies?
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
38 Do Developers Build Bad User Interfaces/Experience?
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
39 Learn By Doing.
Learn By Doing.
Hussein Nasser
40 I Wrote Bad Front-End Code That Broke Chrome
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
41 My Story
My Story
Hussein Nasser
42 Vlog - Horizontal vs Vertical Scaling
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
43 Can User Experience Help Build Better Rest API?
Can User Experience Help Build Better Rest API?
Hussein Nasser
44 Reverse engineering Instagram in flight mode
Reverse engineering Instagram in flight mode
Hussein Nasser
45 The Benefits of the 3-Tier Architecture (e.g. REST API)
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
46 Stateless v. Stateful Architecture (Podcast)
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
47 The evolution from virtual machines to containers
The evolution from virtual machines to containers
Hussein Nasser
48 Proxy vs. Reverse Proxy (Explained by Example)
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
49 Canary Deployment (Explained by Example)
Canary Deployment (Explained by Example)
Hussein Nasser
50 No Excuses
No Excuses
Hussein Nasser
51 Synchronous vs Asynchronous Applications (Explained by Example)
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
52 What is an Asynchronous service?
What is an Asynchronous service?
Hussein Nasser
53 Difference between Client Polling vs Server Push in Notifications
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
54 Software vs. Hardware AdBlockers (Explained by Example)
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
55 HTTP Caching with E-Tags -  (Explained by Example)
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
56 Simple Object Access Protocol Pros and Cons (Explained by Example)
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
57 Nodejs Express "Hello, World"
Nodejs Express "Hello, World"
Hussein Nasser
58 Reverse Engineering Instagram feed
Reverse Engineering Instagram feed
Hussein Nasser
59 Popup Modal Dialog with Javascript and HTML
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
60 MIME and Media Type sniffing explained and the type of attacks it leads to
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser

The improvement of the Linux function memchr() can teach us about the importance of understanding hardware and software architecture in coding, and how to optimize code for better performance.

Key Takeaways
  1. Read the article on Phoronix about the improvement of memchr()
  2. Understand the code of memchr() and how it was optimized
  3. Learn about computer architecture and its impact on coding
  4. Apply the principles of software optimization to your own code
💡 Understanding hardware and software architecture is crucial for optimizing code and improving performance.

Related Reads

Up next
/dev/push: An Open Vercel Alternative to Ship Your Apps Quickly
Ian Wootten
Watch →