Skip to content

Comments

Refactor MultiIndexMap#399

Closed
PoulavBhowmick03 wants to merge 2 commits intosigp:unstablefrom
PoulavBhowmick03:refactor/MultiIndexMap
Closed

Refactor MultiIndexMap#399
PoulavBhowmick03 wants to merge 2 commits intosigp:unstablefrom
PoulavBhowmick03:refactor/MultiIndexMap

Conversation

@PoulavBhowmick03
Copy link

Issue Addressed

Fixes #253

@PoulavBhowmick03 PoulavBhowmick03 force-pushed the refactor/MultiIndexMap branch from f62c47b to a9a9661 Compare June 28, 2025 06:41
@cla-assistant
Copy link

cla-assistant bot commented Jun 28, 2025

CLA assistant check
All committers have signed the CLA.

@cla-assistant
Copy link

cla-assistant bot commented Jun 28, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@PoulavBhowmick03 PoulavBhowmick03 force-pushed the refactor/MultiIndexMap branch from a9a9661 to 8b70a1a Compare June 28, 2025 06:43
@diegomrsantos diegomrsantos requested a review from Copilot July 1, 2025 11:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the MultiIndexMap implementation and its usages to improve type safety and unify index access patterns.

  • Swapped out the old UniqueIndex/NonUniqueIndex traits for the new UniqueIndexAccess/NonUniqueIndexAccess variants.
  • Changed MultiIndexMap to use IndexDescriptor types (UniqueIndex<K>/NonUniqueIndex<K>) with Arc-wrapped primary keys.
  • Updated imports and call sites across modules (validator_store, subnet_tracker, message_receiver, eth, database) to align with the new API.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
anchor/validator_store/src/lib.rs Updated database trait imports to *Access variants
anchor/subnet_tracker/src/lib.rs Same import adjustments
anchor/message_receiver/src/manager.rs Same import adjustments
anchor/eth/src/index_sync.rs Updated UniqueIndexAccess import
anchor/eth/src/event_processor.rs Same import adjustments
anchor/database/src/validator_operations.rs Swapped to UniqueIndexAccess/NonUniqueIndexAccess
anchor/database/src/tests/utils.rs Added missing UniqueIndexAccess import in tests
anchor/database/src/tests/state_tests.rs Added missing UniqueIndexAccess import in tests
anchor/database/src/tests/mod.rs Removed UniqueIndex re-export from test prelude
anchor/database/src/state.rs Adjusted NetworkState insertion calls to owned values
anchor/database/src/multi_index.rs Overhauled MultiIndexMap internals and API
anchor/database/src/lib.rs Updated pub use to export new index types
anchor/database/src/cluster_operations.rs Updated calls to MultiIndexMap::insert with new API
Comments suppressed due to low confidence (1)

anchor/database/src/tests/mod.rs:13

  • The test prelude no longer re-exports multi_index::UniqueIndex, causing tests that reference UniqueIndex to fail to compile. Consider re-adding multi_index::UniqueIndex to the prelude or importing it explicitly in the tests.
    pub use crate::NetworkDatabase;


/// Marker trait for uniquely identifying indices
#[allow(dead_code)]
pub trait Unique {}
Copy link

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

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

[nitpick] The marker traits Unique and NotUnique are no longer used after this refactoring. You can remove them to reduce dead code and simplify the module.

Copilot uses AI. Check for mistakes.
.primary
.keys()
.find(|key| key.as_ref() == k1)
.cloned()?;
Copy link

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

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

This linear search over self.primary.keys() makes update O(n). Consider maintaining a direct map from K1 to its Arc<K1> to allow O(1) lookups.

Copilot uses AI. Check for mistakes.
Comment on lines 156 to 184
if std::any::TypeId::of::<I2::Uniqueness>() == std::any::TypeId::of::<UniqueTag>() {
self.secondary_unique.insert(k2, primary_key.clone());
} else {
self.maps
.secondary_multi
.entry(k2.clone())
.and_modify(|vec| vec.push(k1.clone()))
.or_insert_with(|| vec![k1.clone()]);
self.secondary_multi
.entry(k2)
.or_insert(Vec::new())
.push(primary_key.clone());
}

// Handle tertiary index based on uniqueness
if std::any::TypeId::of::<U2>() == std::any::TypeId::of::<UniqueTag>() {
self.maps.tertiary_unique.insert(k3.clone(), k1.clone());
if std::any::TypeId::of::<I3::Uniqueness>() == std::any::TypeId::of::<UniqueTag>() {
self.tertiary_unique.insert(k3.clone(), primary_key.clone());
} else {
self.maps
.tertiary_multi
self.tertiary_multi
.entry(k3.clone())
.and_modify(|vec| vec.push(k1.clone()))
.or_insert_with(|| vec![k1.clone()]);
.and_modify(|vec| vec.push(primary_key.clone()))
.or_insert_with(|| vec![primary_key.clone()]);
}

// Handle quaternary index based on uniqueness
if std::any::TypeId::of::<U3>() == std::any::TypeId::of::<UniqueTag>() {
self.maps.quaternary_unique.insert(k4.clone(), k1.clone());
if std::any::TypeId::of::<I4::Uniqueness>() == std::any::TypeId::of::<UniqueTag>() {
self.quaternary_unique
.insert(k4.clone(), primary_key.clone());
} else {
self.maps
.quaternary_multi
self.quaternary_multi
.entry(k4.clone())
.and_modify(|vec| vec.push(k1.clone()))
.or_insert_with(|| vec![k1.clone()]);
.and_modify(|vec| vec.push(primary_key.clone()))
.or_insert_with(|| vec![primary_key.clone()]);
}
Copy link

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

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

[nitpick] Using TypeId comparisons for compile-time dispatch can be brittle and may not optimize away. Consider using trait specialization or const generics to branch on index uniqueness at compile time.

Copilot uses AI. Check for mistakes.
.or_insert_with(|| vec![k1.clone()]);
self.secondary_multi
.entry(k2)
.or_insert(Vec::new())
Copy link

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

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

[nitpick] You can simplify or_insert(Vec::new()) to or_default() for a more concise and idiomatic entry API usage.

Suggested change
.or_insert(Vec::new())
.or_default()

Copilot uses AI. Check for mistakes.
@PoulavBhowmick03 PoulavBhowmick03 force-pushed the refactor/MultiIndexMap branch from 8b70a1a to 2c555b3 Compare July 1, 2025 17:39
@PoulavBhowmick03
Copy link
Author

@diegomrsantos i have fixed the tests, can you check again

@PoulavBhowmick03 PoulavBhowmick03 force-pushed the refactor/MultiIndexMap branch from 2c555b3 to d195b92 Compare July 5, 2025 17:50
@dknopik
Copy link
Member

dknopik commented Jul 14, 2025

Hi @PoulavBhowmick03! Thank you for your PR, this is a good start.

Please take a look at the Copilot suggestions above. Also, while I like the reduction of generic parameters, it would be also great if we find a way to avoid the redundant impl (Non)UniqueIndexAccess for ....

@dknopik
Copy link
Member

dknopik commented Aug 5, 2025

With #463, we decided to go another way, but thank you for the PR anyway!

@dknopik dknopik closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants