Skip to content

DL Config cache size. #19476

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

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chia/data_layer/data_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ async def manage(self) -> AsyncIterator[None]:
if self.config.get("log_sqlite_cmds", False):
sql_log_path = path_from_root(self.root_path, "log/data_sql.log")
self.log.info(f"logging SQL commands to {sql_log_path}")
cache_capacity = self.config.get("merkle_blobs_cache_size", 1)

async with DataStore.managed(database=self.db_path, sql_log_path=sql_log_path) as self._data_store:
async with DataStore.managed(
database=self.db_path, sql_log_path=sql_log_path, cache_capacity=cache_capacity
) as self._data_store:
self._wallet_rpc = await self.wallet_rpc_init

await self._data_store.migrate_db(self.server_files_location)
Expand Down
12 changes: 10 additions & 2 deletions chia/data_layer/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class DataStore:
@classmethod
@contextlib.asynccontextmanager
async def managed(
cls, database: Union[str, Path], uri: bool = False, sql_log_path: Optional[Path] = None
cls,
database: Union[str, Path],
uri: bool = False,
sql_log_path: Optional[Path] = None,
cache_capacity: int = 1,
) -> AsyncIterator[DataStore]:
async with DBWrapper2.managed(
database=database,
Expand All @@ -85,7 +89,7 @@ async def managed(
row_factory=aiosqlite.Row,
log_path=sql_log_path,
) as db_wrapper:
recent_merkle_blobs: LRUCache[bytes32, MerkleBlob] = LRUCache(capacity=128)
recent_merkle_blobs: LRUCache[bytes32, MerkleBlob] = LRUCache(capacity=cache_capacity)
self = cls(db_wrapper=db_wrapper, recent_merkle_blobs=recent_merkle_blobs)

async with db_wrapper.writer() as writer:
Expand Down Expand Up @@ -436,6 +440,8 @@ async def get_merkle_blob(
) -> MerkleBlob:
if root_hash is None:
return MerkleBlob(blob=b"")
if self.recent_merkle_blobs.get_capacity() == 0:
update_cache = False

existing_blob = self.recent_merkle_blobs.get(root_hash)
if existing_blob is not None:
Expand Down Expand Up @@ -471,6 +477,8 @@ async def insert_root_from_merkle_blob(
) -> Root:
if not merkle_blob.empty():
merkle_blob.calculate_lazy_hashes()
if self.recent_merkle_blobs.get_capacity() == 0:
update_cache = False

root_hash = merkle_blob.get_root_hash()
if old_root is not None and old_root.node_hash == root_hash:
Expand Down
3 changes: 3 additions & 0 deletions chia/util/initial-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ data_layer:
# Enable to store all .DAT files grouped by store id
group_files_by_store: False

# Increasing this number may help sync old clients faster, at the expense of using more RAM memory
merkle_blobs_cache_size: 1

simulator:
# Should the simulator farm a block whenever a transaction is in mempool
auto_farm: True
Expand Down
12 changes: 8 additions & 4 deletions chia/util/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ def get(self, key: K) -> Optional[V]:
return self.cache[key]

def put(self, key: K, value: V) -> None:
self.cache[key] = value
self.cache.move_to_end(key)
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
if self.capacity > 0:
self.cache[key] = value
self.cache.move_to_end(key)
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)

def remove(self, key: K) -> None:
self.cache.pop(key)

def get_capacity(self) -> int:
return self.capacity
Loading