Skip to content

Commit

Permalink
impl: shrink size of Inst
Browse files Browse the repository at this point in the history
By using a boxed slice instead of a vector, we can shrink the size
of the `Inst` structure by 8 bytes going from 40 to 32 bytes on
64-bit platforms.

PR #760
  • Loading branch information
marmeladema authored Apr 14, 2021
1 parent cc0f2c9 commit 6d95a6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,10 @@ impl InstHole {
Inst::EmptyLook(InstEmptyLook { goto: goto, look: look })
}
InstHole::Char { c } => Inst::Char(InstChar { goto: goto, c: c }),
InstHole::Ranges { ref ranges } => {
Inst::Ranges(InstRanges { goto: goto, ranges: ranges.clone() })
}
InstHole::Ranges { ref ranges } => Inst::Ranges(InstRanges {
goto: goto,
ranges: ranges.clone().into_boxed_slice(),
}),
InstHole::Bytes { start, end } => {
Inst::Bytes(InstBytes { goto: goto, start: start, end: end })
}
Expand Down
19 changes: 16 additions & 3 deletions src/prog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ impl<'a> IntoIterator for &'a Program {
///
/// Other than the benefit of moving invariants into the type system, another
/// benefit is the decreased size. If we remove the `Char` and `Ranges`
/// instructions from the `Inst` enum, then its size shrinks from 40 bytes to
/// 24 bytes. (This is because of the removal of a `Vec` in the `Ranges`
/// instructions from the `Inst` enum, then its size shrinks from 32 bytes to
/// 24 bytes. (This is because of the removal of a `Box<[]>` in the `Ranges`
/// variant.) Given that byte based machines are typically much bigger than
/// their Unicode analogues (because they can decode UTF-8 directly), this ends
/// up being a pretty significant savings.
Expand Down Expand Up @@ -374,7 +374,7 @@ pub struct InstRanges {
/// succeeds.
pub goto: InstPtr,
/// The set of Unicode scalar value ranges to test.
pub ranges: Vec<(char, char)>,
pub ranges: Box<[(char, char)]>,
}

impl InstRanges {
Expand Down Expand Up @@ -432,3 +432,16 @@ impl InstBytes {
self.start <= byte && byte <= self.end
}
}

#[cfg(test)]
mod test {
#[test]
#[cfg(target_pointer_width = "64")]
fn test_size_of_inst() {
use std::mem::size_of;

use super::Inst;

assert_eq!(32, size_of::<Inst>());
}
}

0 comments on commit 6d95a6f

Please sign in to comment.