Skip to content

Commit

Permalink
Adding benchmarking info to collections.md
Browse files Browse the repository at this point in the history
Adding near vs native collections benchmarking investigation links and some numbers from results.
  • Loading branch information
volodymyr-matselyukh authored Sep 23, 2024
1 parent 15fdd5c commit 28093d8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions docs/2.build/2.smart-contracts/anatomy/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> consumes 41% more gas compared to SDK IterableSet<i32>.

:::

:::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.

:::

Expand Down Expand Up @@ -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.
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.

0 comments on commit 28093d8

Please sign in to comment.