Skip to content

Commit ec272cc

Browse files
committed
Replace symbol_with_update macros by const generics
After using compile-time generics instead of dyn dispatch for writers, the #[always(inline)] on symbol_with_update causes it to be inline for the long tail of larger CDF sizes. This results in larger binary size, for no speed benefit. Converting to const generics and removing inline preserves the specialization for smaller CDF sizes and reduces binary size by 40KiB on x86_64. The code is also more clear and concise.
1 parent ef9c3b7 commit ec272cc

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

src/context/cdf_context.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -557,16 +557,8 @@ macro_rules! symbol_with_update {
557557
}
558558
}
559559
};
560-
($self:ident, $w:ident, $s:expr, $cdf:expr, 2) => {
561-
$w.symbol_with_update_2($s, $cdf, &mut $self.fc_log);
562-
symbol_with_update!($self, $cdf);
563-
};
564-
($self:ident, $w:ident, $s:expr, $cdf:expr, 3) => {
565-
$w.symbol_with_update_3($s, $cdf, &mut $self.fc_log);
566-
symbol_with_update!($self, $cdf);
567-
};
568-
($self:ident, $w:ident, $s:expr, $cdf:expr, 4) => {
569-
$w.symbol_with_update_4($s, $cdf, &mut $self.fc_log);
560+
($self:ident, $w:ident, $s:expr, $cdf:expr, $cdf_len:expr) => {
561+
$w.symbol_with_update::<$cdf_len>($s, $cdf, &mut $self.fc_log);
570562
symbol_with_update!($self, $cdf);
571563
};
572564
}

src/ec.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ const EC_PROB_SHIFT: u32 = 6;
2828
const EC_MIN_PROB: u32 = 4;
2929
type ec_window = u32;
3030

31-
macro_rules! symbol_with_update_decl {($($n:expr),*) => {$(paste::item!{
32-
fn [<symbol_with_update_ $n>](
33-
&mut self, s: u32, cdf: &mut [u16; $n], log: &mut CDFContextLog,
34-
);
35-
})*}}
36-
3731
/// Public trait interface to a bitstream Writer: a Counter can be
3832
/// used to count bits for cost analysis without actually storing
3933
/// anything (using a new::WriterCounter() as a Writer), to record
@@ -49,10 +43,9 @@ pub trait Writer {
4943
/// leaves cdf unchanged
5044
fn symbol_bits(&self, s: u32, cdf: &[u16]) -> u32;
5145
/// Write a symbol s, using the passed in cdf reference; updates the referenced cdf.
52-
fn symbol_with_update(
53-
&mut self, s: u32, cdf: &mut [u16], log: &mut CDFContextLog,
46+
fn symbol_with_update<const CDF_LEN: usize>(
47+
&mut self, s: u32, cdf: &mut [u16; CDF_LEN], log: &mut CDFContextLog,
5448
);
55-
symbol_with_update_decl!(2, 3, 4);
5649
/// Write a bool using passed in probability
5750
fn bool(&mut self, val: bool, f: u16);
5851
/// Write a single bit with flat proability
@@ -489,14 +482,6 @@ impl WriterBase<WriterEncoder> {
489482
}
490483
}
491484

492-
macro_rules! symbol_with_update_impl {($($n:expr),*) => {$(paste::item!{
493-
fn [<symbol_with_update_ $n>](
494-
&mut self, s: u32, cdf: &mut [u16; $n], log: &mut CDFContextLog,
495-
) {
496-
self.symbol_with_update(s, cdf, log);
497-
}
498-
})*}}
499-
500485
/// Generic/shared implementation for Writers with StorageBackends (ie, Encoders and Recorders)
501486
impl<S> Writer for WriterBase<S>
502487
where
@@ -558,9 +543,8 @@ where
558543
/// The values must be monotonically non-decreasing, and the last value
559544
/// must be greater 32704. There should be at most 16 values.
560545
/// The lower 6 bits of the last value hold the count.
561-
#[inline(always)]
562-
fn symbol_with_update(
563-
&mut self, s: u32, cdf: &mut [u16], log: &mut CDFContextLog,
546+
fn symbol_with_update<const CDF_LEN: usize>(
547+
&mut self, s: u32, cdf: &mut [u16; CDF_LEN], log: &mut CDFContextLog,
564548
) {
565549
#[cfg(feature = "desync_finder")]
566550
{
@@ -573,7 +557,6 @@ where
573557

574558
update_cdf(cdf, s);
575559
}
576-
symbol_with_update_impl!(2, 3, 4);
577560
/// Returns approximate cost for a symbol given a cumulative
578561
/// distribution function (CDF) table and current write state.
579562
/// `s`: The index of the symbol to encode.

0 commit comments

Comments
 (0)