What is a buffer?


May 05, 2005

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.