├── go.mod ├── README.md ├── intern.go ├── LICENSE.md └── intern_test.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/josharian/intern 2 | 3 | go 1.5 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Docs: https://godoc.org/github.com/josharian/intern 2 | 3 | For a blog post introducing string interning, see: https://commaok.xyz/post/intern-strings/ 4 | 5 | License: MIT 6 | -------------------------------------------------------------------------------- /intern.go: -------------------------------------------------------------------------------- 1 | // Package intern interns strings. 2 | // Interning is best effort only. 3 | // Interned strings may be removed automatically 4 | // at any time without notification. 5 | // All functions may be called concurrently 6 | // with themselves and each other. 7 | package intern 8 | 9 | import "sync" 10 | 11 | var ( 12 | pool sync.Pool = sync.Pool{ 13 | New: func() interface{} { 14 | return make(map[string]string) 15 | }, 16 | } 17 | ) 18 | 19 | // String returns s, interned. 20 | func String(s string) string { 21 | m := pool.Get().(map[string]string) 22 | c, ok := m[s] 23 | if ok { 24 | pool.Put(m) 25 | return c 26 | } 27 | m[s] = s 28 | pool.Put(m) 29 | return s 30 | } 31 | 32 | // Bytes returns b converted to a string, interned. 33 | func Bytes(b []byte) string { 34 | m := pool.Get().(map[string]string) 35 | c, ok := m[string(b)] 36 | if ok { 37 | pool.Put(m) 38 | return c 39 | } 40 | s := string(b) 41 | m[s] = s 42 | pool.Put(m) 43 | return s 44 | } 45 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Josh Bleecher Snyder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /intern_test.go: -------------------------------------------------------------------------------- 1 | // +build !race 2 | 3 | // When -race is enabled, sync.Pool is a no-op, 4 | // which will cause these tests to fail 5 | // and these benchmarks to be misleading. 6 | 7 | package intern 8 | 9 | import ( 10 | "bytes" 11 | "reflect" 12 | "testing" 13 | "unsafe" 14 | ) 15 | 16 | func TestString(t *testing.T) { 17 | s := "abcde" 18 | sub := String(s[1:4]) 19 | interned := String("bcd") 20 | want := (*reflect.StringHeader)(unsafe.Pointer(&sub)).Data 21 | got := (*reflect.StringHeader)(unsafe.Pointer(&interned)).Data 22 | if want != got { 23 | t.Errorf("failed to intern string") 24 | } 25 | } 26 | 27 | func TestBytes(t *testing.T) { 28 | s := bytes.Repeat([]byte("abc"), 100) 29 | n := testing.AllocsPerRun(100, func() { 30 | for i := 0; i < 100; i++ { 31 | _ = Bytes(s[i*len("abc") : (i+1)*len("abc")]) 32 | } 33 | }) 34 | if n > 0 { 35 | t.Errorf("Bytes allocated %d, want 0", int(n)) 36 | } 37 | } 38 | 39 | func BenchmarkString(b *testing.B) { 40 | in := "hello brad" 41 | b.ReportAllocs() 42 | b.SetBytes(int64(len(in))) 43 | b.RunParallel(func(pb *testing.PB) { 44 | var s string 45 | for pb.Next() { 46 | s = String(in[1:5]) 47 | } 48 | _ = s 49 | }) 50 | } 51 | 52 | func BenchmarkBytes(b *testing.B) { 53 | in := []byte("hello brad") 54 | b.ReportAllocs() 55 | b.SetBytes(int64(len(in))) 56 | b.RunParallel(func(pb *testing.PB) { 57 | var s string 58 | for pb.Next() { 59 | s = Bytes(in[1:5]) 60 | } 61 | _ = s 62 | }) 63 | } 64 | --------------------------------------------------------------------------------