metabase: Do not allocate map in cache unless needed
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
2184d996c9
commit
3d4b5c3e52
1 changed files with 10 additions and 9 deletions
|
@ -14,10 +14,7 @@ type bucketCache struct {
|
|||
}
|
||||
|
||||
func newBucketCache() *bucketCache {
|
||||
return &bucketCache{
|
||||
expired: make(map[cid.ID]*bbolt.Bucket),
|
||||
primary: make(map[cid.ID]*bbolt.Bucket),
|
||||
}
|
||||
return &bucketCache{}
|
||||
}
|
||||
|
||||
func getLockedBucket(bc *bucketCache, tx *bbolt.Tx) *bbolt.Bucket {
|
||||
|
@ -56,7 +53,7 @@ func getExpiredBucket(bc *bucketCache, tx *bbolt.Tx, cnr cid.ID) *bbolt.Bucket {
|
|||
bucketName = objectToExpirationEpochBucketName(cnr, bucketName)
|
||||
return tx.Bucket(bucketName)
|
||||
}
|
||||
return getMappedBucket(bc.expired, tx, objectToExpirationEpochBucketName, cnr)
|
||||
return getMappedBucket(&bc.expired, tx, objectToExpirationEpochBucketName, cnr)
|
||||
}
|
||||
|
||||
func getPrimaryBucket(bc *bucketCache, tx *bbolt.Tx, cnr cid.ID) *bbolt.Bucket {
|
||||
|
@ -65,17 +62,21 @@ func getPrimaryBucket(bc *bucketCache, tx *bbolt.Tx, cnr cid.ID) *bbolt.Bucket {
|
|||
bucketName = primaryBucketName(cnr, bucketName)
|
||||
return tx.Bucket(bucketName)
|
||||
}
|
||||
return getMappedBucket(bc.primary, tx, primaryBucketName, cnr)
|
||||
return getMappedBucket(&bc.primary, tx, primaryBucketName, cnr)
|
||||
}
|
||||
|
||||
func getMappedBucket(m map[cid.ID]*bbolt.Bucket, tx *bbolt.Tx, nameFunc func(cid.ID, []byte) []byte, cnr cid.ID) *bbolt.Bucket {
|
||||
value, ok := m[cnr]
|
||||
func getMappedBucket(m *map[cid.ID]*bbolt.Bucket, tx *bbolt.Tx, nameFunc func(cid.ID, []byte) []byte, cnr cid.ID) *bbolt.Bucket {
|
||||
value, ok := (*m)[cnr]
|
||||
if ok {
|
||||
return value
|
||||
}
|
||||
|
||||
if *m == nil {
|
||||
*m = make(map[cid.ID]*bbolt.Bucket, 1)
|
||||
}
|
||||
|
||||
bucketName := make([]byte, bucketKeySize)
|
||||
bucketName = nameFunc(cnr, bucketName)
|
||||
m[cnr] = getBucket(&value, tx, bucketName)
|
||||
(*m)[cnr] = getBucket(&value, tx, bucketName)
|
||||
return value
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue