Skip to content

os: make File.WriteString not allocate #42406

@josharian

Description

@josharian

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions