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
lockfree fixed size sublist cache #4451
Open
HeavyHorst
wants to merge
2
commits into
nats-io:main
Choose a base branch
from
HeavyHorst:main
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,101 @@ | ||
// Copyright 2016-2023 The NATS Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package server | ||
|
||
import ( | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
type Cache struct { | ||
data [slCacheMax]atomic.Pointer[cacheEntry] | ||
hashFunc func(string) int | ||
} | ||
|
||
type cacheEntry struct { | ||
key string | ||
value *SublistResult | ||
} | ||
|
||
type stringStruct struct { | ||
str unsafe.Pointer | ||
len int | ||
} | ||
|
||
//go:noescape | ||
//go:linkname memhash runtime.memhash | ||
func memhash(p unsafe.Pointer, h, s uintptr) uintptr | ||
|
||
// MemHashString is the hash function used by go map, it utilizes available hardware instructions | ||
func MemHashString(str string) int { | ||
ss := (*stringStruct)(unsafe.Pointer(&str)) | ||
return int(uint64(memhash(ss.str, 0, uintptr(ss.len))) % slCacheMax) | ||
} | ||
|
||
func NewCache(hashFunc func(string) int) *Cache { | ||
return &Cache{ | ||
hashFunc: hashFunc, | ||
} | ||
} | ||
|
||
func (c *Cache) Set(key string, value *SublistResult) { | ||
if c == nil { | ||
return | ||
} | ||
|
||
index := c.hashFunc(key) | ||
entry := &cacheEntry{key: key, value: value} | ||
c.data[index].Store(entry) | ||
} | ||
|
||
func (c *Cache) Get(key string) (*SublistResult, bool) { | ||
if c == nil { | ||
return nil, false | ||
} | ||
|
||
index := c.hashFunc(key) | ||
entry := c.data[index].Load() | ||
if entry != nil { | ||
if entry.key == key { | ||
return entry.value, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (c *Cache) Delete(key string) { | ||
index := c.hashFunc(key) | ||
c.data[index].Store(nil) | ||
} | ||
|
||
func (c *Cache) Len() int { | ||
var cc int | ||
c.Iterate(func(key string, value *SublistResult) { | ||
cc++ | ||
}) | ||
return cc | ||
} | ||
|
||
func (c *Cache) Iterate(cf func(key string, value *SublistResult)) { | ||
if c == nil { | ||
return | ||
} | ||
|
||
for i := range c.data { | ||
p := c.data[i].Load() | ||
if p != nil { | ||
cf(p.key, p.value) | ||
} | ||
} | ||
} |
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.
While the idea of the PR is interesting and the results promising, relying on
go:linkname
in this way is dangerous as it ties us into the behaviour of a specific Go compiler implementation. This probably means that NATS Server would no longer compile usinggccgo
for example.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 changed it to use the maphash package (what is already used in the server).
The performance seems to be even a little bit better.
Sidenote:
the maphash package does the same here: https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/hash/maphash/maphash_runtime.go
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.
Thanks for switching to the
maphash
package, indeed it does call out withgo:linkname
in the happy case, but more importantly, it has apurego
fallback.