Understanding Confirmation about `Vectors on the Heap` and `Storage Vectors`

From my understanding,
Vectors on the Heap puts data next to each other similar to how virtual memory works in modern memory computer architecture.
Storage Vectors are more like what actually happened in physical memory where neighbor data are not been stored in memory with consecutive keys.

In terms of usage,
StorageVec<T> can only be used in a contract because only contracts are allowed to access persistent storage.

Is the above understanding about Vectors on the Heap and Storage Vectors correct? If there are deeper explanations happy to learn!

3 Likes

Your overall understanding is correct. Indeed, elements of a StorageVec are not consecutive and they live in persistent storage. The closest thing to storage in a computer is the hard drive actually. And yes, only contracts are allowed to use StorageVec while Vec is allowed in any program type.

StorageVec represent a mapping between the integers 0, 1, 2, … and storage slots, and those slots are not consecutive in the current implementation. The key for each of those slots is computed as the hash of some function of each integer.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.