[go: up one dir, main page]

Protocol Storage : Introduce local access to the indexed subcontext to optimize local context accesses for implicit contract cleaning.

This MR introduces Local module to the Map of the indexed subcontext in order to optimize consecutive local context accesses under a fixed directory.

The cleaning of the implicit account directory with 0 balance is optimized using this local accesses.

Idea of optimization for implicit account cleaning

The cleaning of an implicit account successively deletes files under the same directory of the account. However, the current implementation accesses each element by the absolute path. This is inefficient. For example it is like the following list of unix commands:

rm /data/contracts/index/0000448ef2e1fb58f6761e5fa9a40e9f03fe2bfd3265/balance
rm /data/contracts/index/0000448ef2e1fb58f6761e5fa9a40e9f03fe2bfd3265/manager
rm /data/contracts/index/0000448ef2e1fb58f6761e5fa9a40e9f03fe2bfd3265/counter

This MR replaces these removals via full paths by the relative path from the contract directory:

cd /data/contracts/index/0000448ef2e1fb58f6761e5fa9a40e9f03fe2bfd3265
rm ./balance
rm ./manager
rm ./counter

What this MR does

It may look an overkill but is required to execute the above optimization in a clean manner. Also it will be useful for other optimizations using local trees.

Local context

A new thpe Raw_context.local_context is introduced for the local access using the context tree. Like Raw_context.t, local_context has the gas fields to account the storage access cost using it.

Function with_local_context

Raw_context.with_local_context provides a wrapping function to use the local context. It takes a global context, the path for the tree to access locally, and a function to perform local access using the tree. It has a boolean option whether the final local tree is to be added back to the global context or not. If false is set, any modifications to the local tree are discarded. Whether the final tree is added back or not, the consumed gas in the local accesses is charged to the gas counter of the global context.

module Local

The MR introduces API functions for the relative access to storage created by Make_map functor in Indexed_raw_context. This allows relative access functions for many modules instantiated from the Make_map functor, such as the Storage.Contracts.Balance module.

Storage.Contracts.Balance.Local : sig
    type context = Raw_context.local_context

    val remove_existing : context -> context tzresult Lwt.t

    val remove : context -> context Lwt.t

    ...
  end

Example: optimization of Contract_storage.delete

As an example, Contract_storage.delete function is rewritten using these local access functions.

Gas

The code does not consume any gas. No need to worry about the gas change.

Benchmark

You can measure the speed using the following branch: https://gitlab.com/proof_ninja/tezos/-/tree/yoshihiro503@contract-deletion-ithaca.

dune exec ./src/proto_alpha/lib_protocol/test/unit/main.exe -- test Contract_storage && cat _build/_tests/protocol\ U+003E\ unit/*.output
[new]: 0.442880
[previous]: 0.611912

In my environment, the speedup is about x1.38.

Manual test

The MR is backported to Ithaca protocol at the branch https://gitlab.com/proof_ninja/tezos/-/tree/yoshihiro503@contract-deletion-ithaca . 10,000 blocks from Mainnet block level 2,400,000 are replayed and verified correctly with the optimization. No strange hash mismatch.

Checklist

  • Document the interface of any function added or modified (see the coding guidelines)
  • Document any change to the user interface, including configuration parameters (see node configuration)
  • Provide automatic testing (see the testing guide).
  • For new features and bug fixes, add an item in the appropriate changelog (docs/protocols/alpha.rst for the protocol and the environment, CHANGES.rst at the root of the repository for everything else).
  • Select suitable reviewers using the Reviewers field below.
  • Select as Assignee the next person who should take action on that MR
Edited by Mehdi Bouaziz

Merge request reports

Loading