Skip to content

Commit

Permalink
Preallocate TritBuf in b1t6::encode (#835)
Browse files Browse the repository at this point in the history
* Preallocate `TritBuf` in `b1t6::encode`

* Update changelog

* Bump version and prepare release

* Update bee-ternary/CHANGELOG.md

* Make @thibault-martinez happy 😄

* Remove magic number

* Provide `.capacity()` method for `TritBuf`

* Rename `gen_buf`

* Basic `.capacity()` test case

* Update bee-ternary/CHANGELOG.md

Co-authored-by: Thibault Martinez <thibault.martinez.30@gmail.com>

* Respect `TRITS_PER_BYTE`

* Fix test cases

* Update changelog

* Getting rid of some more magic numbers

* Rename `BAL` to `BALANCE_DIFF`

* Also test for minimum `.capacity()` minimum

Co-authored-by: Thibault Martinez <thibault.martinez.30@gmail.com>
  • Loading branch information
grtlr and thibault-martinez committed Nov 16, 2021
1 parent c08f1ba commit cb131f1
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion bee-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ keywords = [ "iota", "tangle", "bee", "framework", "crypto" ]
homepage = "https://www.iota.org"

[dependencies]
bee-ternary = { version = "0.5.0", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }

byteorder = {version = "1.4.3", default-features = false }
lazy_static = {version = "1.4.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-message/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://www.iota.org"
[dependencies]
bee-common = { version = "0.5.0", path = "../bee-common/bee-common", default-features = false }
bee-pow = { version = "0.1.0", path = "../bee-pow", default-features = false }
bee-ternary = { version = "0.5.0", path = "../bee-ternary", default-features = false, features = [ "serde1" ] }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false, features = [ "serde1" ] }

bech32 = { version = "0.8.1", default-features = false }
bytemuck = { version = "1.7.2", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-pow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ homepage = "https://www.iota.org"

[dependencies]
bee-crypto = { version = "0.2.1-alpha", path = "../bee-crypto", default-features = false }
bee-ternary = { version = "0.5.0", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }

iota-crypto = { version = "0.7.0", default-features = false, features = [ "blake2b", "digest" ] }
thiserror = { version = "1.0.30", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-signing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://www.iota.org"
[dependencies]
bee-common-derive = { version = "0.1.1-alpha", path = "../bee-common/bee-common-derive", default-features = false }
bee-crypto = { version = "0.2.1-alpha", path = "../bee-crypto", default-features = false }
bee-ternary = { version = "0.5.0", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }

rand = { version = "0.8.4", default-features = false, features = [ "std", "std_rng" ] }
sha3 = { version = "0.9.1", default-features = false }
Expand Down
49 changes: 30 additions & 19 deletions bee-ternary/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,90 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.5.1 - 2021-11-16

### Added

- `.capacity()` for `TritBuf`;
- Expose `TRITS_PER_BYTE` for `RawEncoding`s;

### Changed

- Preallocate `TritBuf` in `b1t6::encode` for better performance;

## 0.5.0 - 2021-09-27

### Added

- `{RawEncodingBuf, TritBuf, T1B1Buf, T2B1Buf, T3B1Buf, T4B1Buf, T5B1Buf}::clear`.
- `{RawEncodingBuf, TritBuf, T1B1Buf, T2B1Buf, T3B1Buf, T4B1Buf, T5B1Buf}::clear`;

## 0.4.2-alpha - 2021-03-30

### Added

- `PartialOrd` and `Eq` implementations for `TritBuf`.
- `Eq` implementation for `Trits`.
- `PartialOrd` and `Eq` implementations for `TritBuf`;
- `Eq` implementation for `Trits`;

## 0.4.1-alpha - 2021-03-15

### Fixed

- B1T6 decoding.
- B1T6 decoding;

## 0.4.0-alpha - 2021-01-18

### Added

- B1T6 bytes-as-trits encoding and decoding support.
- B1T6 bytes-as-trits encoding and decoding support;

## 0.3.4-alpha - 2020-11-13

### Added

- Added proper `i128`/`u128` support detection.
- Added proper `i128`/`u128` support detection;

## 0.3.3-alpha - 2020-11-06

### Fixed

- `TryFrom<Trits>` implemented for `u128` and `i128` only when `cfg(has_i128)`.
- `TryFrom<Trits>` implemented for `u128` and `i128` only when `cfg(has_i128)`;

## 0.3.2-alpha - 2020-10-19

### Added

- `with_capacity` constructor for the buffers of every trit encoding.
- `with_capacity` constructor for the buffers of every trit encoding;

## 0.3.1-alpha - 2020-07-23

### Added

- Conversions between `&[Trit]` and `&Trits<T1B1<T>>`.
- Conversions between `&[Trit]` and `&Trits<T1B1<T>>`;

### Removed

- A useless conversion to same type.
- A useless conversion to same type;

## 0.3.0-alpha - 2020-07-20

### Added

- Support for arbitrary trit to numeric type conversion.
- Support for arbitrary trit to numeric type conversion;

## 0.2.0-alpha - 2020-07-17

### Added

- Binary/ternary numeric conversion.
- FromStr implementation for TryteBuf.
- TritBuf::from_i8s and TritBuf::from_u8s.
- Binary/ternary numeric conversion;
- FromStr implementation for TryteBuf;
- TritBuf::from_i8s and TritBuf::from_u8s;

## 0.1.0-alpha - 2020-06-12

### Added

- Efficient manipulation of ternary buffers (trits and trytes).
- Multiple encoding schemes.
- Extensible design that allows it to sit on top of existing data structures, avoiding unnecessary allocation and copying.
- An array of utility functions to allow for easy manipulation of ternary data.
- Zero-cost conversion between trit and tryte formats (i.e: no slower than the equivalent code would be if hand-written).
- Efficient manipulation of ternary buffers (trits and trytes);
- Multiple encoding schemes;
- Extensible design that allows it to sit on top of existing data structures, avoiding unnecessary allocation and copying;
- An array of utility functions to allow for easy manipulation of ternary data;
- Zero-cost conversion between trit and tryte formats (i.e: no slower than the equivalent code would be if hand-written);
2 changes: 1 addition & 1 deletion bee-ternary/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bee-ternary"
version = "0.5.0"
version = "0.5.1"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Ergonomic ternary manipulation utilities"
Expand Down
9 changes: 5 additions & 4 deletions bee-ternary/src/b1t6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use crate::{Btrit, RawEncoding, RawEncodingBuf, TritBuf, Trits, Tryte};

const TRITS_PER_BYTE: usize = 2;
const TRYTES_PER_BYTE: usize = 2;
const TRITS_PER_TRYTE: usize = 3;
const TRITS_PER_BYTE: usize = TRYTES_PER_BYTE * TRITS_PER_TRYTE;

/// An error that may be emitted when decoding a B1T6 trit slice.
#[derive(Debug)]
Expand All @@ -17,8 +18,8 @@ pub enum DecodeError {
pub fn decode(src: &Trits) -> Result<Vec<u8>, DecodeError> {
assert!(src.len() % TRITS_PER_BYTE == 0);
src.iter_trytes()
.step_by(2)
.zip(src[TRITS_PER_TRYTE..].iter_trytes().step_by(2))
.step_by(TRYTES_PER_BYTE)
.zip(src[TRITS_PER_TRYTE..].iter_trytes().step_by(TRYTES_PER_BYTE))
.map(|(a, b)| decode_group(a, b).ok_or(DecodeError::InvalidTrytes([a, b])))
.collect()
}
Expand All @@ -32,7 +33,7 @@ pub fn encode<T: RawEncodingBuf>(bytes: &[u8]) -> TritBuf<T>
where
T::Slice: RawEncoding<Trit = Btrit>,
{
let mut trits = TritBuf::new();
let mut trits = TritBuf::with_capacity(bytes.len() * TRITS_PER_BYTE);

for byte in bytes {
let (t1, t2) = encode_group(*byte);
Expand Down
5 changes: 5 additions & 0 deletions bee-ternary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,11 @@ impl<T: RawEncodingBuf> TritBuf<T> {
pub fn as_slice_mut(&mut self) -> &mut Trits<T::Slice> {
unsafe { &mut *(self.0.as_slice_mut() as *mut T::Slice as *mut Trits<T::Slice>) }
}

/// Returns the number of trits the `TritBuf` can hold without reallocating.
pub fn capacity(&self) -> usize {
self.0.capacity()
}
}

impl TritBuf<T3B1Buf> {
Expand Down
6 changes: 6 additions & 0 deletions bee-ternary/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub trait RawEncoding {
/// The trit buffer encoding associated with this trit slice encoding.
type Buf: RawEncodingBuf<Slice = Self>;

/// The number of trits that fit into this trit slice encoding.
const TRITS_PER_BYTE: usize;

/// Get an empty slice of this encoding
fn empty() -> &'static Self;

Expand Down Expand Up @@ -92,6 +95,9 @@ pub trait RawEncodingBuf {
/// View the trits in this buffer as a mutable slice.
fn as_slice_mut(&mut self) -> &mut Self::Slice;

/// Returns the number of trits the buffer can hold.
fn capacity(&self) -> usize;

/// Convert this encoding into another encoding.
/// TODO: Rename this `reencode`
#[allow(clippy::wrong_self_convention)]
Expand Down
8 changes: 8 additions & 0 deletions bee-ternary/src/t1b1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{trit::ShiftTernary, Btrit, RawEncoding, RawEncodingBuf, Trit};

use std::{hash, marker::PhantomData, ops::Range};

const TRITS_PER_BYTE: usize = 1;

/// An encoding scheme slice that uses a single byte to represent one trit.
#[repr(transparent)]
pub struct T1B1<T: Trit = Btrit> {
Expand Down Expand Up @@ -43,6 +45,8 @@ where
type Trit = T;
type Buf = T1B1Buf<T>;

const TRITS_PER_BYTE: usize = TRITS_PER_BYTE;

fn empty() -> &'static Self {
unsafe { &*Self::make(&[] as *const _, 0, 0) }
}
Expand Down Expand Up @@ -161,4 +165,8 @@ where
fn as_slice_mut(&mut self) -> &mut Self::Slice {
unsafe { &mut *(Self::Slice::make(self.0.as_ptr() as _, 0, self.0.len()) as *mut _) }
}

fn capacity(&self) -> usize {
self.0.capacity() * Self::Slice::TRITS_PER_BYTE
}
}
Loading

0 comments on commit cb131f1

Please sign in to comment.