html: reduce entity memory usage and init load #26727
Labels
FrozenDueToAge
help wanted
NeedsFix
The path to resolution is known, but the work has not been done.
Performance
Milestone
The html package has two large maps that are populated during an implicit
init()
:https://github.com/golang/go/blob/master/src/html/entity.go
Because of that, the memory is always in the heap and the linker can't discard it, even if the user never uses it. (via https://golang.org/pkg/html/#UnescapeString)
On linux/amd64 at tip, it adds 110 KB of heap just to
import _ "html"
. A more realistic use case is code that only useshtml.EscapeString
. Such code should never need to runinit()
or retain that code in memory.It seems this is a perfect use case of perfect hashing.
@cespare, it looks like your https://github.com/cespare/mph is nice & simple and suitably licensed.
It seems like we could pull
entity.go
into agen-entity.go
file andgo generate
an uglyzentity.go
file that is just one big string literal, suitably packed.Then there'd be no init work to construct the map, and the linker could discard it.
/cc @josharian
The text was updated successfully, but these errors were encountered: