Skip to content

Introduce storage cache #3118

Closed
Closed
@chriseth

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions