Update sievecache
This commit is contained in:
parent
9b71edd2a3
commit
3dd7dffffe
7 changed files with 107 additions and 56 deletions
4
go.mod
4
go.mod
|
|
@ -1,6 +1,6 @@
|
|||
module github.com/dnscrypt/dnscrypt-proxy
|
||||
|
||||
go 1.21
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.5.0
|
||||
|
|
@ -16,7 +16,7 @@ require (
|
|||
github.com/jedisct1/go-dnsstamps v0.0.0-20240423203910-07a0735c7774
|
||||
github.com/jedisct1/go-hpke-compact v0.0.0-20241212093903-5caa4621366f
|
||||
github.com/jedisct1/go-minisign v0.0.0-20241212093149-d2f9f49435c7
|
||||
github.com/jedisct1/go-sieve-cache v0.1.0
|
||||
github.com/jedisct1/go-sieve-cache v0.1.5
|
||||
github.com/jedisct1/xsecretbox v0.0.0-20241212092125-3afc4917ac41
|
||||
github.com/k-sone/critbitgo v1.4.0
|
||||
github.com/kardianos/service v1.2.2
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -51,8 +51,8 @@ github.com/jedisct1/go-hpke-compact v0.0.0-20241212093903-5caa4621366f h1:h5/HKr
|
|||
github.com/jedisct1/go-hpke-compact v0.0.0-20241212093903-5caa4621366f/go.mod h1:IjVYCPbDciyDZpJpUIYodX+FvctxGmnHVZ/ZwGBCjNA=
|
||||
github.com/jedisct1/go-minisign v0.0.0-20241212093149-d2f9f49435c7 h1:FWpSWRD8FbVkKQu8M1DM9jF5oXFLyE+XpisIYfdzbic=
|
||||
github.com/jedisct1/go-minisign v0.0.0-20241212093149-d2f9f49435c7/go.mod h1:BMxO138bOokdgt4UaxZiEfypcSHX0t6SIFimVP1oRfk=
|
||||
github.com/jedisct1/go-sieve-cache v0.1.0 h1:Na/wqSeVzaUVvlKYvjkyI5mXI+PIRTzizsxKFT7F01c=
|
||||
github.com/jedisct1/go-sieve-cache v0.1.0/go.mod h1:EN88bnjKpiyS9TfZZNbUkCwgpWueZSaUi4vgVtw2988=
|
||||
github.com/jedisct1/go-sieve-cache v0.1.5 h1:ZmkjZc7xvx5CHUw/ePJbRu61KZa6KRyzJlN45x5HBS8=
|
||||
github.com/jedisct1/go-sieve-cache v0.1.5/go.mod h1:EN88bnjKpiyS9TfZZNbUkCwgpWueZSaUi4vgVtw2988=
|
||||
github.com/jedisct1/xsecretbox v0.0.0-20241212092125-3afc4917ac41 h1:TPF+VETyhqUOY51j3KF0uk5cgHQ2Bzi6XCorcGNGfTs=
|
||||
github.com/jedisct1/xsecretbox v0.0.0-20241212092125-3afc4917ac41/go.mod h1:eh2PYNEklsNDqUxnbnN9Duvpw1b+ZectZAtDUDRj2tA=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
|
|
|
|||
4
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/node.go
generated
vendored
4
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/node.go
generated
vendored
|
|
@ -8,8 +8,8 @@ type Node[K comparable, V any] struct {
|
|||
}
|
||||
|
||||
// NewNode creates a new cache node
|
||||
func NewNode[K comparable, V any](key K, value V) *Node[K, V] {
|
||||
return &Node[K, V]{
|
||||
func NewNode[K comparable, V any](key K, value V) Node[K, V] {
|
||||
return Node[K, V]{
|
||||
Key: key,
|
||||
Value: value,
|
||||
Visited: false,
|
||||
|
|
|
|||
29
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sharded.go
generated
vendored
29
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sharded.go
generated
vendored
|
|
@ -222,7 +222,14 @@ func (c *ShardedSieveCache[K, V]) Clear() {
|
|||
|
||||
// Keys returns a slice of all keys in the cache.
|
||||
func (c *ShardedSieveCache[K, V]) Keys() []K {
|
||||
var allKeys []K
|
||||
// First count total keys to allocate proper size
|
||||
totalKeys := 0
|
||||
for _, shard := range c.shards {
|
||||
totalKeys += shard.Len()
|
||||
}
|
||||
|
||||
// Pre-allocate slice with exact capacity
|
||||
allKeys := make([]K, 0, totalKeys)
|
||||
|
||||
// Collect keys from all shards
|
||||
for _, shard := range c.shards {
|
||||
|
|
@ -234,7 +241,14 @@ func (c *ShardedSieveCache[K, V]) Keys() []K {
|
|||
|
||||
// Values returns a slice of all values in the cache.
|
||||
func (c *ShardedSieveCache[K, V]) Values() []V {
|
||||
var allValues []V
|
||||
// First count total values to allocate proper size
|
||||
totalValues := 0
|
||||
for _, shard := range c.shards {
|
||||
totalValues += shard.Len()
|
||||
}
|
||||
|
||||
// Pre-allocate slice with exact capacity
|
||||
allValues := make([]V, 0, totalValues)
|
||||
|
||||
// Collect values from all shards
|
||||
for _, shard := range c.shards {
|
||||
|
|
@ -249,10 +263,17 @@ func (c *ShardedSieveCache[K, V]) Items() []struct {
|
|||
Key K
|
||||
Value V
|
||||
} {
|
||||
var allItems []struct {
|
||||
// First count total items to allocate proper size
|
||||
totalItems := 0
|
||||
for _, shard := range c.shards {
|
||||
totalItems += shard.Len()
|
||||
}
|
||||
|
||||
// Pre-allocate slice with exact capacity
|
||||
allItems := make([]struct {
|
||||
Key K
|
||||
Value V
|
||||
}
|
||||
}, 0, totalItems)
|
||||
|
||||
// Collect items from all shards
|
||||
for _, shard := range c.shards {
|
||||
|
|
|
|||
92
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sievecache.go
generated
vendored
92
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sievecache.go
generated
vendored
|
|
@ -11,9 +11,11 @@ type SieveCache[K comparable, V any] struct {
|
|||
// Map of keys to indices in the nodes slice
|
||||
indices map[K]int
|
||||
// Slice of all cache nodes
|
||||
nodes []*Node[K, V]
|
||||
nodes []Node[K, V]
|
||||
// Index to the "hand" pointer used by the SIEVE algorithm for eviction
|
||||
hand *int
|
||||
hand int
|
||||
// Flag indicating if the hand pointer is initialized
|
||||
handInitialized bool
|
||||
// Maximum number of entries the cache can hold
|
||||
capacity int
|
||||
}
|
||||
|
|
@ -26,10 +28,11 @@ func New[K comparable, V any](capacity int) (*SieveCache[K, V], error) {
|
|||
}
|
||||
|
||||
return &SieveCache[K, V]{
|
||||
indices: make(map[K]int, capacity),
|
||||
nodes: make([]*Node[K, V], 0, capacity),
|
||||
hand: nil,
|
||||
capacity: capacity,
|
||||
indices: make(map[K]int, capacity),
|
||||
nodes: make([]Node[K, V], 0, capacity),
|
||||
hand: 0,
|
||||
handInitialized: false,
|
||||
capacity: capacity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -130,20 +133,17 @@ func (c *SieveCache[K, V]) Remove(key K) (V, bool) {
|
|||
}
|
||||
|
||||
// Update hand if needed
|
||||
if c.hand != nil {
|
||||
handIdx := *c.hand
|
||||
if handIdx == idx {
|
||||
if c.handInitialized {
|
||||
if c.hand == idx {
|
||||
// Move hand to the previous node or wrap to end
|
||||
if idx > 0 {
|
||||
newHand := idx - 1
|
||||
c.hand = &newHand
|
||||
c.hand = idx - 1
|
||||
} else {
|
||||
newHand := len(c.nodes) - 2
|
||||
c.hand = &newHand
|
||||
c.hand = len(c.nodes) - 2
|
||||
}
|
||||
} else if handIdx == len(c.nodes)-1 {
|
||||
} else if c.hand == len(c.nodes)-1 {
|
||||
// If hand points to the last element (which will be moved to idx)
|
||||
c.hand = &idx
|
||||
c.hand = idx
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -171,24 +171,24 @@ func (c *SieveCache[K, V]) Evict() (V, bool) {
|
|||
return zero, false
|
||||
}
|
||||
|
||||
// Start from the hand pointer or the end if no hand
|
||||
// Start from the hand pointer or the end if hand is not initialized
|
||||
var currentIdx int
|
||||
if c.hand != nil {
|
||||
currentIdx = *c.hand
|
||||
if c.handInitialized {
|
||||
currentIdx = c.hand
|
||||
} else {
|
||||
currentIdx = len(c.nodes) - 1
|
||||
}
|
||||
startIdx := currentIdx
|
||||
|
||||
// Track whether we've wrapped around and whether we found a node to evict
|
||||
// Track whether we've wrapped around
|
||||
wrapped := false
|
||||
var foundIdx *int
|
||||
foundIdx := -1
|
||||
|
||||
// Scan for a non-visited entry
|
||||
for {
|
||||
// If current node is not visited, mark it for eviction
|
||||
if !c.nodes[currentIdx].Visited {
|
||||
foundIdx = ¤tIdx
|
||||
foundIdx = currentIdx
|
||||
break
|
||||
}
|
||||
|
||||
|
|
@ -215,19 +215,19 @@ func (c *SieveCache[K, V]) Evict() (V, bool) {
|
|||
}
|
||||
|
||||
// If we found a node to evict
|
||||
if foundIdx != nil {
|
||||
evictIdx := *foundIdx
|
||||
if foundIdx >= 0 {
|
||||
evictIdx := foundIdx
|
||||
|
||||
// Update the hand pointer to the previous node or wrap to end
|
||||
if evictIdx > 0 {
|
||||
newHand := evictIdx - 1
|
||||
c.hand = &newHand
|
||||
c.hand = evictIdx - 1
|
||||
} else if len(c.nodes) > 1 {
|
||||
newHand := len(c.nodes) - 2
|
||||
c.hand = &newHand
|
||||
c.hand = len(c.nodes) - 2
|
||||
} else {
|
||||
c.hand = nil
|
||||
// Keep hand at 0 but mark as not initialized
|
||||
c.handInitialized = false
|
||||
}
|
||||
c.handInitialized = true
|
||||
|
||||
// Remove the key from the map
|
||||
delete(c.indices, c.nodes[evictIdx].Key)
|
||||
|
|
@ -258,12 +258,14 @@ func (c *SieveCache[K, V]) Evict() (V, bool) {
|
|||
// Clear removes all entries from the cache.
|
||||
func (c *SieveCache[K, V]) Clear() {
|
||||
c.indices = make(map[K]int, c.capacity)
|
||||
c.nodes = make([]*Node[K, V], 0, c.capacity)
|
||||
c.hand = nil
|
||||
c.nodes = make([]Node[K, V], 0, c.capacity)
|
||||
c.hand = 0
|
||||
c.handInitialized = false
|
||||
}
|
||||
|
||||
// Keys returns a slice of all keys in the cache.
|
||||
func (c *SieveCache[K, V]) Keys() []K {
|
||||
// Pre-allocate with exact capacity
|
||||
keys := make([]K, len(c.nodes))
|
||||
for i, node := range c.nodes {
|
||||
keys[i] = node.Key
|
||||
|
|
@ -273,6 +275,7 @@ func (c *SieveCache[K, V]) Keys() []K {
|
|||
|
||||
// Values returns a slice of all values in the cache.
|
||||
func (c *SieveCache[K, V]) Values() []V {
|
||||
// Pre-allocate with exact capacity
|
||||
values := make([]V, len(c.nodes))
|
||||
for i, node := range c.nodes {
|
||||
values[i] = node.Value
|
||||
|
|
@ -285,6 +288,7 @@ func (c *SieveCache[K, V]) Items() []struct {
|
|||
Key K
|
||||
Value V
|
||||
} {
|
||||
// Pre-allocate with exact capacity
|
||||
items := make([]struct {
|
||||
Key K
|
||||
Value V
|
||||
|
|
@ -309,8 +313,17 @@ func (c *SieveCache[K, V]) ForEach(f func(k K, v V)) {
|
|||
// Retain only keeps elements specified by the predicate.
|
||||
// Removes all entries for which f returns false.
|
||||
func (c *SieveCache[K, V]) Retain(f func(k K, v V) bool) {
|
||||
// Estimate number of elements to remove - pre-allocate with a reasonable capacity
|
||||
estimatedRemoveCount := len(c.nodes) / 4 // Assume about 25% will be removed
|
||||
if estimatedRemoveCount < 8 {
|
||||
estimatedRemoveCount = 8 // Minimum size for small caches
|
||||
}
|
||||
if estimatedRemoveCount > 1024 {
|
||||
estimatedRemoveCount = 1024 // Cap at reasonable maximum
|
||||
}
|
||||
|
||||
// Collect indices to remove
|
||||
var toRemove []int
|
||||
toRemove := make([]int, 0, estimatedRemoveCount)
|
||||
|
||||
for i, node := range c.nodes {
|
||||
if !f(node.Key, node.Value) {
|
||||
|
|
@ -343,22 +356,19 @@ func (c *SieveCache[K, V]) Retain(f func(k K, v V) bool) {
|
|||
}
|
||||
|
||||
// Update hand if needed
|
||||
if c.hand != nil {
|
||||
handIdx := *c.hand
|
||||
if handIdx == idx {
|
||||
if c.handInitialized {
|
||||
if c.hand == idx {
|
||||
// Hand was pointing to the removed node, move it to previous
|
||||
if idx > 0 {
|
||||
newHand := idx - 1
|
||||
c.hand = &newHand
|
||||
c.hand = idx - 1
|
||||
} else if len(c.nodes) > 0 {
|
||||
newHand := len(c.nodes) - 1
|
||||
c.hand = &newHand
|
||||
c.hand = len(c.nodes) - 1
|
||||
} else {
|
||||
c.hand = nil
|
||||
c.handInitialized = false
|
||||
}
|
||||
} else if handIdx == lastIdx {
|
||||
} else if c.hand == lastIdx {
|
||||
// Hand was pointing to the last node that was moved
|
||||
c.hand = &idx
|
||||
c.hand = idx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sync.go
generated
vendored
28
vendor/github.com/jedisct1/go-sieve-cache/pkg/sievecache/sync.go
generated
vendored
|
|
@ -175,7 +175,8 @@ func (c *SyncSieveCache[K, V]) ForEachValue(f func(*V)) {
|
|||
c.mutex.RUnlock()
|
||||
|
||||
// Process each value without holding the lock
|
||||
updatedItems := make(map[K]V)
|
||||
// Pre-allocate map with the expected size to prevent resizing
|
||||
updatedItems := make(map[K]V, len(items))
|
||||
for _, item := range items {
|
||||
valueCopy := item.Value
|
||||
f(&valueCopy)
|
||||
|
|
@ -201,7 +202,8 @@ func (c *SyncSieveCache[K, V]) ForEachEntry(f func(K, *V)) {
|
|||
c.mutex.RUnlock()
|
||||
|
||||
// Process each entry without holding the lock
|
||||
updatedItems := make(map[K]V)
|
||||
// Pre-allocate map with the expected size to prevent resizing
|
||||
updatedItems := make(map[K]V, len(items))
|
||||
for _, item := range items {
|
||||
valueCopy := item.Value
|
||||
f(item.Key, &valueCopy)
|
||||
|
|
@ -234,8 +236,17 @@ func (c *SyncSieveCache[K, V]) Retain(f func(K, V) bool) {
|
|||
items := c.cache.Items()
|
||||
c.mutex.RUnlock()
|
||||
|
||||
// Estimate number of elements to remove - pre-allocate with a reasonable capacity
|
||||
estimatedRemoveCount := len(items) / 4 // Assume about 25% will be removed
|
||||
if estimatedRemoveCount < 8 {
|
||||
estimatedRemoveCount = 8 // Minimum size for small caches
|
||||
}
|
||||
if estimatedRemoveCount > 1024 {
|
||||
estimatedRemoveCount = 1024 // Cap at reasonable maximum
|
||||
}
|
||||
|
||||
// Check each entry against the predicate without holding the lock
|
||||
var keysToRemove []K
|
||||
keysToRemove := make([]K, 0, estimatedRemoveCount)
|
||||
for _, item := range items {
|
||||
if !f(item.Key, item.Value) {
|
||||
keysToRemove = append(keysToRemove, item.Key)
|
||||
|
|
@ -258,8 +269,17 @@ func (c *SyncSieveCache[K, V]) RetainBatch(f func(K, V) bool) {
|
|||
items := c.cache.Items()
|
||||
c.mutex.RUnlock()
|
||||
|
||||
// Estimate number of elements to remove - pre-allocate with a reasonable capacity
|
||||
estimatedRemoveCount := len(items) / 4 // Assume about 25% will be removed
|
||||
if estimatedRemoveCount < 8 {
|
||||
estimatedRemoveCount = 8 // Minimum size for small caches
|
||||
}
|
||||
if estimatedRemoveCount > 1024 {
|
||||
estimatedRemoveCount = 1024 // Cap at reasonable maximum
|
||||
}
|
||||
|
||||
// Collect keys to remove without holding the lock
|
||||
var keysToRemove []K
|
||||
keysToRemove := make([]K, 0, estimatedRemoveCount)
|
||||
for _, item := range items {
|
||||
if !f(item.Key, item.Value) {
|
||||
keysToRemove = append(keysToRemove, item.Key)
|
||||
|
|
|
|||
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
|
|
@ -63,7 +63,7 @@ github.com/jedisct1/go-hpke-compact
|
|||
# github.com/jedisct1/go-minisign v0.0.0-20241212093149-d2f9f49435c7
|
||||
## explicit; go 1.23.4
|
||||
github.com/jedisct1/go-minisign
|
||||
# github.com/jedisct1/go-sieve-cache v0.1.0
|
||||
# github.com/jedisct1/go-sieve-cache v0.1.5
|
||||
## explicit; go 1.21
|
||||
github.com/jedisct1/go-sieve-cache/pkg/sievecache
|
||||
# github.com/jedisct1/xsecretbox v0.0.0-20241212092125-3afc4917ac41
|
||||
|
|
|
|||
Loading…
Reference in a new issue