Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

token-swap: Add version to state #985

Merged
merged 5 commits into from
Feb 4, 2021
Merged

Conversation

joncinque
Copy link
Contributor

Since we now have an upgradeable loader, we'll need to properly handle any updates to state structures. The approach here is to use the excellent enum_dispatch crate https://docs.rs/enum_dispatch/0.3.4/enum_dispatch/ to have multiple structs implement the same interface, and then use these structs as trait objects in processors. This seemed like the best approach to minimize code bloat in the current case. Note that this won't cover every possible case for backwards compatibility. For example, we might end up adding a field that won't have a good backwards-compatible default, requiring us to have different versions of instruction processors. Let's hope that doesn't happen too soon.

Since this approach requires dynamic dispatch on trait objects, I measured the number of BPF instructions before and after this PR on every swap instruction, along with the number of dynamic dispatch calls in each program instruction. Here are the results:

Dyn Calls Instruction Name BPF Instructions Before BPF Instructions After
30 Swap 60250 60516
17 WithdrawSingleTokenTypeExactAmountOut 64934 65168
15 DepositSingleTokenTypeExactAmountIn 57298 57505
15 WithdrawAllTokenTypes 37469 37667
13 DepositAllTokenTypes 29754 29953
0 Init 17928 17362

There's a small impact of ~250 BPF instructions, so this could be a viable approach for all programs! Any suggestions or comments are very appreciated.

cc @aeyakovenko @jstarry @mvines

@joncinque joncinque requested a review from jackcmay December 30, 2020 20:38
@bartosz-lipinski
Copy link
Contributor

Awesome, I am adding that to client side right now.

@joncinque joncinque force-pushed the ts-version branch 3 times, most recently from 199e438 to a1e8027 Compare January 19, 2021 18:24
@joncinque joncinque merged commit b264915 into solana-labs:master Feb 4, 2021
@joncinque joncinque deleted the ts-version branch February 4, 2021 14:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants