joshua | Flow (2023-11-06)
I have some questions about Cadence performance. If I have a large dictionary in account storage and I want to read one of its values, is it cheaper to copy the dictionary from storage and then read the value at a specific key, or to borrow a reference to it from storage and then read the value for the key through the reference? Or is it the same?
14 Replies
I've created a thread for your message. Please continue any relevant discussion in this thread.
You can rename this thread using
/title <new title>
If this is a technical question that others may benefit from, considering also asking it on Stackoverflow: https://stackoverflow.com/questions/ask?tags=onflow-cadenceUnknown User•14mo ago
Message Not Public
Sign In & Join Server To View
like how much cheaper? If I have a dictionary with 1000 key-value pairs, what is the cost difference between loading and accessing a value and borrowing and accessing a value
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Loading, i.e. moving (resource) or copying (struct), is always expensive, because data is physically copied from storage. Borrowing is always the cheapest
https://developers.flow.com/cadence/design-patterns#avoid-excessive-load-and-save-storage-operations-prefer-in-place-mutations
yeah, I am just curious what the difference is
As in performance, or functionality?
performance. like if I have a dictionary of 1000 resources
We're trying to solve some performance issues in the staking contract and I am trying to see which operations are causing the most problems because there are multiple candidates
(I left a relevant comment here BTW: https://github.com/onflow/flow-core-contracts/pull/379#discussion_r1385267189)
thank you
the more data the dictionary (or any other container, like an array) has, the more expensive the load/save approach becomes. if you only have a small dictionary, then it's likely unnoticeable
the borrow/reference approach is accessing the data directly, in storage. there is no need (and a lot of unnecessary overhead) for copying all the data, modifying it, and then copying it back
It would be nice to expose some performance details when running code in the Emulator, e.g. from a transaction/script, and also the test framework. We already have coverage data, but exposing metering data would allow identifying such performance hotspots
cc @mpeter
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Why not? That the serialization of the data is delayed until the end makes it a bit harder to see where the cause is for that, but on it's own the transfers are metered, so should show up when looking at metering data
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View