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

fix: [spearbit-32] Remove extra iteration for length check in AssociatedLinkedListSet #115

Merged
merged 2 commits into from
Jan 23, 2024

Conversation

adam-alchemy
Copy link
Contributor

Motivation

https://github.com/spearbit-audits/alchemy-nov-review/issues/32

AssociatedLinkedListSetLib iterates over the set twice, once to get the length and once to collect the elements. However, LinkedListSetLib already has an implementation that does not require an extra iteration over all elements, because the returned array is constructed as the elements are loaded. This causes an extra gas overhead for AssociatedLinkedListSetLib.

Solution

Port over the logic from LinkedListSetLib's implementation of getAll to AssociatedLinkedListSetLib's implementation.

Because the mapping lookups are handled differently due to the extra elements in the key buffer, we first convert the TempBytesMemory into a type that is effectively a "bytes memory" by adding its size to the free memory pointer.

@@ -354,48 +354,60 @@ library AssociatedLinkedListSetLib {
/// @notice Gets all elements in a set.
/// @dev This is an O(n) operation, where n is the number of elements in the set.
/// @param set The set to get the elements of.
/// @return res An array of all elements in the set.
/// @return ret An array of all elements in the set.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice

Copy link
Collaborator

@fangting-alchemy fangting-alchemy left a comment

Choose a reason for hiding this comment

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

Nice trick! Looks good.

@adam-alchemy adam-alchemy merged commit f1c6a16 into audit-2023-11-20 Jan 23, 2024
3 checks passed
@adam-alchemy adam-alchemy deleted the adam/fix-32 branch January 23, 2024 22:43
jaypaik pushed a commit that referenced this pull request Jan 25, 2024
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.

4 participants