2

I have an extrinsic with multiple calls to other pallet functions. As it calls other internal functions, more than one of them access the same storage value (either an actual StorageValue or the same key in a StorageMap).

Will this add to the total weight of the extrinsic, i.e., is there an additional cost to reading a storage value more than once? Should I refactor my code to read the storage value once and pass it around to avoid re-reading it?

1
  • 1
    Not sure how the cache works, but my practice is to minimize the read and write operations and count all of them.
    – h4x3rotab
    Apr 12, 2022 at 6:06

1 Answer 1

2

My understanding is that if your reading the same value multiple times in the same transaction then the read should be cached and you only pay for it once in weight. I think the same is true if you write to the same location multiple times in a transaction - it would only save the last write (and only charge you for one write).

3
  • That's interesting. Is there a way I can test that (e.g., with multiple reads) to make sure?
    – Angelo
    Apr 10, 2022 at 9:20
  • 2
    Maybe, the simplest way to test is to benchmark it, and do a A/B test, such as just add another 10 times call to read the same storage(and key), and check the benchmark value. Apr 11, 2022 at 14:40
  • I don't think there's "pay" or "charge" in an extrinsic at all. All you can do is to benchmark the extrinsic, and assign a value to the weight for it. Depends on if you enable the state cache or not, you may get different benchmark result.
    – h4x3rotab
    Apr 12, 2022 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.