Skip to content

Commit

Permalink
Use serialized_size to allocate serialization buffer (#1945)
Browse files Browse the repository at this point in the history
Merge pull request #1945

Use serialized_size to allocate serialization buffer
  • Loading branch information
CjS77 committed Jun 4, 2020
2 parents 139117a + c128725 commit f22ba34
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions infrastructure/storage/src/lmdb_store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl LMDBDatabase {
let tx = WriteTransaction::new(env)?;
{
let mut accessor = tx.access();
let buf = LMDBWriteTransaction::convert_value(value, 512)?;
let buf = LMDBWriteTransaction::convert_value(value)?;
accessor.put(&*self.db, key, &buf, put::Flags::empty())?;
}
tx.commit().map_err(LMDBError::from)
Expand Down Expand Up @@ -613,7 +613,7 @@ impl<'txn, 'db: 'txn> LMDBWriteTransaction<'txn, 'db> {
K: AsLmdbBytes + ?Sized,
V: serde::Serialize,
{
let buf = LMDBWriteTransaction::convert_value(value, 512)?;
let buf = Self::convert_value(value)?;
self.access.put(&self.db, key, &buf, put::Flags::empty())?;
Ok(())
}
Expand All @@ -631,9 +631,10 @@ impl<'txn, 'db: 'txn> LMDBWriteTransaction<'txn, 'db> {
Ok(self.access.del_key(&self.db, key)?)
}

fn convert_value<V>(value: &V, size_estimate: usize) -> Result<Vec<u8>, LMDBError>
fn convert_value<V>(value: &V) -> Result<Vec<u8>, LMDBError>
where V: serde::Serialize {
let mut buf = Vec::with_capacity(size_estimate);
let size = bincode::serialized_size(value).map_err(|e| LMDBError::SerializationErr(e.to_string()))?;
let mut buf = Vec::with_capacity(size as usize);
bincode::serialize_into(&mut buf, value).map_err(|e| LMDBError::SerializationErr(e.to_string()))?;
Ok(buf)
}
Expand Down

0 comments on commit f22ba34

Please sign in to comment.