Skip to content

Capacity inconsistency for byte slices from strings #18424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dlsniper opened this issue Dec 24, 2016 · 7 comments
Closed

Capacity inconsistency for byte slices from strings #18424

dlsniper opened this issue Dec 24, 2016 · 7 comments

Comments

@dlsniper
Copy link
Contributor

dlsniper commented Dec 24, 2016

What version of Go are you using (go version)?

  • the Go Playground
  • go version go1.7.4 linux/amd64
  • go version go1.8beta2 linux/amd64

What operating system and processor architecture are you using (go env)?

  • the Go Playground

  • go 1.7:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/florin/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build121149425=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
  • go 1.8 beta 2
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/florin/go"
GORACE=""
GOROOT="/usr/local/go1.8"
GOTOOLDIR="/usr/local/go1.8/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build149699806=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

https://play.golang.org/p/mvU3emq7Yj

What did you expect to see?

// from struct
len:	4
cap:	4
// from var
len:	4
cap:	4

What did you see instead?

  • In playground:
// from struct
len:	4
cap:	32
// from var
len:	4
cap:	32
  • go 1.7.4:
// from struct
len:    4
cap:    8
// from var
len:    4
cap:    32
  • go 1.8 beta 2:
// from struct
len:    4
cap:    8
// from var
len:    4
cap:    32

I would expect that no extra memory is allocated beyond the needs of the string but the capacity seems different in both cases. What's even more bizarre is that the values are different from between locations where this is allocated and it seems that the playground is also different from my linux machine.

@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@dlsniper
Copy link
Contributor Author

For me it's unexpected. Worse, in a scenario where you have a high-traffic server or an app with hundreds of thousands of objects like this, those "few bytes" might make some differences (for example here, those few bytes are about 400 https://play.golang.org/p/oDTX87BviO ).

I understand how the runtime might preallocate for that scenario and I'm familiar with the append mechanics however, I'm expecting that what my code declares to be respected. The compiler could hint if there's an append later on or not if that's the concern we have.

@dominikh
Copy link
Member

Related: #14232

@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@randall77
Copy link
Contributor

See #14235 and https://go-review.googlesource.com/#/c/22424/ for more context on why this behaves as it does.

@dlsniper
Copy link
Contributor Author

Thank you all for the quick reply and pointers on this

@golang golang locked and limited conversation to collaborators Dec 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants