Skip to content
/ intern Public

Go package that maps strings to integers for fast comparisons

License

Notifications You must be signed in to change notification settings

spakin/intern

Repository files navigation

intern

Go Report Card Build Status Go Reference

Description

String interning traditionally means mapping multiple strings with equal contents to a single string in memory. Doing so improves program speed by replacing slow string comparisons with fast pointer comparisons.

The intern package for Go takes a slightly different approach: It maps strings to integers. This serves two purposes. First, in Go, unlike C, a string is not simply a pointer to an array of characters so the pointer-comparison approach doesn't apply as naturally in Go as it does in C. Second, the package provides a rather unique mechanism for preserving order. That is, strings can be mapped to integers in such a way that if one string precedes another alphabetically, then the first string will map to a smaller integer than the second string.

More specifically, intern provides two symbol types: Eq, which supports only comparisons for equality and inequality (== and !=); and LGE, which supports less than, greater than, and equal to comparisons (<, >, ==, <=, >=, and !=). The former is faster to allocate, and allocation always succeeds. The latter is slower to allocate and generally requires a pre-allocation step to avoid running out of integers that preserve the less than/greater than/equal to properties between all pairs of interned strings.

Installation

Assuming you're using the Go module system, simply import the package into your program with

import "github.com/spakin/intern"

and run

go mod tidy

to download and install intern.

Documentation

Descriptions and examples of the intern API can be found online in the pkg.go.dev documentation.

Author

Scott Pakin, scott+int@pakin.org

About

Go package that maps strings to integers for fast comparisons

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages