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

Tweak enum serialization to generate better LLVM IR and more compact code #77

Merged
merged 1 commit into from
Feb 3, 2022

Commits on Feb 3, 2022

  1. Tweak enum serialization to generate better LLVM IR and more compact …

    …code
    
    Change enum serialization form
    
      match self {
        Enum::Variant1(field1, field2, ...) => {
            // serialize variant idx
            let variant_idx: u8 = 0;
            writer.write_all(&variant_idx.to_le_bytes())?;
    
            // serialize variant fields
            BorshSerialize::serialize(field1, writer)?;
            BorshSerialize::serialize(field2, writer)?;
            ...
        }
        Enum::Variant2(field1, field2, ...) => {
            let variant_idx: u8 = 1;
            writer.write_all(&variant_idx.to_le_bytes())?;
    
            // serialize variant fields
            BorshSerialize::serialize(field1, writer)?;
            BorshSerialize::serialize(field2, writer)?;
            ...
    
        }
        ...
      }
    
    To:
    
      let variant_idx: u8 = match self {
        Enum::Variant1(..) => 0,
        Enum::Variant2(..) => 1,
        ...
      };
      // serialize variant_idx
      writer.write_all(&variant_idx.to_le_bytes())?;
    
      match self {
        Enum::Variant1(field1, field2, ...) => {
            // serialize variant fields
            BorshSerialize::serialize(field1, writer)?;
            BorshSerialize::serialize(field2, writer)?;
            ...
        }
        Enum::Variant2(field1, field2, ...) => {
            // serialize variant fields
            BorshSerialize::serialize(field1, writer)?;
            BorshSerialize::serialize(field2, writer)?;
            ...
    
        }
        ...
      }
    
    The latter generates better LLVM IR, and avoids
    writer.write_all(&variant_idx.to_le_bytes())?; from being inlined into each
    match branch.
    alessandrod committed Feb 3, 2022
    Configuration menu
    Copy the full SHA
    62eb134 View commit details
    Browse the repository at this point in the history