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

Update basic order flow to avoid allocating unnecessary memory #45

Merged
merged 3 commits into from
Mar 1, 2024

Conversation

0age
Copy link
Contributor

@0age 0age commented Feb 29, 2024

No description provided.

@Saw-mon-and-Natalie
Copy link
Collaborator

Saw-mon-and-Natalie commented Feb 29, 2024

@0age, here is at least one more edit that is needed:

This head of the event also needs to be lifted up (lower in the memory region):

// Derive pointer to start of OrderFulfilled event data.
let eventDataPtr :=
    add(
        OrderFulfilled_baseOffset,
        shl(
            OneWordShift,
            calldataload(
                BasicOrder_additionalRecipients_length_cdPtr // <--- need to change this as well
            )
        )
    )

@0age 0age force-pushed the wip-basic-order-memory-optimization branch from ac61dbe to 4d5138d Compare February 29, 2024 19:50
@0age
Copy link
Contributor Author

0age commented Feb 29, 2024

made the suggested change @Saw-mon-and-Natalie — still seeing failures however 🙃

@0age 0age changed the title update basic order flow to avoid allocating unnecessary memory WIP — update basic order flow to avoid allocating unnecessary memory Feb 29, 2024
Comment on lines +1010 to +1019
mstore(add(eventDataPtr, 0x80), 1)

// Write itemType to the SpentItem struct.
mstore(add(eventDataPtr, 0xa0), offeredItemType)

// Copy calldata region with (offerToken, offerIdentifier,
// offerAmount) from OrderParameters to (token, identifier,
// amount) in SpentItem struct.
calldatacopy(
add(eventDataPtr, 0xc0),
Copy link
Collaborator

Choose a reason for hiding this comment

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

TODO: define named constants for these 3 new offsets.

Comment on lines +1009 to +1022
// Set a length of 1 for the offer array.
mstore(add(eventDataPtr, 0x80), 1)

// Write itemType to the SpentItem struct.
mstore(add(eventDataPtr, 0xa0), offeredItemType)

// Copy calldata region with (offerToken, offerIdentifier,
// offerAmount) from OrderParameters to (token, identifier,
// amount) in SpentItem struct.
calldatacopy(
add(eventDataPtr, 0xc0),
BasicOrder_offerToken_cdPtr,
ThreeWords
)
Copy link
Collaborator

@Saw-mon-and-Natalie Saw-mon-and-Natalie Mar 1, 2024

Choose a reason for hiding this comment

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

I moved these down here, since it would make it more clear what we are doing in memory and also would require less calculation of the pointer.

and automatically the offset is also corrected.

shl(
OneWordShift,
calldataload(
BasicOrder_additionalRecipients_length_cdPtr
Copy link
Collaborator

Choose a reason for hiding this comment

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

this was wrong, we needed to use:

BasicOrder_totalOriginalAdditionalRecipients_cdPtr

@Saw-mon-and-Natalie
Copy link
Collaborator

Also for restricted orders pre and post hook we can reuse the same memory region for spent and received items without copying that region again. Just need to rewrite the memory before the region and make sure to allocate 3 word slots after that region for the order hashes and the 0 length extra data. It should be done in a different PR though.

uint256 additionalRecipientsLength = CalldataPointer.wrap(
BasicOrder_additionalRecipients_length_cdPtr
).readUint256();

// Retrieve the length of additional recipients.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Retrieve the length of additional recipients.
// Retrieve the total original recipients length.

Copy link
Collaborator

Choose a reason for hiding this comment

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

can be fixed in a different PR or even this one.

@0age 0age changed the title WIP — update basic order flow to avoid allocating unnecessary memory Update basic order flow to avoid allocating unnecessary memory Mar 1, 2024
@0age
Copy link
Contributor Author

0age commented Mar 1, 2024

cc @Saw-mon-and-Natalie to fix the TODOs and comments in a follow-up PR

@0age 0age merged commit d9d3e59 into main Mar 1, 2024
4 checks passed
@0age 0age deleted the wip-basic-order-memory-optimization branch March 1, 2024 18:02
Comment on lines +1009 to +1022
// Set a length of 1 for the offer array.
mstore(add(eventDataPtr, 0x80), 1)

// Write itemType to the SpentItem struct.
mstore(add(eventDataPtr, 0xa0), offeredItemType)

// Copy calldata region with (offerToken, offerIdentifier,
// offerAmount) from OrderParameters to (token, identifier,
// amount) in SpentItem struct.
calldatacopy(
add(eventDataPtr, 0xc0),
BasicOrder_offerToken_cdPtr,
ThreeWords
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

note for myself: these are all shifted by 0x80 compared to the previous version because old eventConsiderationArrPtr used OrderFulfilled_offer_length_baseOffset = 0x200, whereas this eventDataPtr only uses OrderFulfilled_baseOffset = 0x180. The diff is the 0x80.
Then we write to 0x80+0, 0x80+0x20, 0x80+0x40

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