Skip to content

Commit

Permalink
cache tweaks (#2098)
Browse files Browse the repository at this point in the history
* cache tweaks

* cleanup
  • Loading branch information
tudor-malene authored Oct 21, 2024
1 parent ba9c10b commit 0b0cc5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions go/common/gethencoding/geth_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func (enc *gethEncodingServiceImpl) CreateEthHeaderForBatch(ctx context.Context,
ExcessBlobGas: nil,
Bloom: types.Bloom{},
}
enc.cachingService.CacheConvertedHash(ctx, h.Hash(), gethHeader.Hash())
return &gethHeader, nil
})
}
Expand Down
43 changes: 23 additions & 20 deletions go/enclave/storage/cache_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,14 @@ type CacheService struct {
}

func NewCacheService(logger gethlog.Logger) *CacheService {
// todo (tudor) figure out the config
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 100_000_000, // 10 times the expected elements
MaxCost: 1024 * 1024 * 1024, // allocate 1GB
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
logger.Crit("Could not initialise ristretto cache", log.ErrKey, err)
}
ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
// the general cache for 50Mil elements, - 2GB
// todo - consider making it fine grained per cache
ristrettoStore := newCache(logger, 50_000_000, 2*1024*1024*1024)

// cache the latest received batches to avoid a lookup when streaming it back to the host after processing
nrBatches := int64(50)
ristrettoStoreForBatches := newCache(logger, nrBatches, nrBatches*batchCost)

nrBatches := 5
ristrettoCacheForBatches, err := ristretto.NewCache(&ristretto.Config{
NumCounters: int64(10 * nrBatches), // 10 times the expected elements
MaxCost: int64(nrBatches * batchCost), // allocate 5MB
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
logger.Crit("Could not initialise ristretto cache", log.ErrKey, err)
}
ristrettoStoreForBatches := ristretto_store.NewRistretto(ristrettoCacheForBatches)
return &CacheService{
blockCache: cache.New[*types.Header](ristrettoStore),
batchCacheBySeqNo: cache.New[*common.BatchHeader](ristrettoStore),
Expand All @@ -102,6 +89,22 @@ func NewCacheService(logger gethlog.Logger) *CacheService {
}
}

func newCache(logger gethlog.Logger, nrElem, capacity int64) *ristretto_store.RistrettoStore {
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10 * nrElem, // 10 times the expected elements
MaxCost: capacity,
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
logger.Crit("Could not initialise ristretto cache", log.ErrKey, err)
}
return ristretto_store.NewRistretto(ristrettoCache)
}

func (cs *CacheService) CacheConvertedHash(ctx context.Context, batchHash, convertedHash gethcommon.Hash) {
cacheValue(ctx, cs.convertedHashCache, cs.logger, batchHash, &convertedHash, hashCost)
}

func (cs *CacheService) CacheBlock(ctx context.Context, b *types.Header) {
cacheValue(ctx, cs.blockCache, cs.logger, b.Hash(), b, blockHeaderCost)
}
Expand Down

0 comments on commit 0b0cc5e

Please sign in to comment.