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

Open
wants to merge 2 commits into
base: db/field_less_than
Choose a base branch
from

Conversation

dbanks12
Copy link
Contributor

No description provided.

Copy link
Contributor Author

dbanks12 commented Oct 22, 2024

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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: b213fc7bc0ddfb30bc6368869f5576e298033d46, compared to commit: ad801693592df3263b8a621a081c7616948524da

🧾 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 ❌ +5.07%
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 +164 ❌ +0.71%
AvmTest::public_dispatch +184 ❌ +0.33%
TokenBridge::public_dispatch -20 ✅ -0.10%
Test::public_dispatch -20 ✅ -0.11%
TokenBridge::claim_public -20 ✅ -0.20%
Test::consume_mint_public_message -20 ✅ -0.23%
FPC::public_dispatch -463 ✅ -4.84%
Router::public_dispatch -431 ✅ -15.25%
FPC::pay_refund -431 ✅ -25.44%
FPC::pay_refund_with_shielded_rebate -431 ✅ -26.44%
Router::_check_timestamp -431 ✅ -33.18%
Router::_check_block_number -431 ✅ -33.62%

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 2,982 (+144) +5.07%
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 23,249 (+164) +0.71%
AvmTest::public_dispatch 55,176 (+184) +0.33%
TokenBridge::public_dispatch 20,187 (-20) -0.10%
Test::public_dispatch 17,532 (-20) -0.11%
TokenBridge::claim_public 10,082 (-20) -0.20%
Test::consume_mint_public_message 8,745 (-20) -0.23%
FPC::public_dispatch 9,110 (-463) -4.84%
Router::public_dispatch 2,395 (-431) -15.25%
FPC::pay_refund 1,263 (-431) -25.44%
FPC::pay_refund_with_shielded_rebate 1,199 (-431) -26.44%
Router::_check_timestamp 868 (-431) -33.18%
Router::_check_block_number 851 (-431) -33.62%

@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
Copy link
Contributor 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
Contributor

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
Contributor

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
Contributor

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
Contributor 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
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