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

chore(net): get and peek for LruCache #8508

Merged
merged 7 commits into from
Jun 5, 2024
Merged

chore(net): get and peek for LruCache #8508

merged 7 commits into from
Jun 5, 2024

Conversation

emhane
Copy link
Member

@emhane emhane commented May 30, 2024

Adds methods get and peek to LruCache, which are needed for #6559

@emhane emhane added C-debt Refactor of code section that is hard to understand or maintain A-networking Related to networking in general labels May 30, 2024
crates/net/network/src/cache.rs Show resolved Hide resolved
crates/net/network/src/cache.rs Show resolved Hide resolved
crates/net/network/src/cache.rs Outdated Show resolved Hide resolved
crates/net/network/src/cache.rs Outdated Show resolved Hide resolved
crates/net/network/src/cache.rs Outdated Show resolved Hide resolved
Comment on lines +53 to +54
let _ = self.inner.get(entry)?;
self.iter().next()
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is the same as:

Suggested change
let _ = self.inner.get(entry)?;
self.iter().next()
let _ = self.inner.get(entry)?;
Some(entry)

?

Copy link
Member Author

Choose a reason for hiding this comment

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

not if the types have a custom PartialEq impl, consider type

struct MyType {
    id: i32,
    colour: i32,
}

impl PartialEq for MyType {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Hash ..

then we can have two instances with different colour, that will both have the same key

let green = MyType { id: 1, colour: 32 };
let blue = MyType { id: 1, colour: 34 };

let mut cache = LruCache::new();
cache.insert(green);
let _ = cache.get(&blue); // this will exist, but it will actually be green that is in the map

this extension to LruCache just makes it safe to use in the codebase kind of, cause we have a couple of types that impl custom PartialEq on a subset of fields. using a HashMap for these types makes way more sense though.

@emhane emhane added this pull request to the merge queue Jun 5, 2024
Merged via the queue into main with commit 1e0d724 Jun 5, 2024
30 checks passed
@emhane emhane deleted the emhane/lrucache branch June 5, 2024 00:52
mw2000 pushed a commit to mw2000/reth that referenced this pull request Jun 5, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-networking Related to networking in general C-debt Refactor of code section that is hard to understand or maintain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants