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

[move-stdlib] Implement bcs::constant_serialized_size<T>(): Option<u64> #14984

Merged
merged 3 commits into from
Nov 14, 2024

Conversation

igor-aptos
Copy link
Contributor

Description

It is sometimes useful to know if type has constants serialized size, and for example perform some optimization based on it.

How Has This Been Tested?

provided unit tests

Key Areas to Review

Type of Change

  • New feature

Which Components or Systems Does This Change Impact?

  • Move/Aptos Virtual Machine

Checklist

  • I have read and followed the CONTRIBUTING doc
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I identified and added all stakeholders and component owners affected by this change as reviewers
  • I tested both happy and unhappy path of the functionality
  • I have made corresponding changes to the documentation

Copy link

trunk-io bot commented Oct 16, 2024

⏱️ 1h 5m total CI duration on this PR
Job Cumulative Duration Recent Runs
rust-move-unit-coverage 15m 🟩
rust-move-tests 9m 🟥
rust-move-tests 9m 🟥
rust-move-unit-coverage 8m
rust-move-tests 8m
rust-cargo-deny 5m 🟩🟩🟩
check 4m 🟩
check-dynamic-deps 3m 🟩🟩🟩
general-lints 1m 🟩🟩🟩
semgrep/ci 54s 🟩🟩🟩
file_change_determinator 40s 🟩🟩🟩
permission-check 8s 🟩🟩🟩
permission-check 7s 🟩🟩🟩

settingsfeedbackdocs ⋅ learn more about trunk.io

@igor-aptos igor-aptos force-pushed the igor/use_native_vector_move_range branch from 3bcedb1 to a5af4dc Compare October 16, 2024 21:17
@igor-aptos igor-aptos force-pushed the igor/constant_serialized_size branch from 8a929c9 to a420b4c Compare October 16, 2024 21:17
Copy link

codecov bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 1.81818% with 54 lines in your changes missing coverage. Please review.

Project coverage is 57.5%. Comparing base (a5af4dc) to head (a420b4c).

Files with missing lines Patch % Lines
third_party/move/move-vm/types/src/value_serde.rs 0.0% 34 Missing ⚠️
...ptos-move/framework/move-stdlib/src/natives/bcs.rs 4.7% 20 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           igor/use_native_vector_move_range   #14984     +/-   ##
====================================================================
- Coverage                               60.1%    57.5%   -2.7%     
====================================================================
  Files                                    858      858             
  Lines                                 211455   211510     +55     
====================================================================
- Hits                                  127237   121651   -5586     
- Misses                                 84218    89859   +5641     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Ok(fields
.iter()
.map(|field| constant_serialized_size(&field.layout))
.collect::<Result<Option<Vec<_>>, _>>()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this potentially allocate too much? Shall we bother to write a loop or try_for_each()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is nice, but I agree - safer to not worry about it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@igor-aptos igor-aptos force-pushed the igor/use_native_vector_move_range branch from a5af4dc to 9fcf365 Compare October 21, 2024 23:10
@igor-aptos igor-aptos force-pushed the igor/constant_serialized_size branch from a420b4c to 4c0b5bc Compare October 21, 2024 23:10
@igor-aptos igor-aptos requested a review from msmouse October 21, 2024 23:27
@igor-aptos igor-aptos force-pushed the igor/constant_serialized_size branch from 4c0b5bc to 58152b4 Compare November 8, 2024 23:21

This comment has been minimized.

@@ -110,6 +110,10 @@ module admin::transaction_context_test {
), type_info::type_name<T3>()],
13
);

assert!(option::some(option::destroy_some(payload_opt)) == transaction_context::entry_function_payload(), 13);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unrelated, but done while working on this PR - added a test that move correctly returns Option from natives, which this PR also does

/// If this function returned Some() for some type before - it is guaranteed to continue returning Some()
/// On the other hand, if function has returned None for some type,
/// it might change in the future to return Some() instead, if size becomes "known".
native public(friend) fun constant_serialized_size<MoveValue>(): std::option::Option<u64>;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: import at the top of the file, here just Option?

