Skip to content

proposal: strings: a string factory to convert bytes to string #32593

@jmoh932

Description

@jmoh932

Problem:

Each time converting a bytes to string 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 function New([]byte) string which converts bytes to string with the memory allocation optimised, and a concurrent-less struct Factory for the internal usage of Goroutines.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions