-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.Proposal-Accepted
Milestone
Description
Years ago I filed #6714, hoping that it would one day be possible to construct a string efficiently without wasted allocations.
I hereby propose that we stop waiting and just do it explicitly with a new type in a new internal package.
package builder // import "internal/builder"
// String is like a bytes.Buffer but never allows its internal
// byte slice to escape. The zero value is usable.
type String struct {
// ...
}
func (s *String) Len() int { ... }
func (s *String) Grow(n int) { ... }
func (s *String) WriteByte(b byte) { ... }
func (s *String) WriteString(s string) { ... }
func (s *String) Write(b []byte) { ... }
func (s *String) String() string { ... }
The (*String).String
method would use unsafe
to make a string header out of an internal []byte
slice header.
The Write
methods could even recycle old too-small []byte
backing arrays as they grow.
There would be no Reset
or Bytes
or Truncate
or Read
methods. Nothing that could mutate the []byte
once it was unsafely converted to a string
.
The implementation would have to take care to use a new []byte
backing array on any resize after an unsafe string
was constructed.
cristaloleg, bep, bketelsen, cznic, davidrjenni and 22 more
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.Proposal-Accepted