/// Note:
/// For some types it might not be known they have constant size, and function might return None.
/// For example, signer appears to have constant size, but it's size might change.
/// If this function returned Some() for some type before - it is guaranteed to continue returning Some()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: . missing

}

// enum Singleton {
Copy link
Contributor

Choose a reason for hiding this comment

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

This for Move 2 tests only? You have tested it locally? Or will enable later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will enable this once framework is compiled with move 2

}
Ok(total)
},
MoveTypeLayout::Struct(MoveStructLayout::WithFields(fields))
Copy link
Contributor

Choose a reason for hiding this comment

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

WithFields and WithTypes should not occur at runtime, I think we can error. As type_to_type_layout returns the runtime variant

@georgemitenkov
Copy link
Contributor

Thanks! Feel free to land and address the comments in a separate PR if needed

/// For example, signer appears to have constant size, but it's size might change.
/// If this function returned Some() for some type before - it is guaranteed to continue returning Some()
/// On the other hand, if function has returned None for some type,
/// it might change in the future to return Some() instead, if size becomes "known".
Copy link
Contributor

Choose a reason for hiding this comment

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

KK, that makes sense.

@@ -172,6 +174,112 @@ pub fn serialized_size_allowing_delayed_values(
})
}

/// Count number of types constant_serialized_size would visit, used for gas charging.
Copy link
Contributor

Choose a reason for hiding this comment

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

My comment was that "Count number of types constant_serialized_size would visit" is incorrect. Mainly because constant_serializeed_size returns early if it encounters non-constant seriazible field:

match cur {
                    Some(cur_value) => total = total.map(|v| v + cur_value),
                    None => {
                        total = None; // <--------------------------------------------------------
                        break;
                    },
                }

Maybe re-comment to: The upper bound of the number of types constant_serialized_size would visit... or something like that?

Also, given that these two functions here are so specific for the current native implementation, I'd just move them to the same file where the native function is defined (I'd say they belong there a lot more than here!).

@igor-aptos igor-aptos force-pushed the igor/constant_serialized_size branch from 974ad93 to f6c8829 Compare November 13, 2024 23:24
@igor-aptos igor-aptos requested a review from ziaptos November 13, 2024 23:24
@igor-aptos
Copy link
Contributor Author

addressed comments

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@igor-aptos
Copy link
Contributor Author

I've looked at bcs::to_bytes / bcs::serialized_size, and we don't charge gas as we go, but charge base upfront, and rest at the end (so if it goes out of gas work is done).

not sure if that's best, but it's simpler and in this case even more reasonable (as cost of actual work is pretty cheap), so I've merged two functions, and charge visited count after computation - but both on success and failure path. This should be clean and clear now. let me know if you have any concerns. I'll proceed to land it in the next hour or two

@igor-aptos igor-aptos dismissed ziaptos’s stale review November 14, 2024 00:03

addressed comments

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@igor-aptos igor-aptos enabled auto-merge (squash) November 14, 2024 01:10
Copy link
Contributor

@ziaptos ziaptos left a comment

Choose a reason for hiding this comment

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

Air Igor 14984 Cleared for Landing

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

✅ Forge suite realistic_env_max_load success on 2db92e968ec30c371c9c237d9fab80ff2dba62ec

two traffics test: inner traffic : committed: 14364.25 txn/s, latency: 2769.55 ms, (p50: 2700 ms, p70: 2700, p90: 3000 ms, p99: 3300 ms), latency samples: 5461640
two traffics test : committed: 99.95 txn/s, latency: 1473.33 ms, (p50: 1300 ms, p70: 1400, p90: 1500 ms, p99: 10400 ms), latency samples: 1800
Latency breakdown for phase 0: ["MempoolToBlockCreation: max: 2.043, avg: 1.600", "ConsensusProposalToOrdered: max: 0.330, avg: 0.294", "ConsensusOrderedToCommit: max: 0.359, avg: 0.351", "ConsensusProposalToCommit: max: 0.654, avg: 0.645"]
Max non-epoch-change gap was: 0 rounds at version 0 (avg 0.00) [limit 4], 1.68s no progress at version 25844 (avg 0.20s) [limit 15].
Max epoch-change gap was: 0 rounds at version 0 (avg 0.00) [limit 4], 8.63s no progress at version 2296280 (avg 8.63s) [limit 15].
Test Ok

