From 28093d81fee5adca608ca1b17ee1074de97269ca Mon Sep 17 00:00:00 2001 From: volodymyr-matselyukh <99895600+volodymyr-matselyukh@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:35:51 +0200 Subject: [PATCH] Adding benchmarking info to collections.md Adding near vs native collections benchmarking investigation links and some numbers from results. --- docs/2.build/2.smart-contracts/anatomy/collections.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/2.build/2.smart-contracts/anatomy/collections.md b/docs/2.build/2.smart-contracts/anatomy/collections.md index 61859ca936a..996c42894ce 100644 --- a/docs/2.build/2.smart-contracts/anatomy/collections.md +++ b/docs/2.build/2.smart-contracts/anatomy/collections.md @@ -17,13 +17,15 @@ Understanding how the contract stores and loads both types of collections is cru :::tip -Use native collections for small amounts of data that need to be accessed all together, and SDK collections for large amounts of data that do not need to be accessed all together +Use native collections for small amounts of data that need to be accessed all together, and SDK collections for large amounts of data that do not need to be accessed all together. +If your collection has up to 100 entries, it's acceptable to use the native collection, as it might be simpler since you don't have to manage prefixes as we do with SDK collections. +However, if your collection has 1,000 or more entries, it's better to use SDK collection. The [investigation](https://github.com/volodymyr-matselyukh/near-benchmarking) shows that running the `contains` method on a native HashSet consumes 41% more gas compared to SDK IterableSet. ::: :::info How the State is Handled -Each time the contract is executed, the first thing it will do is to read the values and [deserialize](./serialization.md) them into memory, and after the function finishes, it will [serialize](./serialization.md) and write the values back to the database. +Each time the contract is executed, the first thing it will do is to read the values and [deserialize](./serialization.md) them into memory, and after the function finishes, it will [serialize](./serialization.md) and write the values back to the database. That means the contract will load your native collections fully into memory before the contract's method execution. The method you invoke may not even use the loaded collection. This will have impact on GAS you spend for methods in your contract. So, using native collection which will have more than 100 entries as the top level property of your contract is a bad practice. ::: @@ -599,4 +601,4 @@ For storing data on-chain it’s important to keep in mind the following: Let’s say for example, someone wants to put an NFT purely on-chain (rather than IPFS or some other decentralized storage solution) you’ll have almost an unlimited amount of storage but will have to pay 1 $NEAR per 100kb of storage used. -Users will be limited to 4MB per contract call upload due to MAX_GAS constraints. The maximum amount of gas one can attach to a given functionCall is 300TGas. \ No newline at end of file +Users will be limited to 4MB per contract call upload due to MAX_GAS constraints. The maximum amount of gas one can attach to a given functionCall is 300TGas.