Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Stylus Cache Manager #36

Merged
merged 21 commits into from
Apr 15, 2024
Merged

Stylus Cache Manager #36

merged 21 commits into from
Apr 15, 2024

Conversation

rachel-bousfield
Copy link
Contributor

This PR introduces new precompiles to support program & trie slot caching

@rachel-bousfield rachel-bousfield changed the title Stylus Cache Precompiles Stylus Cache Manager Apr 11, 2024
@rachel-bousfield rachel-bousfield merged commit 133fdc8 into stylus Apr 15, 2024
8 of 10 checks passed
@rachel-bousfield rachel-bousfield deleted the init-cache branch April 15, 2024 05:02
@@ -47,7 +47,8 @@
"@offchainlabs/upgrade-executor": "1.1.0-beta.0",
"@openzeppelin/contracts": "4.5.0",
"@openzeppelin/contracts-upgradeable": "4.5.2",
"patch-package": "^6.4.7"
"patch-package": "^6.4.7",
"solady": "^0.0.182"
Copy link
Member

Choose a reason for hiding this comment

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

prefer to pin contract dependency instead of have it floating
or if we are only using 1 file we can copy it to this repo

Comment on lines +33 to +34
event InsertBid(uint256 bid, bytes32 indexed codehash, uint64 size);
event DeleteBid(uint256 bid, bytes32 indexed codehash, uint64 size);
Copy link
Member

Choose a reason for hiding this comment

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

would be nice to have indexed fields first

/// Evicts entries until enough space exists in the cache, reverting if payment is insufficient.
/// Returns the new amount of space available on success.
/// Note: will only make up to 5Mb of space. Call repeatedly for more.
function makeSpace(uint64 size) external payable returns (uint64 space) {
Copy link
Member

Choose a reason for hiding this comment

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

this is callable when paused, not sure if its expected

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 wasn't necessary for correctness (the point of pausing is to eventually evict everything), but we may as well just let the owner do everything in this case

index = uint64(entries.length);

uint256 min;
while (queueSize + size > cacheSize) {
Copy link
Member

Choose a reason for hiding this comment

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

cacheSize can be cached outside the loop to save some SLOAD

Comment on lines +38 to +41
struct Entry {
bytes32 code;
uint64 size;
}
Copy link
Member

Choose a reason for hiding this comment

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

we don't need this struct and can save 1 storage slot if we remove size here, and instead call _asmSize(code) when we need the size when removing a bid

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 a very reasonable suggestion, but it turns out _asmSize does an SLOAD in ArbOS so it's a bit cheaper to keep the struct :)


/// Creates a packed bid item
function _packBid(uint256 bid, uint64 index) internal pure returns (uint256) {
return (bid << 64) | uint256(index);
Copy link
Member

Choose a reason for hiding this comment

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

this is unlikely to be reachable unless misconfigured decay, but we should check for bid < type(uint192).max before shifting or might as well use uint192 for all bid values

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 the math to use 192-bit numbers, but this shouldn't be reachable since decay is 64-bit

Comment on lines +56 to +63
function setCacheSize(uint64 newSize) external onlyOwner {
cacheSize = newSize;
}

/// Sets the intended decay factor. Does not modify existing bids.
function setDecayRate(uint64 newDecay) external onlyOwner {
decay = newDecay;
}
Copy link
Member

Choose a reason for hiding this comment

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

can add event

Comment on lines +122 to +124
if (size > MAX_MAKE_SPACE) {
size = MAX_MAKE_SPACE;
}
Copy link
Member

Choose a reason for hiding this comment

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

would prefer revert instead of capping if it requested to make more space than allowed

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

Successfully merging this pull request may close these issues.

2 participants