Closed
Description
Suggested by @puellavulnerata - it might be a good idea to cache storage access in memory.
A simple cache based on a conflict free hash table in memory could already provide a gigantic benefit. Storage read has been changed from 50 to 200 gas since Solidity was designed (actually memory costs were assumed to be much higher at that point, too), so this might be feasile.
Storage reads could be replaced by the following code:
function sloadCached(key) -> value
{
// Cache consists of a sequence of 256 key-value pairs,
// where least significant byte determines position in cache.
let cachePos := add(0x100, mul(and(key, 0xff), 0x40))
if eq(mload(cachePos), key) {
value := mload(add(cachePos, 0x20))
} else {
value := sload(key)
mstore(cachePos, key)
mstore(add(cachePos, 0x20), value)
}
}
Storage writes have to invalidate the cache. Furthermore, external function calls have to clear the full cache.
Metadata
Assignees
Labels
No labels