Copy link
Contributor

✅ Forge suite framework_upgrade success on a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec

Compatibility test results for a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec (PR)
Upgrade the nodes to version: 2db92e968ec30c371c9c237d9fab80ff2dba62ec
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 1329.86 txn/s, submitted: 1331.41 txn/s, failed submission: 1.55 txn/s, expired: 1.55 txn/s, latency: 2383.32 ms, (p50: 2100 ms, p70: 2400, p90: 3600 ms, p99: 4800 ms), latency samples: 119920
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 1300.99 txn/s, submitted: 1303.84 txn/s, failed submission: 2.85 txn/s, expired: 2.85 txn/s, latency: 2267.43 ms, (p50: 2100 ms, p70: 2400, p90: 3400 ms, p99: 4800 ms), latency samples: 118600
5. check swarm health
Compatibility test for a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec passed
Upgrade the remaining nodes to version: 2db92e968ec30c371c9c237d9fab80ff2dba62ec
framework_upgrade::framework-upgrade::full-framework-upgrade : committed: 1338.52 txn/s, submitted: 1341.64 txn/s, failed submission: 3.13 txn/s, expired: 3.13 txn/s, latency: 2280.00 ms, (p50: 2100 ms, p70: 2400, p90: 3600 ms, p99: 5100 ms), latency samples: 119820
Test Ok

Copy link
Contributor

✅ Forge suite compat success on a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec

Compatibility test results for a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec (PR)
1. Check liveness of validators at old version: a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe
compatibility::simple-validator-upgrade::liveness-check : committed: 18322.70 txn/s, latency: 1862.89 ms, (p50: 1900 ms, p70: 2000, p90: 2100 ms, p99: 2200 ms), latency samples: 587560
2. Upgrading first Validator to new version: 2db92e968ec30c371c9c237d9fab80ff2dba62ec
compatibility::simple-validator-upgrade::single-validator-upgrading : committed: 7904.43 txn/s, latency: 3653.45 ms, (p50: 4100 ms, p70: 4300, p90: 4400 ms, p99: 4400 ms), latency samples: 145700
compatibility::simple-validator-upgrade::single-validator-upgrade : committed: 7091.50 txn/s, latency: 4428.80 ms, (p50: 4300 ms, p70: 4400, p90: 6900 ms, p99: 7100 ms), latency samples: 236940
3. Upgrading rest of first batch to new version: 2db92e968ec30c371c9c237d9fab80ff2dba62ec
compatibility::simple-validator-upgrade::half-validator-upgrading : committed: 7579.64 txn/s, latency: 3639.12 ms, (p50: 3800 ms, p70: 4100, p90: 5000 ms, p99: 5400 ms), latency samples: 141660
compatibility::simple-validator-upgrade::half-validator-upgrade : committed: 7605.35 txn/s, latency: 4251.69 ms, (p50: 4400 ms, p70: 4600, p90: 6100 ms, p99: 6200 ms), latency samples: 251740
4. upgrading second batch to new version: 2db92e968ec30c371c9c237d9fab80ff2dba62ec
compatibility::simple-validator-upgrade::rest-validator-upgrading : committed: 12415.57 txn/s, latency: 2222.39 ms, (p50: 2500 ms, p70: 2500, p90: 2600 ms, p99: 2700 ms), latency samples: 216460
compatibility::simple-validator-upgrade::rest-validator-upgrade : committed: 10747.52 txn/s, latency: 2906.43 ms, (p50: 2600 ms, p70: 2700, p90: 5600 ms, p99: 7400 ms), latency samples: 354900
5. check swarm health
Compatibility test for a0ec6ba11bfe4cfc5b586edc9e227aba4909e8fe ==> 2db92e968ec30c371c9c237d9fab80ff2dba62ec passed
Test Ok

@igor-aptos igor-aptos merged commit 573e01e into main Nov 14, 2024
92 of 93 checks passed
@igor-aptos igor-aptos deleted the igor/constant_serialized_size branch November 14, 2024 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CICD:run-e2e-tests when this label is present github actions will run all land-blocking e2e tests from the PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants