Go: os: make File.WriteString not allocate

Created on 5 Nov 2020  ·  5Comments  ·  Source: golang/go

This is os.File.WriteString's implementation:

// WriteString is like Write, but writes the contents of string s rather than
// a slice of bytes.
func (f *File) WriteString(s string) (n int, err error) {
    return f.Write([]byte(s))
}

This currently accounts for ~10%~ 35% of allocated space in a service I'm maintaining.

We should be able to do an unsafe cast of s to b here, and avoid the allocation.

Do any kernels modify the bytes during Write? Any other reason why we wouldn't be able to do this?

cc @bradfitz

NeedsDecision Performance

Most helpful comment

@josharian Permit me to introduce you to the exciting new internal/unsafeheader package.

All 5 comments

Problem: os can't import reflect, at least according to existing dependency rules. And I don't think https://github.com/golang/go/issues/19367 will help here.

There are options: add go:linkname with package runtime support, allow os to import reflect, add StringHeader/SliceHeader to package reflectlite and teach the compiler about that. I imagine the first is the most palatable.

@josharian you are Josh and can make it work perhaps in the compiler by special recognition, if the symbol is a (*os.File).WriteString and its on an architecture that we've audited as won't be mutating bytes, rewrite it :)

@josharian Permit me to introduce you to the exciting new internal/unsafeheader package.

Oooooooooooooooooh!

Change https://golang.org/cl/268020 mentions this issue: os: avoid allocation in File.WriteString

Was this page helpful?
0 / 5 - 0 ratings