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

refactor(store): optimize PackedCounter #1231

Merged
merged 34 commits into from
Aug 18, 2023
Merged

refactor(store): optimize PackedCounter #1231

merged 34 commits into from
Aug 18, 2023

Conversation

dk1a
Copy link
Contributor

@dk1a dk1a commented Aug 2, 2023

Many small tweaks in PackedCounter.sol,
and a big change to pack lengths from right to left (and corresponding changes to sync)

@changeset-bot
Copy link

changeset-bot bot commented Aug 2, 2023

🦋 Changeset detected

Latest commit: 366007d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@latticexyz/cli Major
@latticexyz/protocol-parser Major
@latticexyz/services Major
@latticexyz/store-sync Major
@latticexyz/store Major
@latticexyz/world Major
@latticexyz/std-client Major
@latticexyz/dev-tools Major
@latticexyz/store-indexer Major
@latticexyz/react Major
@latticexyz/store-cache Major
@latticexyz/ecs-browser Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/recs Major
@latticexyz/schema-type Major
@latticexyz/solecs Major
solhint-config-mud Major
solhint-plugin-mud Major
@latticexyz/std-contracts Major
@latticexyz/utils Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

).toThrowErrorMatchingInlineSnapshot(
'"PackedCounter \\"0x0000000000004000000000200000000020000000004000000000000000000000\\" total bytes length (64) did not match the summed length of all field byte lengths (128)."'
'"PackedCounter \\"0x0000000000000000000000000000400000000020000000002000000000000040\\" total bytes length (64) did not match the summed length of all field byte lengths (128)."'
Copy link
Member

Choose a reason for hiding this comment

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

why did these 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.

ints are right-aligned, it's a bit more optimal to have them packed right to left

Copy link
Member

@holic holic Aug 6, 2023

Choose a reason for hiding this comment

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

to clarify, you just changed the position order of (uint56, uint40[5]) to (uint40[5], uint56)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, but it's important to clarify that uint40[5] is in reverse as well, the first element is the right-most one

@dk1a
Copy link
Contributor Author

dk1a commented Aug 7, 2023

Pretty sure we'll have more breaking changes (store events, methods, maybe schema), probably best not to add support for old versions each time

Copy link
Member

@alvrs alvrs left a comment

Choose a reason for hiding this comment

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

Very nice savings and code looks good overall. Just had some minor nits and questions. Let's also make sure to add some context on the change from left aligned to right aligned to the changeset (i only understood the reason after looking at the diff of PackedCounter.sol, but we can't expect users to look at the PR diffs so we should explain it in the changeelog)

.changeset/little-ravens-yawn.md Outdated Show resolved Hide resolved
packages/protocol-parser/src/hexToPackedCounter.ts Outdated Show resolved Hide resolved
@@ -75,25 +75,25 @@
"file": "test/Gas.t.sol",
"test": "testCompareAbiEncodeVsCustom",
"name": "custom encode",
"gasUsed": 2806
"gasUsed": 1394
Copy link
Member

Choose a reason for hiding this comment

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

nice 🔥

Comment on lines 273 to +282
"file": "test/PackedCounter.t.sol",
"test": "testAtIndex",
"name": "get value at index of PackedCounter",
"gasUsed": 255
"gasUsed": 24
},
{
"file": "test/PackedCounter.t.sol",
"test": "testSetAtIndex",
"name": "set value at index of PackedCounter",
"gasUsed": 793
"test": "testGas",
"name": "pack 1 length into PackedCounter",
"gasUsed": 35
Copy link
Member

Choose a reason for hiding this comment

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

wow 🔥

}

/**
* Set a counter at the given index, return the new packed counter
*/
function setAtIndex(
PackedCounter packedCounter,
uint256 index,
uint8 index,
Copy link
Member

Choose a reason for hiding this comment

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

why uint8 instead of uint256?

Copy link
Contributor Author

@dk1a dk1a Aug 8, 2023

Choose a reason for hiding this comment

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

it was easier/cheaper than a manual check, and aligns well with how StoreCore also uses uint8 for schemaIndex

packages/store/src/PackedCounter.sol Outdated Show resolved Hide resolved
packages/store/src/PackedCounter.sol Outdated Show resolved Hide resolved
packages/store/src/PackedCounter.sol Outdated Show resolved Hide resolved
packages/store/src/PackedCounter.sol Outdated Show resolved Hide resolved
Co-authored-by: alvarius <alvarius@lattice.xyz>
@@ -194,9 +194,11 @@ library NumberList {

Copy link
Member

Choose a reason for hiding this comment

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

do templates codegen need to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

double-checked, nope (I think CI'd catch it). This affects only tables with dynamic fields, which templates don't have

holic
holic previously approved these changes Aug 18, 2023
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
@dk1a dk1a merged commit 433078c into main Aug 18, 2023
@dk1a dk1a deleted the dk1a/packedcounter branch August 18, 2023 15:50
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.

3 participants