-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Improved aggregator Move documentation (#5659)
* [docs] Improved aggregator documentation * [docs] Typo fixes in aggregator documentation * [docs] addressed comments
- Loading branch information
1 parent
d085a3a
commit 2487961
Showing
7 changed files
with
132 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
aptos-move/framework/aptos-framework/sources/aggregator/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Design Rationale | ||
|
||
## Aggregators | ||
|
||
Aggregator is a parellizable integer that supports addition and subtraction. | ||
Unlike integer, aggregator has a user-defined `limit` which specifies when | ||
the value of aggregator overflows. Similarly to unsigned integers, the value | ||
of an aggregator underflows when going below zero. | ||
|
||
Both additions and subtractions are executed speculatively, and thus can be | ||
easily parallelized. On underflow or overflow, these operations are guaranteed | ||
to abort. Using these operations is encouraged. | ||
|
||
Reading an aggregator's value "materializes" the speculative value, and it | ||
can involve reading from the storage or checking for underflow or overflow. | ||
In general, using this operation is discouraged, or at least it should be used | ||
as rarely as possible. | ||
|
||
## Aggregator factory | ||
|
||
Unfortunately, aggregators cannot be part of a resource. At the moment, Move | ||
does not allow fine-grained access to resource fields, which ruins perfromance | ||
benefits aggregators can provide. In addition, getting the value of the field of | ||
a resource from storage is not possible without hardcoding the struct layout. | ||
For example, given a struct | ||
|
||
```move | ||
struct Foo<A> has key { | ||
a: A, | ||
b: u128, | ||
} | ||
``` | ||
|
||
there is no clean way of getting the value of `Foo::a` without knowing that the | ||
offset is 0. | ||
|
||
To mitigate the problem, we store aggregators as table items. Recall that every | ||
item stored in the table is uniquely identified by `(handle, key)` pair: `handle` | ||
identifies a table instance, and `key` identifies an item within the table. Now, | ||
if aggregator is a table item, it can be easily queried from storage and has a | ||
fine-grained access. | ||
|
||
To create an aggregator, one can use an `AggregatorFactory`. It is a resource | ||
which contains a single `phantom_table` field. When the factory is initialized, | ||
this field is used to generate a unique table `handle` which is passed to all | ||
new aggregators. When a new aggregator instance is created, it has a unique | ||
`key` which together with the `handle` is stored in `Aggregator` struct. |
Oops, something went wrong.