r/unix • • Jun 13 '17

How is GNU `yes` so fast?

1.5k Upvotes

How is GNU's yes so fast?

$ yes | pv > /dev/null
... [10.2GiB/s] ...

Compared to other Unices, GNU is outrageously fast. NetBSD's is 139MiB/s, FreeBSD, OpenBSD, DragonFlyBSD have very similar code as NetBSD and are probably identical, illumos's is 141MiB/s without an argument, 100MiB/s with. OS X just uses an old NetBSD version similar to OpenBSD's, MINIX uses NetBSD's, BusyBox's is 107MiB/s, Ultrix's (3.1) is 139 MiB/s, COHERENT's is 141MiB/s.

Let's try to recreate its speed (I won't be including headers here):

/* yes.c - iteration 1 */
void main() {
    while(puts("y"));
}

$ gcc yes.c -o yes
$ ./yes | pv > /dev/null
... [141 MiB/s] ...

That's nowhere near 10.2 GiB/s, so let's just call write without the puts overhead.

/* yes.c - iteration 2 */
void main() {
    while(write(1, "y\n", 2)); // 1 is stdout
}

$ gcc yes.c -o yes
$ ./yes | pv > /dev/null
... [6.21 MiB/s] ...

Wait a second, that's slower than puts, how can that be? Clearly, there's some buffering going on before writing. We could dig through the source code of glibc, and figure it out, but let's see how yes does it first. Line 80 gives a hint:

/* Buffer data locally once, rather than having the
large overhead of stdio buffering each item.  */

The code below that simply copies argv[1:] or "y\n" to a buffer, and assuming that two or more copies could fit, copies it several times to a buffer of BUFSIZ. So, let's use a buffer:

/* yes.c - iteration 3 */
#define LEN 2
#define TOTAL LEN * 1000
int main() {
    char yes[LEN] = {'y', '\n'};
    char *buf = malloc(TOTAL);
    int used = 0;
    while (used < TOTAL) {
        memcpy(buf+used, yes, LEN);
        used += LEN;
    }
while(write(1, buf, TOTAL));
return 1;
}

$ gcc yes.c -o yes
$ ./yes | pv > /dev/null
... [4.81GiB/s] ...

That's a ton better, but why aren't we reaching the same speed as GNU's yes? We're doing the exact same thing, maybe it's something to do with this full_write function. Digging leads to this being a wrapper for a wrapper for a wrapper (approximately) just to write().

This is the only part of the while loop, so maybe there's something special about their BUFSIZ?

I dug around in yes.c's headers forever, thinking that maybe it's part of config.h which autotools generates. It turns out, BUFSIZ is a macro defined in stdio.h:

#define BUFSIZ _IO_BUFSIZ

What's _IO_BUFSIZ? libio.h:

#define _IO_BUFSIZ _G_BUFSIZ

At least the comment gives a hint: _G_config.h:

#define _G_BUFSIZ 8192

Now it all makes sense, BUFSIZ is page-aligned (memory pages are 4096 bytes, usually), so let's change the buffer to match:

/* yes.c - iteration 4 */
#define LEN 2
#define TOTAL 8192
int main() {
    char yes[LEN] = {'y', '\n'};
    char *buf = malloc(TOTAL);
    int bufused = 0;
    while (bufused < TOTAL) {
        memcpy(buf+bufused, yes, LEN);
        bufused += LEN;
    }
    while(write(1, buf, TOTAL));
    return 1;
}

And, since without using the same flags as the yes on my system does make it run slower (yes on my system was built with CFLAGS="-O2 -pipe -march=native -mtune=native"), let's build it differently, and refresh our benchmark:

$ gcc -O2 -pipe -march=native -mtune=native yes.c -o yes
$ ./yes | pv > /dev/null
... [10.2GiB/s] ... 
$ yes | pv > /dev/null
... [10.2GiB/s] ...

We didn't beat GNU's yes, and there probably is no way. Even with the function overheads and additional bounds checks of GNU's yes, the limit isn't the processor, it's how fast memory is. With DDR3-1600, it should be 11.97 GiB/s (12.8 GB/s), where is the missing 1.5? Can we get it back with assembly?

; yes.s - iteration 5, hacked together for demo
BITS 64
CPU X64
global _start
section .text
_start:
    inc rdi       ; stdout, will not change after syscall
    mov rsi, y    ; will not change after syscall
    mov rdx, 8192 ; will not change after syscall
_loop:
    mov rax, 1    ; sys_write
    syscall
jmp _loop
y:      times 4096 db "y", 0xA

$ nasm -f elf64 yes.s
$ ld yes.o -o yes
$ ./yes | pv > /dev/null
... [10.2GiB/s] ...

It looks like we can't outdo C nor GNU in this case. Buffering is the secret, and all the overhead incurred by the kernel throttles our memory access, pipes, pv, and redirection is enough to negate 1.5 GiB/s.

What have we learned?

  • Buffer your I/O for faster throughput

  • Traverse source files for information

  • You can't out-optimize your hardware

Edit: _mrb managed to edit pv to reach over 123GiB/s on his system!

Edit: Special mention to agonnaz's contribution in various languages! Extra special mention to Nekit1234007's implementation completely doubling the speed using vmsplice!


r/unix • • Aug 14 '25

Saved these from going to trash

Post image
1.2k Upvotes

Yesterday I found my PhD advisor organizing her books at her office and in the pile to be thrown away I found these two


r/unix • • Aug 31 '25

Is the Unix philosophy dead or just sleeping?

1.1k Upvotes

Been writing C since the 80s. Cut my teeth on Version 7. Watching modern software development makes me wonder what happened to "do one thing and do it well."

Today's tools are bloated Swiss Army knives. A text editor that's also a web browser, mail client, and IRC client. Command line tools that need 500MB of dependencies. Programs that won't even start without a config file the size of War and Peace.

Remember when you could read the entire source of a Unix utility in an afternoon? When pipes actually meant something? When text streams were all you needed?

I still write tools that way. But I feel like a dinosaur.

How many of you still follow the old ways? Or am I just yelling at clouds here?

(And don't tell me about Plan 9. I know about Plan 9.)


r/unix • • May 09 '26

Emailed Ken Thompson about the UNICS to UNIX namechange. Actually got a reply!

Post image
783 Upvotes

Of course I asked him if it's okay for me to share this on the internet. Really didn't expect him to respond!


r/unix • • Sep 09 '25

Happy Birthday to the legend!

Post image
738 Upvotes

Remembering a Legend šŸ™Œ

šŸ“œ His contributions shaped the foundations of modern computing and inspired generations of developers. šŸ–„ļøā¤ļø


r/unix • • Dec 21 '25

UNIX v4, the 1st version rewritten in C, was successfully recovered from tape this weekend — & here it is running in SimH on IRIX.

Thumbnail
oldbytes.space
690 Upvotes

For children under 50, the amazing bit is the contents of the big window in the middle, not the windows themselves.


r/unix • • Mar 28 '26

Just got my fresh copy of the Holy Grail

Thumbnail
gallery
584 Upvotes

r/unix • • Jun 29 '25 Helpful

Unix ftw

Post image
540 Upvotes

No more MS Windows over here! Only Macs as personal devices and Linux/BSD servers!


r/unix • • Dec 13 '25

Is one allowed to grow a Unix beard after reading most of these books?

Post image
467 Upvotes

I am currently halfway through APITUE, and have read about 1/4 of the rest. I plan on finishing them in the upcoming years.

The only remaining classic that I'm chasing to add to the collection is "The UNIX Programming Environment" by Kernighan & Pike.


r/unix • • Dec 22 '24 Polished Silver Turd

Thanks to your help, I got NetBSD 10 on my Sun Ultra 5!!!

Post image
460 Upvotes

Thanks everyone for your help yesterday! I finally got NetBSD running on my Sun Ultra 5! Woohoo!


r/unix • • Feb 28 '20

Best description of SSH I've seen

Post image
337 Upvotes

r/unix • • Jan 19 '21

Just realized this... Dennis Ritchie is the true innovator!

Post image
322 Upvotes

r/unix • • Jul 02 '26

You are not expected to understand this

Post image
298 Upvotes

(the famous bit of source code in unix v6)

And indeed I don't, not really. But I can compile it, that's something already :-)


r/unix • • Jan 02 '25

Mentally stable? No

Post image
273 Upvotes

r/unix • • Dec 23 '25

UNIX V4 tape successfully recovered <- I wrote up the data recovery -- and got it running

Thumbnail
theregister.com
255 Upvotes

r/unix • • Aug 30 '26

Ken Thompson (2024) — 4 hours on Unix, C, Plan 9, Go, Bell Labs, and computer security

Thumbnail
youtube.com
251 Upvotes

Ken Thompson walks through his entire career—from Berkeley, Bell Labs, Multics, and the creation of Unix and C with Dennis Ritchie, to Plan 9, computer security (ā€œReflections on Trusting Trustā€), computer chess, Google, and the origins of Go.

It’s nearly four hours long, but it’s fascinating to hear the stories directly from one of the people who shaped modern computing.

Highly recommended if you’re interested in Unix history or software engineering.


r/unix • • Jan 25 '26

Do I have a problem? Nah...

Post image
246 Upvotes

r/unix • • Sep 16 '25

Was programming easier and more simple in the past?

244 Upvotes

Hello!
For context, I am a 22 years old who's working in web development. My perception is that old stuff had a better quality, generally speaking. I could be wrong but I think that also applies to software. I started programming in high school with C++ and from there I switched to higher level programming languages like Python, Lua or Java. I can't say that I am an expert but I feel like old code followed much simpler patterns that made it more readable.

Today, I am asked to know like dozens of different frameworks that, in my opinion, do more harm than good. For instance, I don't understand why a simple news website can't be built using only plain HTML and CSS? Why does it need JavaScript? All that bloat is in the end taxing the performance of the device the end user owns. And even so, the majority of dynamic websites could be built entirely in HTML and CSS with parts using basic JavaScript for real time data or updates (these are called widgets). But in reality, the majority of websites are built with frameworks like React and Angular that add a lot of overhead and makes, in my opinion, the development much more complex than it should be.

