Skip to content

html: reduce entity memory usage and init load #26727

@bradfitz

Description

@bradfitz

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 uses html.EscapeString. Such code should never need to run init() 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 a gen-entity.go file and go generate an ugly zentity.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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions