Skip to content

Commit 6d95a6f

Browse files
authored
impl: shrink size of Inst
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
1 parent cc0f2c9 commit 6d95a6f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/compile.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,10 @@ impl InstHole {
927927
Inst::EmptyLook(InstEmptyLook { goto: goto, look: look })
928928
}
929929
InstHole::Char { c } => Inst::Char(InstChar { goto: goto, c: c }),
930-
InstHole::Ranges { ref ranges } => {
931-
Inst::Ranges(InstRanges { goto: goto, ranges: ranges.clone() })
932-
}
930+
InstHole::Ranges { ref ranges } => Inst::Ranges(InstRanges {
931+
goto: goto,
932+
ranges: ranges.clone().into_boxed_slice(),
933+
}),
933934
InstHole::Bytes { start, end } => {
934935
Inst::Bytes(InstBytes { goto: goto, start: start, end: end })
935936
}

src/prog.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ impl<'a> IntoIterator for &'a Program {
259259
///
260260
/// Other than the benefit of moving invariants into the type system, another
261261
/// benefit is the decreased size. If we remove the `Char` and `Ranges`
262-
/// instructions from the `Inst` enum, then its size shrinks from 40 bytes to
263-
/// 24 bytes. (This is because of the removal of a `Vec` in the `Ranges`
262+
/// instructions from the `Inst` enum, then its size shrinks from 32 bytes to
263+
/// 24 bytes. (This is because of the removal of a `Box<[]>` in the `Ranges`
264264
/// variant.) Given that byte based machines are typically much bigger than
265265
/// their Unicode analogues (because they can decode UTF-8 directly), this ends
266266
/// up being a pretty significant savings.
@@ -374,7 +374,7 @@ pub struct InstRanges {
374374
/// succeeds.
375375
pub goto: InstPtr,
376376
/// The set of Unicode scalar value ranges to test.
377-
pub ranges: Vec<(char, char)>,
377+
pub ranges: Box<[(char, char)]>,
378378
}
379379

380380
impl InstRanges {
@@ -432,3 +432,16 @@ impl InstBytes {
432432
self.start <= byte && byte <= self.end
433433
}
434434
}
435+
436+
#[cfg(test)]
437+
mod test {
438+
#[test]
439+
#[cfg(target_pointer_width = "64")]
440+
fn test_size_of_inst() {
441+
use std::mem::size_of;
442+
443+
use super::Inst;
444+
445+
assert_eq!(32, size_of::<Inst>());
446+
}
447+
}

0 commit comments

Comments
 (0)