Skip to content
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

runtime: don't allocate when converting a single byte to string #24172

Closed
mvdan opened this issue Feb 28, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@mvdan
Copy link
Member

commented Feb 28, 2018

For example:

var StringSink string

func Foo(bs []byte) {
    StringSink = string(bs[0])  // no allocation
    StringSink = string(bs[:1]) // no allocation
    StringSink = string(bs)     // no allocation if len(bs)==1
}

At the moment, all three allocate, easily measured with:

func BenchmarkFoo(b *testing.B) {
        bs := []byte("foobar")
        for i := 0; i < b.N; i++ {
                Foo(bs)
        }
}

There is some precedent to this, I believe. For example:

This was even briefly mentioned by @mdempsky in 2016, but as far as I can see it was never followed up on: #17725 (comment)

Another potential route would be for string(bs[0]) and string(bs[:1]) to not allocate thanks to the compiler, as it can statically know that len==1. However, that could be built on top of the runtime optimization, as a form of skipping the len check and calling the non-allocating runtime func directly.

If there is interest in investigating the performance penalty of such a change, I can work on a prototype CL to get some measurements. Unless this was already measured and discarded by someone, and I haven't been able to find a thread for it.

/cc @randall77 @josharian

@gopherbot gopherbot added this to the Proposal milestone Feb 28, 2018

@gopherbot gopherbot added the Proposal label Feb 28, 2018

@randall77

This comment has been minimized.

Copy link
Contributor

commented Feb 28, 2018

Go ahead. This is small enough I don't think we need the proposal process for it.

@randall77 randall77 changed the title Proposal: runtime: don't allocate when converting a single byte to string runtime: don't allocate when converting a single byte to string Feb 28, 2018

@randall77 randall77 added Performance and removed Proposal labels Feb 28, 2018

@randall77 randall77 modified the milestones: Proposal, Go1.11 Feb 28, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Feb 28, 2018

Change https://golang.org/cl/97717 mentions this issue: runtime: don't allocate to build strings of length 1

@josharian

This comment has been minimized.

Copy link
Contributor

commented Feb 28, 2018

I had a change sitting around in my tree from Sat Mar 4 16:55:03 2017 to do this. Apparently I had decided at the time that it wasn't worth it? Unclear. Anyway, I mailed it. The perf numbers may be stale.

And to echo Keith, for small- to medium-sized optimization ideas, no proposal needed in general.

@mvdan

This comment has been minimized.

Copy link
Member Author

commented Feb 28, 2018

Oh, awesome. I used a proposal in case there was a good reason not to do this that I wasn't aware of.

@gopherbot gopherbot closed this in 9372e3f Mar 1, 2018

@golang golang locked and limited conversation to collaborators Mar 1, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.