Skip to content

starknet_patricia_storage: atomic counters for read stats#12122

Open
nimrod-starkware wants to merge 1 commit intomain-v0.14.2from
nimrod/parallel-reads/remove-reads-from-cached-storage
Open

starknet_patricia_storage: atomic counters for read stats#12122
nimrod-starkware wants to merge 1 commit intomain-v0.14.2from
nimrod/parallel-reads/remove-reads-from-cached-storage

Conversation

@nimrod-starkware
Copy link
Contributor

@nimrod-starkware nimrod-starkware commented Jan 29, 2026

Note

Low Risk
Small, localized change affecting only in-memory stats counters; no persistence, storage semantics, or key/value handling logic is modified.

Overview
Switches CachedStorage read statistics (reads, cached_reads) from u128 fields to AtomicU64 counters, updating get/mget to use atomic fetch_add.

Adjusts stats reporting/reset to load/store with explicit memory ordering, and removes the now-obsolete total_reads/total_cached_reads accessors (writes stats remain non-atomic).

Written by Cursor Bugbot for commit ff021c7. This will update automatically on new commits. Configure here.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

nimrod-starkware commented Jan 29, 2026

@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/cache-size-u64 branch from 8db4cfd to f97e43f Compare January 29, 2026 08:58
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from 41cc5f2 to 3ebf9db Compare January 29, 2026 08:58
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/cache-size-u64 branch from f97e43f to b32f6a9 Compare February 1, 2026 09:24
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch 2 times, most recently from 7091357 to d58cd4a Compare February 1, 2026 09:29
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/cache-size-u64 branch from b32f6a9 to 37bda93 Compare February 1, 2026 09:29
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from d58cd4a to f899e10 Compare February 1, 2026 11:19
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/cache-size-u64 branch from 37bda93 to a3f7269 Compare February 1, 2026 11:19
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from f899e10 to df1d000 Compare February 2, 2026 08:36
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/cache-size-u64 branch from a3b6813 to d27181a Compare February 3, 2026 13:08
@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from df1d000 to 9d30e2b Compare February 3, 2026 13:08
Copy link
Contributor Author

@nimrod-starkware nimrod-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nimrod-starkware made 1 comment and resolved 1 discussion.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @ArielElp).


crates/starknet_patricia_storage/src/map_storage.rs line 79 at r1 (raw file):

Previously, ArielElp wrote…

Change to:

use std::sync::atomic::{AtomicU64, Ordering};

And update via

        if found {
            self.hits.fetch_add(1, Ordering::Relaxed);
            Some(value)
        } else {
            self.misses.fetch_add(1, Ordering::Relaxed);
            None
        }

Ordering::Relaxed guarantees eventual correctness AFAIU, so shouldn't be significant compare to regular addition (Cursor says 1 vs 10-20 cycles for addition)

Done.

Copy link
Contributor

@ArielElp ArielElp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArielElp reviewed 1 file and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @nimrod-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 310 at r3 (raw file):

    fn reset_stats(&mut self) -> PatriciaStorageResult<()> {
        self.reads.store(0, Ordering::Relaxed);

Non blocking, consider something stricter here to make sure stats are reset

@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from 4a9c8a2 to fd5dea5 Compare February 10, 2026 12:34
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

@nimrod-starkware nimrod-starkware changed the title starknet_patricia_storage: dont keep track of reads for stats starknet_patricia_storage: atomic counters for read stats Feb 10, 2026
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorimedini-starkware reviewed 1 file and all commit messages, and made 3 comments.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @nimrod-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 232 at r3 (raw file):

    async fn get(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
        if let Some(cached_value) = self.cache.get(key) {
            self.cached_reads.fetch_add(1, Ordering::Relaxed);

what is Ordering?
didn't really deep-dive into the docs, and corrupting this value is not so bad (it's just for statistics), but I would still like to understand why Relaxed is the choice here

Code quote:

Ordering::Relaxed

crates/starknet_patricia_storage/src/map_storage.rs line 236 at r3 (raw file):

        }

        self.reads.fetch_add(1, Ordering::Relaxed);

why did this move down? now you only count a read if it is not cached.
if this is on purpose, you need to do what the bugbot says (change how cache hit rate is computed)

Code quote:

self.reads.fetch_add(1, Ordering::Relaxed);

crates/starknet_patricia_storage/src/map_storage.rs line 265 at r3 (raw file):

        }

        let n_keys = u64::try_from(keys.len()).expect("keys length should fit in u64");

why count in every iteration?

Suggestion:

        for (index, key) in keys.iter().enumerate() {
            if let Some(cached_value) = self.cache.get(key) {
                values[index] = cached_value.clone();
            } else {
                keys_to_fetch.push(*key);
                indices_to_fetch.push(index);
            }
        }

        let n_keys = u64::try_from(keys.len()).expect("keys length should fit in u64");
        let cached_reads = u64::try_from(keys.len() - keys_to_fetch.len()).expect("usize should fit in u64");

Copy link
Contributor Author

@nimrod-starkware nimrod-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nimrod-starkware made 4 comments and resolved 2 discussions.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on dorimedini-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 232 at r3 (raw file):

Previously, dorimedini-starkware wrote…

what is Ordering?
didn't really deep-dive into the docs, and corrupting this value is not so bad (it's just for statistics), but I would still like to understand why Relaxed is the choice here

IIUC it means that the increment will happen eventually, not neccesarily right away.
There is a trade off between how quickly you want the operation to be done and the accuracy of the counter value at any point.
This is the most relaxed version - might make stats inaccurate but not block for a long time when calling fetch_add().


crates/starknet_patricia_storage/src/map_storage.rs line 236 at r3 (raw file):

Previously, dorimedini-starkware wrote…

why did this move down? now you only count a read if it is not cached.
if this is on purpose, you need to do what the bugbot says (change how cache hit rate is computed)

Done.


crates/starknet_patricia_storage/src/map_storage.rs line 265 at r3 (raw file):

Previously, dorimedini-starkware wrote…

why count in every iteration?

Done.


crates/starknet_patricia_storage/src/map_storage.rs line 310 at r3 (raw file):

Previously, ArielElp wrote…

Non blocking, consider something stricter here to make sure stats are reset

Done

@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from fd5dea5 to 28741bf Compare February 17, 2026 07:13
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorimedini-starkware reviewed 1 file and all commit messages, made 1 comment, and resolved 3 discussions.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @nimrod-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

    fn get_stats(&self) -> PatriciaStorageResult<Self::Stats> {
        let reads = u128::from(self.reads.load(Ordering::Acquire));
        let cached_reads = u128::from(self.cached_reads.load(Ordering::Acquire));

why are these now stricter?
does the fact that you can call get_stats several times without calling reset_stats once matter in this Acquire-Release pattern?

Code quote:

        let reads = u128::from(self.reads.load(Ordering::Acquire));
        let cached_reads = u128::from(self.cached_reads.load(Ordering::Acquire));

Copy link
Contributor Author

@nimrod-starkware nimrod-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nimrod-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on dorimedini-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

Previously, dorimedini-starkware wrote…

why are these now stricter?
does the fact that you can call get_stats several times without calling reset_stats once matter in this Acquire-Release pattern?

They are stricter to provide accurate stats whenever get_stats is called.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorimedini-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @nimrod-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

Previously, nimrod-starkware wrote…

They are stricter to provide accurate stats whenever get_stats is called.

does the fact that you can call get_stats several times without calling reset_stats once matter in this Acquire-Release pattern?

Copy link
Contributor Author

@nimrod-starkware nimrod-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nimrod-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on dorimedini-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

Previously, dorimedini-starkware wrote…

does the fact that you can call get_stats several times without calling reset_stats once matter in this Acquire-Release pattern?

it should be ok, verified with cursor

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorimedini-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on nimrod-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

Previously, nimrod-starkware wrote…

it should be ok, verified with cursor

did cursor point you to docs...? not that docs can't lie, but cursor can lie for sure

@nimrod-starkware nimrod-starkware force-pushed the nimrod/parallel-reads/remove-reads-from-cached-storage branch from 28741bf to ff021c7 Compare February 19, 2026 09:21
Copy link
Contributor Author

@nimrod-starkware nimrod-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nimrod-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on dorimedini-starkware).


crates/starknet_patricia_storage/src/map_storage.rs line 296 at r4 (raw file):

Previously, dorimedini-starkware wrote…

did cursor point you to docs...? not that docs can't lie, but cursor can lie for sure

Went over this and this. It should be fine.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dorimedini-starkware resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on nimrod-starkware).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments