Module near_sdk::collections
source · Expand description
Collections that offer an alternative to standard containers from std::collections::*
by
utilizing the underlying blockchain trie storage more efficiently.
The updated version of this module lives in near_sdk::store
,
where the data structures are more optimized and have a closer API to std::collections
.
For example, the following smart contract does not work with state efficiently, because it will
load the entire HashMap
at the beginning of the contract call, and will save it entirely at
the end, in cases when there is state modification. This is fine for small number of elements,
but very inefficient for large numbers.
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
records: HashMap<String, String>,
}
The following is an efficient alternative. It will load each element individually only when it is read and will save it only when it is written/removed.
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
records: LookupMap<String, String>,
}
The efficiency of LookupMap
comes at the cost, since it has fewer methods than HashMap
and is not
that seemlessly integrated with the rest of the Rust standard library.
Re-exports
pub use vector::Vector;
pub use unordered_map::UnorderedMap;
Modules
std::collections::HashMap
the keys in this map are not
hashed but are instead serialized.