-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiment with string interning on BlockMeta #3931
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
41a6da4
Experiment with string interning on BlockMeta
zalegrala 53c2ecb
Include nil checks for tests
zalegrala bc96740
Drop intentional fail
zalegrala 8a5fe29
Include UnmarshalJSON on COmpactedBlockMeta
zalegrala 78f1470
Copy test structure into benchmarks
zalegrala 59fb86e
Clean unmarshal and avoid recursive unmarshal with an alias type
zalegrala 188cab5
Convert intern interface{} to string
zalegrala 4cd4ae2
Intern dedicated columns
zalegrala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package intern | ||
|
||
import "sync" | ||
|
||
// A Value pointer is the handle to an underlying comparable value. | ||
type Value struct { | ||
cmpVal string | ||
} | ||
|
||
// Get returns the comparable value passed to the Get func that returned v. | ||
func (v *Value) Get() string { return v.cmpVal } | ||
|
||
// Our pool of interned values and a lock to serialize access. | ||
var ( | ||
mu sync.Mutex | ||
val = map[string]*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, and can be used as a map key. | ||
// | ||
// Note that Get returns a *Value so we only return one word of data | ||
// to the caller, despite potentially storing a large amount of data | ||
// within the Value itself. | ||
func Get(cmpVal string) *Value { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
v := val[cmpVal] | ||
if v != nil { | ||
// Value is already interned in the pool. | ||
return v | ||
} | ||
|
||
// Value must be created now and then interned. | ||
v = &Value{cmpVal: cmpVal} | ||
val[cmpVal] = v | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package intern | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBasics(t *testing.T) { | ||
x := Get("abc123") | ||
y := Get("abc123") | ||
|
||
if x.Get() != y.Get() { | ||
t.Error("abc123 values differ") | ||
} | ||
if x.Get() != "abc123" { | ||
t.Error("x.Get not abc123") | ||
} | ||
if x != y { | ||
t.Error("abc123 pointers differ") | ||
} | ||
|
||
a1 := x.Get() | ||
a2 := y.Get() | ||
|
||
p1 := fmt.Sprintf("%08x\n", stringAddr(a1)) | ||
p2 := fmt.Sprintf("%08x\n", stringAddr(a2)) | ||
require.Equal(t, p1, p2) | ||
} | ||
|
||
func stringAddr(s string) uintptr { | ||
return uintptr(unsafe.Pointer(unsafe.StringData(s))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I experimented with the alias here as well, but the embedding throws it off. I think we keep the custom change without the alias here unless I missed something obvious.