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

refactor: use ordered publisher list #402

Merged
merged 3 commits into from
May 13, 2024

Conversation

ali-bahjati
Copy link
Collaborator

@ali-bahjati ali-bahjati commented May 4, 2024

This change brings down the average cost of a price update without aggregation from 5k to 2k by moving from unordered list of publishers to an ordered list of publishers on doing binary search on lookup. It will fall back to linear search if the binary search fails.

Also add_publisher now tries to sort the list with [0,0,0...] (Base58: 11111...) publisher as a migration path. The sort is implemented in a way to be efficient and small (in code size). There is an insertion sort upon each new publisher to make sure the list remains sorted. del_publisher won't change the order of the list so it will remain sorted.

The heavy code path for publishers now uses sol_memcmp (on sorting and lookup) to be more optimized (it costs 10 CU and apparently publisher equality is a few times more expensive). I checked sol_memcmp without change to sorted but the result was not as significant. Maybe later we can extract it out to a function, and use it everywhere.

@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch from 5335952 to 8b2f52f Compare May 6, 2024 11:29
@ali-bahjati ali-bahjati requested a review from cctdaniel May 6, 2024 11:29
program/rust/src/processor/add_publisher.rs Outdated Show resolved Hide resolved
program/rust/src/processor/upd_price.rs Outdated Show resolved Hide resolved
@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch from 8b2f52f to 4fa26a9 Compare May 6, 2024 14:58
Copy link
Contributor

@jayantk jayantk left a comment

Choose a reason for hiding this comment

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

I don't love the incremental sorting pass -- see inline comment -- but happy to discuss that if there's some particular reason you are doing it this way

program/rust/src/processor/upd_price.rs Outdated Show resolved Hide resolved
program/rust/src/processor/upd_price.rs Outdated Show resolved Hide resolved
@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch 2 times, most recently from 0e81986 to 58afc9d Compare May 7, 2024 17:36
jayantk
jayantk previously approved these changes May 9, 2024
@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch 2 times, most recently from 6a58b0a to 13aeaef Compare May 13, 2024 14:13
This change brings down the average cost of a price update without
aggregation from 5k to 2k because binary search requires much less
lookups. There are mechanims implemented in the code to make sure
that upon the upgrade we sort the array without adding overhead to
the happy path (working binary search). Also add_publisher now does
an insertion sort to keep the list sorted and del_publisher won't
change the order of the list.
@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch from 13aeaef to c27465d Compare May 13, 2024 14:28
jayantk
jayantk previously approved these changes May 13, 2024
{
price_data.comp_.swap(current_index, current_index - 1);
current_index -= 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

minor comment: imo, we should call sort_price_comps here as well, because the insertion code above isn't tested by the quickcheck stuff below. We don't add publishers a lot so we don't care about optimizing CU use.

Copy link
Contributor

Choose a reason for hiding this comment

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

(feel free to ignore this. I'm only saying it because it feels like the maximally defensive implementation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

did that. removed the inline hint to make sure code size doesn't get larger (by sacrificing a bit of CU)

@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch from 45002cf to 37e99b8 Compare May 13, 2024 14:51
jayantk
jayantk previously approved these changes May 13, 2024
Reisen
Reisen previously approved these changes May 13, 2024
guibescos
guibescos previously approved these changes May 13, 2024
@ali-bahjati ali-bahjati dismissed stale reviews from guibescos and Reisen via 46cda78 May 13, 2024 18:25
@ali-bahjati ali-bahjati force-pushed the refactor/use-ordered-publisher-set branch from 46cda78 to b991cba Compare May 13, 2024 18:27
@ali-bahjati ali-bahjati merged commit 87ca5ac into main May 13, 2024
3 checks passed
@ali-bahjati ali-bahjati deleted the refactor/use-ordered-publisher-set branch May 13, 2024 18:50
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