Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

solana-program: only decode prior_voters if needed #34972

Merged
merged 1 commit into from
Jan 27, 2024

Conversation

2501babe
Copy link
Contributor

Problem

originally i wrote the VoteState parse to check is_empty and skip parsing prior_voters if it was empty. later i got rid of this as unnecessary complexity. however it turns out prior_voters may be fully half the CU cost of a typical vote account, 7-8k vs 15-16k total

Summary of Changes

add this logic back in for a 50% efficiency gain if prior_voters is empty, which is more likely than not. i wrote this to only operate with the cursor, rather than the raw input bytes, per jons suggestion

also i added bounds checking to the cursor before and after; while writing this, i discovered Cursor successfully returns 0 rather than erroring if it runs out of bounds. also i fixed a mistake with the tests where insufficient data was generated, making coverage of the full struct basically nil

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (3add40f) 81.6% compared to head (8158ed5) 81.6%.
Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #34972   +/-   ##
=======================================
  Coverage    81.6%    81.6%           
=======================================
  Files         830      830           
  Lines      224746   224767   +21     
=======================================
+ Hits       183518   183564   +46     
+ Misses      41228    41203   -25     

@2501babe 2501babe marked this pull request as ready for review January 26, 2024 20:53
@2501babe 2501babe requested a review from joncinque January 26, 2024 20:54
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks great!

Reading past the end and getting 0 is pretty wacky, but it's (shockingly) right in the implementation: https://doc.rust-lang.org/src/std/io/cursor.rs.html#320

@@ -886,7 +898,7 @@ mod tests {

// variant
// provide 4x the minimum struct size in bytes to ensure we typically touch every field
let struct_bytes_x4 = std::mem::size_of::<u64>() * 4;
let struct_bytes_x4 = std::mem::size_of::<VoteState>() * 4;
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes a lot more sense, I probably should have said more in the last PR 😅

@2501babe 2501babe merged commit c99427e into solana-labs:master Jan 27, 2024
45 checks passed
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