On Thu, Sep 6, 2018 at 12:22 AM Eric Raymond <e...@thyrsus.com> wrote: > Matters in my case because the deserialization of a repository history can > contain hundreds of thousands of constants like "M", "D", "R", and "C" > representing fast-export stream file operation types. I could intern them > explicitly but it avoid code clutter and some lookup time if I don't have to.
If you're relatively new to Go, it might not be obvious that instead of using a string-typed field or value, you can use an integer-type. Int comparison is faster than string comparison, as string comparison requires pointer following and can be O(N) in the string length. It also occupies less memory: a uint8 is obviously 1 byte, whereas a string (pointer and len) can be 16 bytes on a 64-bit system, not counting those N additional bytes. Similar to comparison speed, indexing an array by a (dense) integer is faster than indexing a map by a string. You can implement a String method on that integer-type (in Go, unlike C++, integer-types can have methods), for human-readable debugging or for re-serialization. For example: https://play.golang.org/p/u9zC3CKumb0 For a bigger example of the same technique, look at the "golang.org/x/net/html/atom" package (the source is at https://go.googlesource.com/net/+/master/html/atom/), which maps strings (or []byte values) like "body" and "div" to integer constants atom.Body and atom.Div. One level up in the directory hierarchy, the html package implements the HTML5 parsing algorithm (which is switch-heavy, e.g. https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody), and having that parser use integer comparisons throughout (`x == atom.Head` instead of `x == "head"`) lead to a significant (1.1x or 1.2x, IIRC) speedup on some benchmarks. One technique from the HTML parser code (https://go.googlesource.com/net/+/master/html/parse.go) is to rename the import: import a "golang.org/x/net/html/atom" // Note the a after the import. so that, in the rest of the file, you can refer to "a.Table" instead of "atom.Table". -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.