Skip to content
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

Research performance cost of duplicate reads and implement a solution if necessary #1081

Open
maltekliemann opened this issue Aug 21, 2023 · 0 comments
Assignees
Labels
l:M Solving the issue takes days p:medium Medium priority, this issue can wait but should be done fairly soon t:enhancement The issue described an enhancement

Comments

@maltekliemann
Copy link
Contributor

Storage overlay prevents the worst effects of double reads, but the official suggestion is still to avoid excessive use of this feature: https://substrate.stackexchange.com/questions/2574/clarity-on-storage-read-write-caching.

My suggestion is to use the following design:

type DbReader<K, V> = dyn Fn(K) -> Result<V, Box<dyn std::error::Error>> + Send + Sync;

struct Lazy<K, V> {
    key: K,
    object: OnceCell<V>,
    db_reader: Box<DbReader<K, V>>,
}

impl<K: Copy, V> Lazy<K, V> {
    fn new(key: K, db_reader: Box<DbReader<K, V>>) -> Self {
        Lazy {
            key,
            object: OnceCell::new(),
            db_reader,
        }
    }

    fn get(&self) -> Result<&V, Box<dyn std::error::Error>> {
        self.object.get_or_try_init(|| (self.db_reader)(self.key))
    }
}

You can even implement Into<Lazy<MarketId, Market>> for MarketId. All helper and do_* functions should take a wrapped Lazy object.

@maltekliemann maltekliemann self-assigned this Aug 21, 2023
@sea212 sea212 added p:medium Medium priority, this issue can wait but should be done fairly soon t:enhancement The issue described an enhancement l:S Solving this issue takes hours labels Aug 31, 2023
@maltekliemann maltekliemann added l:M Solving the issue takes days and removed l:S Solving this issue takes hours labels May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
l:M Solving the issue takes days p:medium Medium priority, this issue can wait but should be done fairly soon t:enhancement The issue described an enhancement
Projects
None yet
Development

No branches or pull requests

2 participants