2

I was wondering about the number of reads and writes when draining a StorageMap which consists of n items. Is the answer (n, 1) or (n, n)?

2 Answers 2

2

I'm not professional on this.

But these are what I found.

https://github.dev/paritytech/substrate/blob/cb63d3dafc986faf5f2181288737ba8513253a02/frame/support/src/storage/mod.rs#L900

next_key cost 1 read. kill cost 1 write.

The kill interacts with the storage overlay. I'm not sure if there is any optimization.

Basically, (n, n).

1

(n, n) You have to iterate all the storage and perform and update on each element (In your case removing the element).

Not sure if you are implementing it yourself, but you have a function drain() that does that.

let mut drained_keys = MyStorageMap::iter_keys().drain().collect::<Vec<_>>();

Your Answer

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

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