-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: strings: a string factory to convert bytes
to string
#32593
Comments
Change https://golang.org/cl/182057 mentions this issue: |
As bytes are mutable there needs to be at least one allocation (unless it memorizes and there are multiple identical strings/substrings) or pools memory for a general function to produce a string from bytes. There already is https://golang.org/pkg/strings/#Builder. How is the new strings factory better than strings.Builder and what are the tradeoffs? If its avoiding allocations by reserving a large memory pool than a single string can potentially pin a huge amount of memory which is likely not advisable for general usage. |
Sorry for the wrong work stream, I put the PR #32594 to mean to be the code draft for the further talking.
In #32594 , And for your original question, I just gave out a function |
I’m not sure I follow the request, but #5160 may be relevant. There’s also a trick that can be done in user code for the right kinds of setups: https://go-review.googlesource.com/c/164961 |
In the CL, strings.Factory.New is making a copy of the argument; so does string(x). If you really need this one-big-allocation-backing-many-strings, it would be easier just to This does not fit well at all in the standard library. I suggest we decline this proposal. |
I filed #32779 for the more limited JSON issue. |
Suggested declining this on June 25 (#32593 (comment)) and there have been no comments since then. As such, this seems like a likely decline. Leaving open for a week for final comments. |
Marked this last week as likely decline w/ call for last comments (#32593 (comment)). |
Problem:
Each time converting a
bytes
tostring
involves a memory allocation, caused by the design immutable string and mutable bytes.Investigation:
Logically, a constant string which embedded into binaries got limited usage. And the definition of string-processing functions normally got a type string as its argument type, rather than a bytes. The converting has widely occurred, and was driven by the widely usage of strings generated at program runtime.
For example, the benchmark at this Issue from other repository shows that
json.Unmarshal
are generating large number of memory fragments. Even the 3rd-part library didn't work well. Considering Golang is widely as a backend language, and those kind of calling has been put on the hot path.Solution:
Simply add a string factory to package
strings
, exported one functionNew([]byte) string
which converts bytes to string with the memory allocation optimised, and a concurrent-less structFactory
for the internal usage of Goroutines.The text was updated successfully, but these errors were encountered: