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

chore!: ToRadixLE -> ToRadixBE in Brillig and AVM #9340

Closed
wants to merge 2 commits into from

Conversation

dbanks12
Copy link
Collaborator

No description provided.

Copy link
Collaborator Author

dbanks12 commented Oct 22, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @dbanks12 and the rest of your teammates on Graphite Graphite

Copy link
Contributor

github-actions bot commented Oct 22, 2024

Changes to public function bytecode sizes

Generated at commit: 8b214445d0f00c13c601f8a4e3dee48f1cc7ca0c, compared to commit: f4ed55b264ff92979e6e655508b8f8fac826086e

🧾 Summary (100% most significant diffs)

Program Bytecode size in bytes (+/-) %
AvmTest::to_radix_le +144 ❌ +59.02%
AvmTest::keccak_hash +144 ❌ +5.35%
AvmTest::sha256_hash +144 ❌ +3.81%
CardGame::on_game_joined +144 ❌ +3.07%
CardGame::on_card_played +144 ❌ +2.58%
CardGame::start_game +144 ❌ +2.16%
CardGame::on_cards_claimed +144 ❌ +2.06%
CardGame::public_dispatch +144 ❌ +0.92%
AvmTest::bulk_testing +144 ❌ +0.68%
AvmTest::public_dispatch +184 ❌ +0.33%
TokenBridge::public_dispatch -20 ✅ -0.10%
Test::public_dispatch -20 ✅ -0.11%
TokenBridge::claim_public -20 ✅ -0.17%
Test::consume_mint_public_message -20 ✅ -0.20%
Token::public_dispatch -164 ✅ -0.51%
NFT::public_dispatch -164 ✅ -0.64%
NFT::_finalize_transfer_to_private_unsafe -164 ✅ -2.61%
NFT::finalize_transfer_to_private -164 ✅ -2.62%
Token::complete_refund -164 ✅ -2.80%

Full diff report 👇
Program Bytecode size in bytes (+/-) %
AvmTest::to_radix_le 388 (+144) +59.02%
AvmTest::keccak_hash 2,836 (+144) +5.35%
AvmTest::sha256_hash 3,920 (+144) +3.81%
CardGame::on_game_joined 4,833 (+144) +3.07%
CardGame::on_card_played 5,718 (+144) +2.58%
CardGame::start_game 6,821 (+144) +2.16%
CardGame::on_cards_claimed 7,133 (+144) +2.06%
CardGame::public_dispatch 15,848 (+144) +0.92%
AvmTest::bulk_testing 21,411 (+144) +0.68%
AvmTest::public_dispatch 55,743 (+184) +0.33%
TokenBridge::public_dispatch 20,983 (-20) -0.10%
Test::public_dispatch 18,933 (-20) -0.11%
TokenBridge::claim_public 11,478 (-20) -0.17%
Test::consume_mint_public_message 10,146 (-20) -0.20%
Token::public_dispatch 31,961 (-164) -0.51%
NFT::public_dispatch 25,298 (-164) -0.64%
NFT::_finalize_transfer_to_private_unsafe 6,128 (-164) -2.61%
NFT::finalize_transfer_to_private 6,097 (-164) -2.62%
Token::complete_refund 5,694 (-164) -2.80%

@dbanks12 dbanks12 requested review from sirasistant and removed request for Maddiaa0 October 23, 2024 00:58
@dbanks12 dbanks12 added the C-avm Component: AVM related tickets (aka public VM) label Oct 23, 2024
Copy link
Contributor

@fcarreiro fcarreiro left a comment

Choose a reason for hiding this comment

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

I'm surprised this doesn't give us bytecode gains. @sirasistant I thought Brillig was needing some conversions LE -> BE because it was expecting to do ToRadixBE?

@dbanks12 dbanks12 requested a review from TomAFrench October 24, 2024 10:19
@dbanks12
Copy link
Collaborator Author

I'm surprised this doesn't give us bytecode gains. @sirasistant I thought Brillig was needing some conversions LE -> BE because it was expecting to do ToRadixBE?

I thought the same @fcarreiro! @sirasistant's response was "should not save much in bytecode size, but it'd have better runtime"

@sirasistant
Copy link
Collaborator

sirasistant commented Oct 24, 2024

I'm surprised this doesn't give us bytecode gains

Reversals are deduplicated via a procedure call so it shouldn't be noticeable. However I think we might be using little endianness more than I thought given the measurement changes

@sirasistant
Copy link
Collaborator

sirasistant commented Oct 24, 2024

Ah I see, card game uses to_le_bytes for packing and

fn compute_lt(x: Field, y: Field, num_bytes: u32) -> bool {
    let x_bytes: [u8; 32] = x.to_le_bytes();
    let y_bytes: [u8; 32] = y.to_le_bytes();
    let mut x_is_lt = false;
    let mut done = false;
    for i in 0..num_bytes {
        if (!done) {
            let x_byte = x_bytes[num_bytes - 1 - i];
            let y_byte = y_bytes[num_bytes - 1 - i];
            let bytes_match = x_byte == y_byte;
            if !bytes_match {
                x_is_lt = x_byte < y_byte;
                done = true;
            }
        }
    }
    x_is_lt
}

Field comparisons in the stdlib are using little endian (probably because before this PR little endian was cheaper)

@sirasistant
Copy link
Collaborator

Maybe we can switch the compute_lt and the card game to use be and see what the results are

@fcarreiro
Copy link
Contributor

Ah I see, card game uses to_le_bytes for packing and

fn compute_lt(x: Field, y: Field, num_bytes: u32) -> bool {
    let x_bytes: [u8; 32] = x.to_le_bytes();
    let y_bytes: [u8; 32] = y.to_le_bytes();
    let mut x_is_lt = false;
    let mut done = false;
    for i in 0..num_bytes {
        if (!done) {
            let x_byte = x_bytes[num_bytes - 1 - i];
            let y_byte = y_bytes[num_bytes - 1 - i];
            let bytes_match = x_byte == y_byte;
            if !bytes_match {
                x_is_lt = x_byte < y_byte;
                done = true;
            }
        }
    }
    x_is_lt
}

Field comparisons in the stdlib are using little endian (probably because before this PR little endian was cheaper)

Hmm, does this mean that we should drop this PR? or change the stdlib?

@dbanks12
Copy link
Collaborator Author

Does seem to reduce gas costs for some e2e tests by a bit! Not a massive improvement. TokenBridge:claim_public goes from 3,970,000 down to 3,930,000.

@dbanks12 dbanks12 changed the base branch from master to db/field_less_than October 25, 2024 17:42
Base automatically changed from db/field_less_than to master October 28, 2024 13:13
@dbanks12
Copy link
Collaborator Author

Replaced by #9471

@dbanks12 dbanks12 closed this Oct 29, 2024
@dbanks12 dbanks12 deleted the db/toradixbe branch November 21, 2024 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-avm Component: AVM related tickets (aka public VM)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants