Skip to content

Conversation

@songgaoye
Copy link

Closes: #XXX

Description

all 3 remove(0) operations replaced with into_iter().next(). It can avoids the front-shift penalty, O(n) -> O(1)


PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests: integration (for Hermes) or unit/mock tests (for modules).
  • Linked to GitHub issue.
  • Updated code comments and documentation (e.g., docs/).
    • If guide has been updated, tag GitHub user mircea-c
  • Tagged one reviewer who will be the one responsible for shepherding this PR.

Reviewer checklist:

  • Reviewed Files changed in the GitHub PR explorer.
  • Manually tested (in case integration/unit/mock tests are absent).

Copy link

@KirilMihaylov KirilMihaylov left a comment

Choose a reason for hiding this comment

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

Using swap_remove will reduce the readability overhead as it's equivalent to the provided solution.

P.S.:
Leaving this review as a member of the @nolus-protocol dev team.

);

let tx = response.txs.remove(0);
let tx = response.txs.into_iter().next().expect("tx_search was constrained to a single result");

Choose a reason for hiding this comment

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

Suggested change
let tx = response.txs.into_iter().next().expect("tx_search was constrained to a single result");
let tx = response.txs.swap_remove(0);

Choose a reason for hiding this comment

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

On second look, change is unnecessary in the first place because the assertion will fail if there is more than one element.

Ok(vec![])
} else {
let tx = response.txs.remove(0);
let tx = response.txs.into_iter().next().expect("tx_search was constrained to a single result");

Choose a reason for hiding this comment

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

Suggested change
let tx = response.txs.into_iter().next().expect("tx_search was constrained to a single result");
let tx = response.txs.swap_remove(0);


// In either case, use the first (latest) event found for this sequence
let (first_event, _, _) = tx_events.remove(0);
let (first_event, _, _) = tx_events.into_iter().next().expect("tx_events is known to contain at least one entry");

Choose a reason for hiding this comment

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

Suggested change
let (first_event, _, _) = tx_events.into_iter().next().expect("tx_events is known to contain at least one entry");
let (first_event, _, _) = tx_events.swap_remove(0);

@songgaoye
Copy link
Author

songgaoye commented Jan 21, 2026

Using swap_remove will reduce the readability overhead as it's equivalent to the provided solution.

P.S.: Leaving this review as a member of the @nolus-protocol dev team.

I don't think so.
the current solution with into_iter().next() is even better because:

1.Same O(1) performance as swap_remove
2.Consumes the vector - makes it clear the rest of the data is discarded
3.More explicit intent - the code shows we only care about the first element
4.No unnecessary swap - swap_remove still does a swap operation even though we're discarding the vector

@KirilMihaylov
Copy link

At (1):

This does not preserve ordering of the remaining elements, but is O(1). If you need to preserve the element order, use remove instead.

Source: std::vec::Vec::swap_remove

At (2):

{ tx_events }.swap_remove(0)

At (3):
In my opinion it's just as explicit, and what's more, swap_remove is less verbose.

At (4):
Yes, I admit that it will do one swap, but that's a trade-off justified by being more concise in my eyes.

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