May 05, 2005

What is a buffer?

Today's question comes from Adam Steinbaugh of Wittier, California.

Adam writes:

hey Josh -- what, exactly, is a buffer?

Well, Adam, a buffer is an intermediary space in memory where data is stored before being processed and shuffled off to some place else.

What good is a buffer?

Lets say that you're continually gathering input from somewhere and writing it to disk. Writing to disk is an expensive operation - much more expensive than pushing bytes around in memory. Each time you receive a byte, it would be a waste of time to write it to disk immediately because then you're doing a lot of expensive operations.

Much better to first read the incoming bytes into a memory buffer and then flush the entire contents of the buffer (say 1024 bytes) to disk en mass when the buffer is full. This way, you can still process each byte as it comes in, but you minimize the number of disk writes that you have to perform.

Make sense?

Let's look at things another way.

Let's say that you're moving into a new apartment. You have to move all your stuff from your old apartment.

Unfortunately you made the mistake of investing in a 1969 VW bug for transportation, and in it, you can only carry one of your belongings at a time.

Instead of using your own wheels to drive back and forth to your new apartment twenty times, you get your buddy to bring over his VW bus. You pack all your shit in the bus at once and move everything in one trip. After you're finished moving, Natalie Portman comes over to your place, and you make out.

In this story, the VW bus is your buffer.

In Technology and Software

Posted by Josh Staiger at 11:39 PM

Comments

Duder, you definitely have to answer every question like this from now on.

Posted by: Evan Perry May 6, 2005 12:27 AM | Permanent link

That was by far the best explanation of a buffer I've ever seen. Make sure to update wikipedia with that example.

Posted by: Vincent May 7, 2005 09:22 PM | Permanent link