bytes.NewBuffer(nil) vs var buf bytes.Buffer

4,581 views
Skip to first unread message

Erik Unger

unread,
Feb 5, 2012, 7:09:20 AM2/5/12
to golang-nuts
I noticed that bytes.NewBuffer(nil) is used a lot in Go's standard
packages.

Wouldn't it be more efficient to use var buf bytes.Buffer and then
&buf in cases where the buffer is only used locally to avoid dynamic
memory allocations?

Or does the go compiler optimize that away?

-Erik

Jessta

unread,
Feb 5, 2012, 8:11:12 AM2/5/12
to Erik Unger, golang-nuts
On Sun, Feb 5, 2012 at 11:09 PM, Erik Unger <ung...@gmail.com> wrote:
> I noticed that bytes.NewBuffer(nil) is used a lot in Go's standard
> packages.
> Wouldn't it be more efficient to use var buf bytes.Buffer and then
> &buf in cases where the buffer is only used locally to avoid dynamic
> memory allocations?

bytes.NewBuffer() should only be used to prepare a buffer for reading
from or to make the buffer a specific size.
bytes.NewBuffer(nil) is exactly the same as &bytes.Buffer{} so it's weird that
this appears in the stdlib.

> Or does the go compiler optimize that away?

The compiler would, but it's still kind of silly.

- jessta

--
=====================
http://jessta.id.au

Andrew Gerrand

unread,
Feb 5, 2012, 8:14:37 AM2/5/12
to golan...@googlegroups.com, nige...@golang.org


On Sunday, February 5, 2012 11:09:20 PM UTC+11, Erik Unger wrote:
I noticed that bytes.NewBuffer(nil) is used a lot in Go's standard
packages.

Actually there are only a handful of occurrences of that form. Mostly we either use the "var buf bytes.Buffer" form, or "buf := new(bytes.Buffer)". There's no tangible performance benefit to any of the approaches.

"var [a-z]+ bytes.Buffer"

"new(bytes.Buffer)"

By the looks of it, it's Nigel who mostly favours "NewBuffer(nil)". He's special. ;-)

Andrew

unread,
Feb 6, 2012, 5:18:04 AM2/6/12
to golang-nuts
On Feb 5, 2:11 pm, Jessta <jes...@jessta.id.au> wrote:
> On Sun, Feb 5, 2012 at 11:09 PM, Erik Unger <unge...@gmail.com> wrote:
> > Or does the go compiler optimize that away?
>
> The compiler would, but it's still kind of silly.

I wouldn't be silly in an optimizing compiler.

roger peppe

unread,
Feb 6, 2012, 5:26:51 AM2/6/12
to ⚛, golang-nuts

Quite right too. Optimizing compilers look very sternly on
silly behaviour in their innards.

unread,
Feb 6, 2012, 6:15:12 AM2/6/12
to golang-nuts
Correction: It wouldn't be silly in an optimizing compiler.

unread,
Feb 6, 2012, 6:15:25 AM2/6/12
to golang-nuts
On Feb 6, 11:26 am, roger peppe <rogpe...@gmail.com> wrote:
What do you mean?

roger peppe

unread,
Feb 6, 2012, 6:27:03 AM2/6/12
to ⚛, golang-nuts

just being silly with your spelling mistake... :-)

Russ Cox

unread,
Feb 7, 2012, 5:13:39 PM2/7/12
to ⚛, golang-nuts
On Mon, Feb 6, 2012 at 06:15, ⚛ <0xe2.0x...@gmail.com> wrote:
> Correction: It wouldn't be silly in an optimizing compiler.

It is still best to write the simplest, clearest code you can.
Writing unnecessarily complex or inefficient code and leaving
it for the compiler to clean up is not a winning strategy.
http://prog21.dadgum.com/40.html

Russ

Reply all
Reply to author
Forward
0 new messages