pax_global_header00006660000000000000000000000064135752571060014525gustar00rootroot0000000000000052 comment=8e6ff32b3e7c0b018c43953085fe2ac330fe9acd intern-1.0.0/000077500000000000000000000000001357525710600130225ustar00rootroot00000000000000intern-1.0.0/README.md000066400000000000000000000001741357525710600143030ustar00rootroot00000000000000Docs: https://godoc.org/github.com/josharian/intern See also [Go issue 5160](https://golang.org/issue/5160). License: MIT intern-1.0.0/go.mod000066400000000000000000000000531357525710600141260ustar00rootroot00000000000000module github.com/josharian/intern go 1.5 intern-1.0.0/intern.go000066400000000000000000000014261357525710600146530ustar00rootroot00000000000000// Package intern interns strings. // Interning is best effort only. // Interned strings may be removed automatically // at any time without notification. // All functions may be called concurrently // with themselves and each other. package intern import "sync" var ( pool sync.Pool = sync.Pool{ New: func() interface{} { return make(map[string]string) }, } ) // String returns s, interned. func String(s string) string { m := pool.Get().(map[string]string) c, ok := m[s] if ok { pool.Put(m) return c } m[s] = s pool.Put(m) return s } // Bytes returns b converted to a string, interned. func Bytes(b []byte) string { m := pool.Get().(map[string]string) c, ok := m[string(b)] if ok { pool.Put(m) return c } s := string(b) m[s] = s pool.Put(m) return s } intern-1.0.0/intern_test.go000066400000000000000000000022601357525710600157070ustar00rootroot00000000000000// +build !race // When -race is enabled, sync.Pool is a no-op, // which will cause these tests to fail // and these benchmarks to be misleading. package intern import ( "bytes" "reflect" "testing" "unsafe" ) func TestString(t *testing.T) { s := "abcde" sub := String(s[1:4]) interned := String("bcd") want := (*reflect.StringHeader)(unsafe.Pointer(&sub)).Data got := (*reflect.StringHeader)(unsafe.Pointer(&interned)).Data if want != got { t.Errorf("failed to intern string") } } func TestBytes(t *testing.T) { s := bytes.Repeat([]byte("abc"), 100) n := testing.AllocsPerRun(100, func() { for i := 0; i < 100; i++ { _ = Bytes(s[i*len("abc") : (i+1)*len("abc")]) } }) if n > 0 { t.Errorf("Bytes allocated %d, want 0", int(n)) } } func BenchmarkString(b *testing.B) { in := "hello brad" b.ReportAllocs() b.SetBytes(int64(len(in))) b.RunParallel(func(pb *testing.PB) { var s string for pb.Next() { s = String(in[1:5]) } _ = s }) } func BenchmarkBytes(b *testing.B) { in := []byte("hello brad") b.ReportAllocs() b.SetBytes(int64(len(in))) b.RunParallel(func(pb *testing.PB) { var s string for pb.Next() { s = Bytes(in[1:5]) } _ = s }) } intern-1.0.0/license.md000066400000000000000000000020651357525710600147710ustar00rootroot00000000000000MIT License Copyright (c) 2019 Josh Bleecher Snyder Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.