What I find worse is that even desktop applications are literally dead - nobody makes GUI applications in native code anymore. Instead, they build all these apps in JavaScript, emulating a browser engine behind the scenes. If it were not for that, I am sure that 8GB RAM would've remained the norm much longer than it has for a desktop system. The pretext is that they are cross platform but in reality you still have to rewrite the style for each type of screen available out there.

I totally agree that software has evolved but to me it kind of seems it just stopped in 2015. Since then, we keep upgrading our hardware but the software evolution is minimal. Social media apps are the same as they were 10 years ago, 3D rendering capabilities didn't evolve dramatically and Microsoft Word can barely run good on a new laptop with i7 CPU and it's a text editor! The jump from 1990-2010 was magic and entartaining to watch whilst the jump from 2015-2025 is boring, predictable and just worsens with aritifically induced complexity added to everything.

I was reading the source code of Grand Theft Auto III which was written in C++ and the code there made a lot of sense. Sure, much boilerplate but necessary for clarity and to satisfy the language's needs. Today if I open a project from the internet I can barely understand what is going on. What is "var T_q" supposed to mean? I don't get it. I know that programming becoming more mainstream caused some drops in code quality, but a company never asks me how well I plan the architecture of my code, they only want to see how I made a CRUD app in 10 different stacks.

Everyone puts pressure on new patterns and paradigms and modularity but all this modularity is taught bad. It's so hard to have pure modularity in a closed system that, in my opinion, it's not worth it. That closed system should be modular in itself to other systems if that makes sense. The overusage of observer pattern or lots of weird magic functions and abstractions are hiding the code flow and makes debugging harder compared to simple more robust patterns like the finite state machine which is mainy procedurally written code.

I think I wrote a lot haha. What do you think?
Have a nice day!


r/unix • • Jan 05 '26

Another UNIX Bites the Dust - HP-UX End of Life as of December 31, 2025

Thumbnail osnews.com
233 Upvotes

Yet Another Commercial UNIX has officially bitten the dust. HP Enterprise is now the owner of three defunct Commercial Unixes, Tru64, IRIX and now HP-UX. My hope is one day either HP-UX or those other Oses get sold to other companies like VMS or open sourced like OpenSolaris.

HP-UX ultimately died due to being tied to hardware with no future, PA-Risc and Itanium. Once Itanium died there was no way to continue on. HP did consider a port to x86, but decided against it. Commercial Unixes were generally not very portable and very much bespoke systems designed to sell specific hardware platforms. This has the advantage of being very performance optimized for those platforms. That's one area where Linux, BSDs, and Windows NT did better than Unix System V variants, portability. HP UX hardware was also not very affordable nor did HP try hard to market it or spread it, they got complacent like the other Unix vendors.

IBM is the last Unix System V variant (AIX) with its own hardware (power architecture). AIX is smaller than it used to be, but still has a healthy market niche like its mainframes and Z/OS does. Sparc hardware development ended in 2017 and Fujitsu plans to sell Sparc servers till 2029 with support ending in 2034. Solaris 11.4 supports ends 2038, so unless we get an 11.5 release then Solaris will bite the dust then.

HP-UX was a robust and reliable OS that was great for mission critical applications. HP themselves provided excellent support, any issue you had they could fix easily and send an expert to walk you through it. You could upgrade the hardware while the OS was still running. It has excellent tools like LVM, Serviceguard, SAM and VPARS. It did backwards compatibility with both drivers and software very well.

While HP-UX might to dead at HPE, it will always live in people's hearts.


r/unix • • Mar 25 '25

TIL the Bourne Shell uses a very weird programming style

233 Upvotes

https://i.imgur.com/6gvscMy.png

I was browsing the old Unix code and was really surprised to see what the Bourne shell source looks like. It uses so many macros it doesn't look anything like C -- all the standard keywords and braces are gone, and is made to look like pseudo-shell code, but with the keywords in caps so it kind of looks like BASIC.

It stands out especially because of the rest of the Unix source code is very regular C.

I dug a bit deeper and saw that this was still used in this format in BSD releases right through 4.3BSD (it was rewritten as with all the original Unix code after that).

Here's a browsable version of the code from V7:

https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh


r/unix • • Jan 14 '26

Windows slips below 30% of the global operating system market share, as Unix-like operating systems dominate overall.

Thumbnail gs.statcounter.com
228 Upvotes

r/unix • • Nov 21 '25

Got a Book in the Mail..

Thumbnail
gallery
227 Upvotes

(ANSI) C Programming Language


r/unix • • Jan 07 '26

My license plate collection

Post image
220 Upvotes

All of my UNIX/Linux license plates.


r/unix • • 10d ago

šŸµ Ummmm #belllabs #UNIX #operatingsystem #CSRG

Post image
213 Upvotes