Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer bitfield decoding until its first use #803

Merged
merged 6 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/bitfield/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ unsigned-varint = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11.3"
ahash = "0.4"
encoding = { package = "forest_encoding", path = "../../encoding" }

[dev-dependencies]
rand_xorshift = "0.2.0"
Expand Down
5 changes: 4 additions & 1 deletion utils/bitfield/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

pub mod iter;
mod rleplus;
mod unvalidated;

pub use unvalidated::{UnvalidatedBitField, Validate};

use ahash::AHashSet;
use iter::{ranges_from_bits, RangeIterator};
Expand All @@ -13,7 +16,7 @@ use std::{

type Result<T> = std::result::Result<T, &'static str>;

/// An bit field with buffered insertion/removal that serializes to/from RLE+. Similar to
/// A bit field with buffered insertion/removal that serializes to/from RLE+. Similar to
/// `HashSet<usize>`, but more memory-efficient when long runs of 1s and 0s are present.
#[derive(Debug, Default, Clone)]
pub struct BitField {
Expand Down
67 changes: 67 additions & 0 deletions utils/bitfield/src/unvalidated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::{BitField, Result};
use encoding::serde_bytes;
use serde::{Deserialize, Deserializer, Serialize};

/// A trait for types that can produce a `&BitField` (or fail to do so).
/// Generalizes over `&BitField` and `&mut UnvalidatedBitField`.
pub trait Validate<'a> {
fn validate(self) -> Result<&'a BitField>;
}

impl<'a> Validate<'a> for &'a mut UnvalidatedBitField {
/// Validates the RLE+ encoding of the bit field, returning a shared
/// reference to the decoded bit field.
fn validate(self) -> Result<&'a BitField> {
self.validate_mut().map(|bf| &*bf)
}
}

impl<'a> Validate<'a> for &'a BitField {
fn validate(self) -> Result<&'a BitField> {
Ok(self)
}
}

/// A bit field that may not yet have been validated for valid RLE+.
/// Used to defer this validation step until when the bit field is
/// first used, rather than at deserialization.
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum UnvalidatedBitField {
Validated(BitField),
Unvalidated(#[serde(with = "serde_bytes")] Vec<u8>),
}

impl UnvalidatedBitField {
/// Validates the RLE+ encoding of the bit field, returning a unique
/// reference to the decoded bit field.
pub fn validate_mut(&mut self) -> Result<&mut BitField> {
if let Self::Unvalidated(bytes) = self {
*self = Self::Validated(BitField::from_bytes(&bytes)?);
}

match self {
Self::Validated(bf) => Ok(bf),
Self::Unvalidated(_) => unreachable!(),
}
}
}

impl From<BitField> for UnvalidatedBitField {
fn from(bf: BitField) -> Self {
Self::Validated(bf)
}
}

impl<'de> Deserialize<'de> for UnvalidatedBitField {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes: Vec<u8> = serde_bytes::deserialize(deserializer)?;
Ok(Self::Unvalidated(bytes))
}
}
16 changes: 8 additions & 8 deletions vm/actor/src/builtin/miner/deadline_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl Deadline {
store: &BS,
sectors: &Sectors<'_, BS>,
epoch: ChainEpoch,
partition_sectors: &PartitionSectorMap,
partition_sectors: &mut PartitionSectorMap,
sector_size: SectorSize,
quant: QuantSpec,
) -> Result<PowerPair, Box<dyn StdError>> {
Expand Down Expand Up @@ -611,7 +611,7 @@ impl Deadline {
sector_size: SectorSize,
quant: QuantSpec,
fault_expiration_epoch: ChainEpoch,
partition_sectors: &PartitionSectorMap,
partition_sectors: &mut PartitionSectorMap,
) -> Result<PowerPair, Box<dyn StdError>> {
let mut partitions = self.partitions_amt(store)?;

Expand Down Expand Up @@ -687,8 +687,8 @@ impl Deadline {
store: &BS,
sectors: &Sectors<'_, BS>,
sector_size: SectorSize,
partition_sectors: &PartitionSectorMap,
) -> Result<(), ActorError> {
partition_sectors: &mut PartitionSectorMap,
) -> Result<(), Box<dyn StdError>> {
let mut partitions = self.partitions_amt(store)?;

for (partition_idx, sector_numbers) in partition_sectors.iter() {
Expand All @@ -705,7 +705,7 @@ impl Deadline {

partition
.declare_faults_recovered(sectors, sector_size, sector_numbers)
.map_err(|e| e.wrap("failed to add recoveries"))?;
.map_err(|e| e.downcast_wrap("failed to add recoveries"))?;

partitions.set(partition_idx, partition).map_err(|e| {
e.downcast_default(
Expand Down Expand Up @@ -870,7 +870,7 @@ impl Deadline {
sector_size: SectorSize,
quant: QuantSpec,
fault_expiration: ChainEpoch,
post_partitions: &[PoStPartition],
post_partitions: &mut [PoStPartition],
) -> Result<PoStResult, Box<dyn StdError>> {
let mut partitions = self.partitions_amt(store)?;

Expand Down Expand Up @@ -905,7 +905,7 @@ impl Deadline {
sector_size,
quant,
fault_expiration,
&post.skipped,
&mut post.skipped,
)
.map_err(|e| {
e.wrap(format!(
Expand Down Expand Up @@ -994,7 +994,7 @@ impl Deadline {
store: &BS,
sectors: &Sectors<'_, BS>,
expiration: ChainEpoch,
partition_sectors: &PartitionSectorMap,
partition_sectors: &mut PartitionSectorMap,
sector_size: SectorSize,
quant: QuantSpec,
) -> Result<(), Box<dyn StdError>> {
Expand Down
Loading