forked from hemilabs/heminetwork
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tbcd: revisit block header cache (hemilabs#282)
* Use chainhash type for LRU cache * Replace blockheader cache with a simple map * Add a specific blockheader cache type * use env to override * Try to save some memory b y having less pointer laying around * Use even less pointers * 3.5M items is stupid; testnet3 is stupid
- Loading branch information
1 parent
3e55152
commit ebeb8bd
Showing
6 changed files
with
129 additions
and
75 deletions.
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
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,55 @@ | ||
// Copyright (c) 2024 Hemi Labs, Inc. | ||
// Use of this source code is governed by the MIT License, | ||
// which can be found in the LICENSE file. | ||
|
||
package level | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
|
||
"github.com/hemilabs/heminetwork/database/tbcd" | ||
) | ||
|
||
type lowIQMap struct { | ||
mtx sync.RWMutex | ||
|
||
count int | ||
|
||
m map[chainhash.Hash]*tbcd.BlockHeader | ||
} | ||
|
||
func (l *lowIQMap) Put(v *tbcd.BlockHeader) { | ||
l.mtx.Lock() | ||
defer l.mtx.Unlock() | ||
|
||
if _, ok := l.m[v.Hash]; ok { | ||
return | ||
} | ||
|
||
if len(l.m) >= l.count { | ||
// evict entry | ||
for k := range l.m { | ||
delete(l.m, k) | ||
break | ||
} | ||
} | ||
|
||
l.m[v.Hash] = v | ||
} | ||
|
||
func (l *lowIQMap) Get(k *chainhash.Hash) (*tbcd.BlockHeader, bool) { | ||
l.mtx.RLock() | ||
defer l.mtx.RUnlock() | ||
|
||
bh, ok := l.m[*k] | ||
return bh, ok | ||
} | ||
|
||
func lowIQMapNew(count int) *lowIQMap { | ||
return &lowIQMap{ | ||
count: count, | ||
m: make(map[chainhash.Hash]*tbcd.BlockHeader, count), | ||
} | ||
} |
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
Oops, something went wrong.