Skip to content

Use dashmap in a couple of low hanging places #112

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AdamGS
Copy link

@AdamGS AdamGS commented Apr 21, 2025

IDK if that's what you had in mind in #85. I think another two easy followups are:

  1. Create specialized iterator type over dashmap to make sure users of caches can't mutate entries while iterating the content.
  2. Switching the hashing function from the default SipHash to something faster but less DOS resistant like foldhash.

guard.queries.shrink_to_fit();
guard.stats.hits = 0;
guard.stats.misses = 0;
// This is the only place we acquire the write lock, in order to synchronize clearing the cache
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need an RwLock at all? Both stats and queries are synchronized, we can just clear the DashMap and reset the counters to zero. DashMap has its own RwLock, so we're double-locking for each access to the cache.

Copy link
Author

Choose a reason for hiding this comment

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

probably just out of an abundance of caution, making this operation atomic seemed nice but I guess its just fine if the stats are a few cycles behind the map.

@levkk
Copy link
Collaborator

levkk commented Apr 21, 2025

Thank you for the PR. I'm going to benchmark it to see if there is a significant difference between the two approaches. The query parser gets hit only in multi-shard configuration and when the client is using prepared statements (or the extended protocol)*. The peers/discovery module isn't used that heavily (yet). I wrote it before realizing most public clouds disable multicast on their network.

There is another place where DashMap could be used:

clients: Mutex<HashMap<BackendKeyData, ConnectedClient>>,

This place is actually pretty hot: it's touched on every transaction/query and every time a client connects/disconnects.

*Correction: and when the config contains a primary and replicas. It's used for read/write traffic separation.

Comment on lines -70 to -72
/// Number of connected clients.
pub fn clients_len(&self) -> usize {
self.global.clients.lock().len()
Copy link
Author

Choose a reason for hiding this comment

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

Seems like this function and len served the same purpose?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, yeah. Good catch.

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.

2 participants