diff --git a/benchmarks/io_chunks/canbench_results.yml b/benchmarks/io_chunks/canbench_results.yml index 6c2e9160..b9b59d80 100644 --- a/benchmarks/io_chunks/canbench_results.yml +++ b/benchmarks/io_chunks/canbench_results.yml @@ -44,42 +44,42 @@ benches: read_chunks_vec_1: total: calls: 1 - instructions: 104859760 + instructions: 104859642 heap_increase: 0 stable_memory_increase: 0 scopes: {} read_chunks_vec_1k: total: calls: 1 - instructions: 105826498 + instructions: 105511618 heap_increase: 0 stable_memory_increase: 0 scopes: {} read_chunks_vec_1m: total: calls: 1 - instructions: 1010905944 + instructions: 696002618 heap_increase: 0 stable_memory_increase: 0 scopes: {} write_chunks_btreemap_1: total: calls: 1 - instructions: 357205397 + instructions: 360207245 heap_increase: 13 stable_memory_increase: 1536 scopes: {} write_chunks_btreemap_1k: total: calls: 1 - instructions: 4187119879 + instructions: 4194582593 heap_increase: 2 stable_memory_increase: 1536 scopes: {} write_chunks_btreemap_1m: total: calls: 1 - instructions: 83659829857 + instructions: 83673829307 heap_increase: 0 stable_memory_increase: 3072 scopes: {} @@ -107,21 +107,21 @@ benches: write_chunks_vec_1: total: calls: 1 - instructions: 549903573 + instructions: 549903446 heap_increase: 0 stable_memory_increase: 1536 scopes: {} write_chunks_vec_1k: total: calls: 1 - instructions: 562257515 + instructions: 562132513 heap_increase: 0 stable_memory_increase: 1536 scopes: {} write_chunks_vec_1m: total: calls: 1 - instructions: 1896593101 + instructions: 1771593099 heap_increase: 0 stable_memory_increase: 1536 scopes: {} diff --git a/benchmarks/vec/canbench_results.yml b/benchmarks/vec/canbench_results.yml index 7c952c1b..d63e9d34 100644 --- a/benchmarks/vec/canbench_results.yml +++ b/benchmarks/vec/canbench_results.yml @@ -2,112 +2,112 @@ benches: vec_get_blob_128: total: calls: 1 - instructions: 19246661 + instructions: 18706659 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_16: total: calls: 1 - instructions: 6405945 + instructions: 5897185 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_32: total: calls: 1 - instructions: 7123504 + instructions: 6654118 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_4: total: calls: 1 - instructions: 4824326 + instructions: 4284326 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_4_mem_manager: total: calls: 1 - instructions: 7191676 + instructions: 5891676 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_64: total: calls: 1 - instructions: 11310943 + instructions: 10751237 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_64_mem_manager: total: calls: 1 - instructions: 13651091 + instructions: 12331385 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_blob_8: total: calls: 1 - instructions: 5723200 + instructions: 5213200 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_get_u64: total: calls: 1 - instructions: 4790305 + instructions: 3530307 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_insert_blob_128: total: calls: 1 - instructions: 4171424 + instructions: 3821424 heap_increase: 0 stable_memory_increase: 19 scopes: {} vec_insert_blob_16: total: calls: 1 - instructions: 3336227 + instructions: 2986227 heap_increase: 0 stable_memory_increase: 2 scopes: {} vec_insert_blob_32: total: calls: 1 - instructions: 3455467 + instructions: 3105467 heap_increase: 0 stable_memory_increase: 5 scopes: {} vec_insert_blob_4: total: calls: 1 - instructions: 3247468 + instructions: 2897468 heap_increase: 0 stable_memory_increase: 0 scopes: {} vec_insert_blob_64: total: calls: 1 - instructions: 3695804 + instructions: 3345804 heap_increase: 0 stable_memory_increase: 9 scopes: {} vec_insert_blob_8: total: calls: 1 - instructions: 3276889 + instructions: 2926889 heap_increase: 0 stable_memory_increase: 1 scopes: {} vec_insert_u64: total: calls: 1 - instructions: 5379519 + instructions: 4999519 heap_increase: 0 stable_memory_increase: 1 scopes: {} diff --git a/src/base_vec.rs b/src/base_vec.rs index 183b89a3..78cdb4b6 100644 --- a/src/base_vec.rs +++ b/src/base_vec.rs @@ -37,6 +37,7 @@ use crate::{ Memory, Storable, }; use std::borrow::{Borrow, Cow}; +use std::cell::Cell; use std::cmp::min; use std::fmt; use std::marker::PhantomData; @@ -95,6 +96,7 @@ impl std::error::Error for InitError {} pub struct BaseVec { memory: M, + cached_len: Cell, _marker: PhantomData, } @@ -117,6 +119,7 @@ impl BaseVec { Self::write_header(&header, &memory)?; Ok(Self { memory, + cached_len: Cell::new(0), _marker: PhantomData, }) } @@ -148,6 +151,7 @@ impl BaseVec { Ok(Self { memory, + cached_len: Cell::new(header.len), _marker: PhantomData, }) } @@ -178,7 +182,7 @@ impl BaseVec { /// /// Complexity: O(1) pub fn len(&self) -> u64 { - read_u64(&self.memory, Address::from(LEN_OFFSET)) + self.cached_len.get() } /// Sets the item at the specified index to the specified value. @@ -264,6 +268,7 @@ impl BaseVec { /// Sets the vector's length. fn set_len(&self, new_len: u64) { write_u64(&self.memory, Address::from(LEN_OFFSET), new_len); + self.cached_len.set(new_len); } /// Writes the size of the item at the specified offset.