Skip to content

Commit

Permalink
fix(nns): Fix ballot generation inefficiency (for stable neurons) (#2526
Browse files Browse the repository at this point in the history
)

This fixes an inefficiency in how ballots are generated when neurons are
in stable memory.
  • Loading branch information
max-dfinity authored Nov 8, 2024
1 parent 0a3ab41 commit 6d94310
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions rs/nns/governance/src/neuron_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,22 @@ impl NeuronStore {
pub fn with_active_neurons_iter<R>(
&self,
callback: impl for<'b> FnOnce(Box<dyn Iterator<Item = Neuron> + 'b>) -> R,
) -> R {
self.with_active_neurons_iter_sections(callback, NeuronSections::all())
}

fn with_active_neurons_iter_sections<R>(
&self,
callback: impl for<'b> FnOnce(Box<dyn Iterator<Item = Neuron> + 'b>) -> R,
sections: NeuronSections,
) -> R {
if self.use_stable_memory_for_all_neurons {
// Note, during migration, we still need heap_neurons, so we chain them onto the iterator
with_stable_neuron_store(|stable_store| {
let now = self.now();
let iter = Box::new(
stable_store
.range_neurons(..)
.range_neurons_sections(.., sections)
.filter(|n| !n.is_inactive(now))
.chain(self.heap_neurons.values().cloned()),
) as Box<dyn Iterator<Item = Neuron>>;
Expand Down Expand Up @@ -871,11 +879,14 @@ impl NeuronStore {
};

// Active neurons iterator already makes distinctions between stable and heap neurons.
self.with_active_neurons_iter(|iter| {
for neuron in iter {
process_neuron(neuron);
}
});
self.with_active_neurons_iter_sections(
|iter| {
for neuron in iter {
process_neuron(neuron);
}
},
NeuronSections::default(),
);

(ballots, deciding_voting_power, potential_voting_power)
}
Expand Down

0 comments on commit 6d94310

Please sign in to comment.