intern

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2021 License: MIT Imports: 4 Imported by: 1

README

A string interning library for Go

GoDoc

intern has a number of benefits

  1. It deduplicates strings. if you have data that references identical text in very many different places if may significantly reduce the amount of memory used for the strings.
  2. It stores the strings in a way that reduces the load on the garbage collector.
  3. It allows you to store an int ID for the string instead of the string itself. This is considerably smaller, and again is GC friendly.
i := intern.New()
hat := i.Save("hat")
fmt.Printf(i.Get(hat))

Documentation

Overview

Package intern is a string interning library. Pass it a string, and it will store it and return it, removing duplicates. That is, however many times you show it a string, it will only store that string once, and will always return a version of it backed by the same memory.

Storage is kind to GC. It is optimised for storing a very large number of strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Intern

type Intern struct {
	stringbank.Stringbank
	// contains filtered or unexported fields
}

Intern implements the interner. Allocate it

func New

func New(cap int) *Intern

New creates a new interning table

func (*Intern) Cap

func (i *Intern) Cap() int

Cap returns the size of the intern table

func (*Intern) Deduplicate

func (i *Intern) Deduplicate(val string) string

Deduplicate takes a string and returns a permanently stored version. This will always be backed by the same memory for the same string.

func (*Intern) Len

func (i *Intern) Len() int

Len returns the number of unique strings stored

func (*Intern) Save

func (i *Intern) Save(val string) int

Save stores a string in out deduplicated string store, and returns an integer offset for accessing it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL