Skip to content

Commit

Permalink
litecmp package.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Dec 18, 2020
0 parents commit bb95bb3
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2020, Brad Fitzpatrick
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 changes: 74 additions & 0 deletions litecmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2020 Brad Fitzpatrick. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package litecmp lets you make smaller comparable values by boxing
// a larger comparable value (such as a 16 byte string header) down
// into a globally unique 8 byte pointer.
//
// The globally unique pointers are garbage collected with weak
// references and finalizers. This package hides that.
package litecmp

import (
"runtime"
"sync"
"unsafe"
)

// A Value pointer is the lite handle to an underlying comparable
// value.
//
// Because the pointer is globally unique within the process for the comparable value
// passed to the Get func, the pointer itself can be used as a comparable value.
type Value struct {
_ [0]func() // prevent people from accidentally using value type as comparable
cmpVal interface{}
gen int64
}

// Get returns the comparable value passed to the Get func
// that had returned v.
func (v *Value) Get() interface{} { return v.cmpVal }

var (
mu sync.Mutex
valMap = map[interface{}]uintptr{} // to uintptr(*Value)
)

// Get returns a pointer representing the comparable value cmpVal.
//
// The returned pointer will be the same for Get(v) and Get(v2)
// if and only if v == v2.
func Get(cmpVal interface{}) *Value {
mu.Lock()
defer mu.Unlock()

addr, ok := valMap[cmpVal]
var v *Value
if ok {
v = (*Value)((unsafe.Pointer)(addr))
} else {
v = &Value{cmpVal: cmpVal}
valMap[cmpVal] = uintptr(unsafe.Pointer(v))
}
curGen := v.gen + 1
v.gen = curGen

if curGen > 1 {
// Need to clear it before changing it,
// else the runtime throws.
runtime.SetFinalizer(v, nil)
}
runtime.SetFinalizer(v, func(v *Value) {
mu.Lock()
defer mu.Unlock()
if v.gen != curGen {
// Lost the race. Somebody is still using us.
return
}
delete(valMap, v.cmpVal)
})
return v

}
59 changes: 59 additions & 0 deletions litecmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 Brad Fitzpatrick. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package litecmp

import (
"runtime"
"testing"
)

func TestBasics(t *testing.T) {
foo := Get("foo")
bar := Get("bar")
foo2 := Get("foo")
bar2 := Get("bar")

if foo.Get() != foo2.Get() {
t.Error("foo values differ")
}
if foo.Get() != "foo" {
t.Error("foo.Get not foo")
}
if foo != foo2 {
t.Error("foo pointers differ")
}

if bar.Get() != bar2.Get() {
t.Error("bar values differ")
}
if bar.Get() != "bar" {
t.Error("bar.Get not bar")
}
if bar != bar2 {
t.Error("bar pointers differ")
}

if n := mapLen(); n != 2 {
t.Errorf("map len = %d; want 2", n)
}

const gcTries = 5000
for try := 0; try < gcTries; try++ {
runtime.GC()
n := mapLen()
if n == 0 {
break
}
if try == gcTries-1 {
t.Errorf("map len = %d after (%d GC tries); want 0", gcTries, try)
}
}
}

func mapLen() int {
mu.Lock()
defer mu.Unlock()
return len(valMap)
}

0 comments on commit bb95bb3

Please sign in to comment.