Skip to content

Commit

Permalink
Adding benchmarking info to collections.md (#2236)
Browse files Browse the repository at this point in the history
* Adding benchmarking info to collections.md

Adding near vs native collections benchmarking investigation links and some numbers from results.

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

* fix collections.md

---------

Co-authored-by: Guille <[email protected]>
  • Loading branch information
volodymyr-matselyukh and gagdiez authored Oct 2, 2024
1 parent ae49b7d commit 4f33088
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions docs/2.build/2.smart-contracts/anatomy/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ You can choose between two types of collections:

Understanding how the contract stores and loads both types of collections is crucial to decide which one to use.

:::tip
<details>

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
<summary> Native vs SDK Collections </summary>

:::
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. [This user test](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>`.

</details>

<details>

:::info How the State is Handled
<summary> How the State is Handled </summary>

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.

</details>

---

Expand Down Expand Up @@ -599,4 +609,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 4f33088

Please sign in to comment.