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

Dev Feature 8 - Contract Wide CPU Optimizations #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

on-a-t-break
Copy link

@on-a-t-break on-a-t-break commented Oct 3, 2024

Note: This merge is for demonstration purposes & will be rewritten as a final form once all of the other features are implemented.

Overall improvements are benchmarked with minting 100x templated assets, with no mutable or immutable data & 4 schema fields, resulting in:

  • Current version: 4.25ms to 8.54ms range, average of 6.385ms
  • Upgraded version: 3ms to 7ms range, average of 5ms
  • Estimated 1ms to 1.5ms range of reduction in computation time
  • Estimated 20% to 30% range of CPU cost reduction at the high end
  • Estimated 5% to 8% range of CPU cost reduction in practice (due to more data being in mint asset actions, more schema fields, smaller collection's 'serialized_data')

Table Fetch Improvements
When a table fetch is defined as so:

config_t config = config_t(get_self(), get_self().value);

It makes it so that the table is initialized & stored in memory on every single action call to the contract (including inline actions to itself, such as the logs). Essentially, something like 10x tables are being initialized every single time the contract executes any action, adding an unnecessary overhead to CPU costs.

They have been rewritten to be as so:

config_t get_config {return config_t(get_self(), get_self().value);}

Making it so that they are initialized only when the function is called instead of being implicitly initialized, helping mitigate CPU costs. All table fetches have been redefine according to this format.

Collection Data Improvements
All collections' table fetches are consuming (up to) an extra 3kb of on-chain data stored under the 'serialized_data' field, which no smart contract actually needs to interact with. This data is then fetched again during logs, making it so that every 'mintasset' & 'setassetdata' action is fetching (up to) 6kb of unnecessary data.

This upgrade moves the 'serialized_data' to the 'coldata' scope using the same primary_key, as well as implementing automatic conversions to this new approach upon any interaction with a collections' fees, authorized_accounts, notify_accounts or serialized_data.

Further more, 'check_has_collection_auth' has been rewritten to be more CPU performative by not performing a "find" operation on the collections table twice by passing the collection_itr pointer to the function directly

Reference Improvements
Makes the 'check_name_length' & 'check_format' functions operate on data by reference instead of by value, preventing unnecessary copies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant