-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
bytes: add Extend method #9638
Comments
I didn't try to talk him out of it. I just made some observations and asked questions. I'll make another: do we want |
Some of the use cases this could replace could be statically analyzed and optimized. For example, using Historically, performance/optimization considerations have not made it into the language (e.g. in the form of compiler hinting keywords) or the stdlib. The allocation and copying avoidance should therefore be given little weight, as an eventual compiler could make such optimizations implicit anyway. If the non-performance merits of bytes.Extend are enough to still warrant inclusion, the function definition as proposed is a bit unconventional, since it adds convenience at the expense of increasing complexity. I would recommend |
I agree, optimizing Absent that optimization, an extend function in bytes would also be very useful. The function provided is very slow however, and it does not zero out the bytes between len and cap. I came up with this, which allocates less and is faster in every case:
|
@PieterD why do |
Boy, are my ears red. Yeah, that is a lot better. That pretty much invalidates the mid case as well, and what remains is pretty pitiful. And here I thought I was being all clever. Oops! It's true that zero would be false for most cases, and should you want it zeroed you can always do it yourself after extending. However, should you want to zero it, you would only have to do anything if you weren't allocating. Which I suppose would be most of the time anyway, since at least in my case I re-use a lot of buffers. So I guess you're right, it would be exceptionally rare. Looks like I agree with you on all counts. Thank you. |
I do think there's something useful here, but perhaps not useful enough. Not for Go 1.5 at least. |
Bryan Ford suggests adding something like this:
It could be used to grow a slice to make room for a direct write,
instead of writing to a temporary copy and using append.
For example, sha1's Sum method does:
That code was written carefully to return a fixed-size array so that d.checkSum didn't allocate, and we know that append doesn't capture hash, so there's no allocation other than the append.
The code could instead change checkSum to write the 20 bytes to its argument (renaming to writeSum) and use:
Brad and Minux talked him out of it, but I am not so sure.
To consider for Go 1.5.
The text was updated successfully, but these errors were encountered: