Skip to content

Commit dc6d0e5

Browse files
committed
adding SmartTable sections at the end
1 parent 55c81dd commit dc6d0e5

File tree

1 file changed

+29
-5
lines changed
  • apps/nextra/pages/en/build/smart-contracts

1 file changed

+29
-5
lines changed

apps/nextra/pages/en/build/smart-contracts/maps.mdx

+29-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Breaking down the gas costs further, we have:
1616
2. IO gas costs —generally much lower— which depend on the number and size of resources read and modified.
1717
3. execution gas costs are based on the computation needed, and are generally in the similar scale as io gas costs.
1818

19-
Transactions that modify the same **slot** cannot be executed in parallel (with some exceptions, like aggregators and resources as a part of the same resource group), as they conflict with one another.
19+
Transactions that modify the same **slot** cannot be executed concurrently (with some exceptions, like aggregators and resources as a part of the same resource group), as they conflict with one another.
2020

2121
One useful analogy is thinking about each **slot** being a file on a disk,
2222
then performance of smart contract would correlate well to a program that
@@ -27,9 +27,9 @@ operates on files in the same way.
2727
| Implementation | Size Limit | Storage Structure | Key Features |
2828
|--------------------|------------|------------------|--------------|
2929
| `OrderedMap` | Bounded (fits in a single **slot**) | Stored entirely within the resource that contains it | Supports ordered access (front/back, prev/next), implemented as sorted vector, but operations are effectively O(log(n)) due to internal optimizations |
30-
| `Table` | Unbounded | Each (key, value) stored in a separate **slot** | Supports basic operations, like `add`, `remove`, `contains`, but **not iteration**, and **cannot be destroyed**; useful for large/unbounded keys/values and high-parallelism cases |
31-
| `TableWithLength` | Unbounded | same as `Table` | Variant of `Table`, with additional length tracking, which adds `length`, `empty`, and `destroy_empty` methods; Adding or removing elements **cannot** be done in parallel, modifying existing elements can. |
32-
| `BigOrderedMap` | Unbounded | Combines multiple keys into a single **slot**, initially stored within resource that contains it, and grows into multiple **slots** dynamically | Implemented as B+ tree; **opportunistically parallel** for non-adjacent keys; supports ordered access (front/back, prev/next); configurable node capacities to balance storage and performance |
30+
| `Table` | Unbounded | Each (key, value) stored in a separate **slot** | Supports basic operations, like `add`, `remove`, `contains`, but **not iteration**, and **cannot be destroyed**; useful for large/unbounded keys/values and where high-concurrency is needed |
31+
| `TableWithLength` | Unbounded | same as `Table` | Variant of `Table`, with additional length tracking, which adds `length`, `empty`, and `destroy_empty` methods; Adding or removing elements **cannot** be done concurrently, modifying existing elements can. |
32+
| `BigOrderedMap` | Unbounded | Combines multiple keys into a single **slot**, initially stored within resource that contains it, and grows into multiple **slots** dynamically | Implemented as B+ tree; **opportunistically concurrent** for non-adjacent keys; supports ordered access (front/back, prev/next); configurable node capacities to balance storage and performance |
3333

3434
Note:
3535
- `SimpleMap` has been deprecated, and replaced with `OrderedMap`.
@@ -140,7 +140,7 @@ Its current implementation is B+ tree, which is chosen as it is best suited for
140140
Implementation has few characteristics that make it very versatile and useful across wide range of usecases:
141141

142142
- When it has few elements, it stores all of them within the resource that contains it, providing comparable performance to OrderedMap itself, while then dynamically growing to multiple resources as more and more elements are added
143-
- It reduces amount of conflicts: modifications to a different part of the key-space are generally parallel, and it provides knobs for tuning between parallelism and size
143+
- It reduces amount of conflicts: modifications to a different part of the key-space can be generally done concurrently, and it provides knobs for tuning between concurrency and size
144144
- All operations have guaranteed upper-bounds on performance (how long they take, as well as how much execution and io gas they consume), allowing for safe usage across a variety of use cases.
145145
- One caveat, is refundable storage fee. By default, operation that requires map to grow to more resources needs to pay for storage fee for it. Implementation here has an option to pre-pay for storage slots, and to reuse them as elements are added/removed, allowing applications to achieve fully predictable overall gas charges, if needed.
146146
- If key/value is within the size limits map was configured with, inserts will never fail unpredictably, as map internally understands and manages maximal **slot** size limits.
@@ -166,3 +166,27 @@ Because it's layout affects what can be inserted and performance, there are a fe
166166
- [table.move](https://github.com/aptos-labs/aptos-core/blob/6f5872b567075fe3615e1363d35f89dc5eb45b0d/aptos-move/framework/aptos-stdlib/sources/table.move)
167167
- [table_with_length.move](https://github.com/aptos-labs/aptos-core/blob/6f5872b567075fe3615e1363d35f89dc5eb45b0d/aptos-move/framework/aptos-stdlib/sources/table.move)
168168
- [big_ordered_map.move](https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-framework/sources/datastructures/big_ordered_map.move)
169+
170+
## Additional details of (deprecated) SmartTable
171+
172+
The Smart Table is a scalable hash table implementation based on linear hashing.
173+
This data structure aims to optimize storage and performance by utilizing linear hashing, which splits one bucket at a time instead of doubling the number of buckets, thus avoiding unexpected gas costs.
174+
Unfortunatelly, it's implementation makes every addition/removal be a conflict, making such transactions fully sequential.
175+
The Smart Table uses the SipHash function for faster hash computations while tolerating collisions. Unfortunatelly, this also means that collisions are predictable, which means that if end users can control the keys being inserted, it can have large number of collisions in a single bucket.
176+
177+
### SmartTable Structure
178+
179+
The `SmartTable` struct is designed to handle dynamic data efficiently:
180+
181+
- `buckets`: A table with a length that stores vectors of entries.
182+
- `num_buckets`: The current number of buckets.
183+
- `level`: The number of bits representing `num_buckets`.
184+
- `size`: The total number of items in the table.
185+
- `split_load_threshold`: The load threshold percentage that triggers bucket splits.
186+
- `target_bucket_size`: The target size of each bucket, which is not strictly enforced.
187+
188+
### SmartTable usage examples
189+
190+
- [Move Spiders Smart Table](https://movespiders.com/courses/modules/datastructures/lessonId/7)
191+
- [Move Spiders Querying Smart Table via FullNode APIs](https://movespiders.com/courses/modules/datastructures/lessonId/9)
192+
- [Move Spiders Querying Smart Table via View Function](https://movespiders.com/courses/modules/datastructures/lessonId/10)

0 commit comments

Comments
 (0)