1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#[derive(Default, Display, Add, AddAssign, Into, From, Clone, Copy)]
pub struct Bytes(usize);

impl std::iter::Sum for Bytes {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Bytes>,
    {
        iter.fold(Default::default(), std::ops::Add::add)
    }
}

/// A newtype for `usize` used to count the number of instances of some item.
#[derive(
    Default,
    Display,
    Serialize,
    Deserialize,
    From,
    Into,
    Add,
    AddAssign,
    Clone,
    Copy,
    PartialOrd,
    Ord,
    PartialEq,
    Eq,
)]
pub struct Instances(usize);
impl Instances {
    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }
}

impl std::iter::Sum for Instances {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Instances>,
    {
        iter.fold(Default::default(), std::ops::Add::add)
    }
}

pub struct BytesAndInstances {
    bytes: Bytes,
    instances: Instances,
}
impl BytesAndInstances {
    pub fn new(bytes: Bytes, instances: Instances) -> Self {
        BytesAndInstances { bytes, instances }
    }
}

/// A macro used to generate code that will operate on all fields of a `PerUserExtensibleKind`.
#[macro_export]
macro_rules! for_field_in_user_extensible {
    ( $cb: ident ) => {
        $cb!(
            (floats, "floats", b"floats"),
            (unsigned_longs, "unsigned_longs", b"unsigned_longs"),
            (property_keys, "property_keys", b"property_keys"),
            (identifier_names, "identifier_names", b"identifier_names"),
            (string_literals, "string_literals", b"string_literals"),
            (list_lengths, "list_lengths", b"list_lengths")
        )
    };
}

/// During compression, we typically deal with both grammar-fixed data
/// (e.g. the list of possible values in a string enum) and user-extensible
/// data (e.g. string literals).
///
/// This container is meant to store data associated with user-extensible data.
/// This serves typically to store per-kind compression settings, per-kind
/// compressed/decompressed data, dictionaries, etc.
#[derive(Debug, Default, Add, Clone, AddAssign)]
pub struct PerUserExtensibleKind<T> {
    pub floats: T,
    pub unsigned_longs: T,
    pub property_keys: T,
    pub identifier_names: T,
    pub string_literals: T,
    pub list_lengths: T,
}
impl<T> PerUserExtensibleKind<T> {
    /// Initialize a new `PerUserExtensibleKind`.
    pub fn with<F>(f: F) -> Self
    where
        F: Fn(&str) -> T,
    {
        // Generate a PerUserExtensibleKind, where for each field:
        //    `foo: f("foo")`.
        //
        // This macro doubles as a static checks that we haven't forgotten
        // any field in `for_field_in_user_extensible`.
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            PerUserExtensibleKind {
                $(
                    $ident: f($name),
                )*
            }
        } };
        for_field_in_user_extensible!(with_field)
    }

    /// Convert a `PerUserExtensibleKind` into another one.
    pub fn into_with<F, U>(self, f: F) -> PerUserExtensibleKind<U>
    where
        F: Fn(&str, T) -> U,
    {
        // Generate a PerUserExtensibleKind, where for each field:
        //    `foo: f("foo", self.foo)`.
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            PerUserExtensibleKind {
                $(
                    $ident: f($name, self.$ident),
                )*
            }
        } };
        for_field_in_user_extensible!(with_field)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&'static str, &T)> {
        // Generate a vector with one item per field
        //    `(foo, &self.foo)`.
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            vec![
                $(
                    ($name, &self.$ident),
                )*
            ]
        } };
        for_field_in_user_extensible!(with_field).into_iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut T)> {
        // Generate a vector with one item per field
        //    `(foo, &mut self.foo)`.
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            vec![
                $(
                    ($name, &mut self.$ident),
                )*
            ]
        } };
        for_field_in_user_extensible!(with_field).into_iter()
    }

    pub fn into_iter(self) -> impl Iterator<Item = (&'static str, T)> {
        // Generate a vector with one item per field
        //    `(foo, self.foo)`.
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            vec![
                $(
                    ($name, self.$ident),
                )*
            ]
        } };
        for_field_in_user_extensible!(with_field).into_iter()
    }

    /// Access a field by its name.
    ///
    /// This method is typically used to simplify parsing a file that
    /// contains sections explicitly labelled "bools", "floats", etc.
    /// In such case, `field_name` is expected to be a user input.
    ///
    /// Return `None` if `field_name` is not one of the field names.
    pub fn get(&self, field_name: &str) -> Option<&T> {
        self.get_b(field_name.as_bytes())
    }
    pub fn get_mut(&mut self, field_name: &str) -> Option<&mut T> {
        self.get_mut_b(field_name.as_bytes())
    }

    /// Access a field by its name, specified as a sequence of bytes.
    ///
    /// This method is typically used to simplify parsing a file that
    /// contains sections explicitly labelled "bools", "floats", etc.
    /// In such case, `field_name` is expected to be a user input.
    ///
    /// Return `None` if `field_name` is not one of the field names.
    pub fn get_mut_b(&mut self, field_name: &[u8]) -> Option<&mut T> {
        // Generate a `match` with for each field
        //    `b"foo" => Some(&mut self.foo)`
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            match field_name {
                $(
                    $bname => Some(&mut self.$ident),
                )*
                _ => None,
            }
        } };
        for_field_in_user_extensible!(with_field)
    }
    pub fn get_b(&self, field_name: &[u8]) -> Option<&T> {
        // Generate a `match` with for each field
        //    `b"foo" => Some(&self.foo)`
        macro_rules! with_field { ($(($ident: ident, $name: expr, $bname: expr )),*) => {
            match field_name {
                $(
                    $bname => Some(&self.$ident),
                )*
                _ => None,
            }
        } };
        for_field_in_user_extensible!(with_field)
    }
}

/// A macro used to generate code that will operate on all fields of a `PerStaticKind`.
#[macro_export]
macro_rules! for_field_in_per_static {
    ( $cb: ident ) => {
        $cb!(
            (bools, "bools", "bools", b"bools"),
            (interface_names, "interfaces", "interfaces", b"interfaces"),
            (
                string_enums,
                "string_enums",
                "string enums",
                b"string_enums"
            )
        )
    };
}

/// During compression, we typically deal with both grammar-fixed data
/// (e.g. the list of possible values in a string enum) and user-extensible
/// data (e.g. string literals).
///
/// This container is meant to store data associated with grammar-fixed data.
/// This serves typically to store per-kind compression settings, per-kind
/// compressed/decompressed data, dictionaries, etc.
#[derive(Debug, Default)]
pub struct PerStaticKind<T> {
    pub bools: T,
    pub interface_names: T,
    pub string_enums: T,
}

/// A default number of buckets for histograms.
const DEFAULT_HISTOGRAM_BUCKETS: usize = 32;

/// Simple representation of a rational number.
///
/// Used to avoid some fixed-point computation when it's not really necessary.
pub struct Rational<T> {
    /// The numerator.
    pub num: T,

    /// The denominator.
    pub den: T,
}

/// A histogram designed to store information on how hoften we perform an operation
/// marked by a given probability.
pub struct ProbabilityHistogram {
    buckets: Vec<usize>,
}
impl ProbabilityHistogram {
    pub fn with_capacity(len: usize) -> Self {
        assert!(len > 1);
        let mut buckets = Vec::with_capacity(len);
        buckets.resize(len, 0);
        Self { buckets }
    }

    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_HISTOGRAM_BUCKETS)
    }

    /// Add a probability to the corresponding bucket in the histogram.
    pub fn add_probability(&mut self, probability: Rational<usize>) {
        assert!(probability.num <= probability.den);
        let len = self.buckets.len();
        let index = (probability.num * (len - 1)) / probability.den;
        self.buckets[index] += 1;
    }
}
impl Default for ProbabilityHistogram {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for ProbabilityHistogram {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let total: usize = self.buckets.iter().sum();
        write!(formatter, "  bucket ")?;
        for i in 0..32 {
            write!(formatter, " | {:3}", i)?;
        }
        write!(formatter, "\n --------")?;
        for _ in 0..32 {
            write!(formatter, "-|----")?;
        }
        write!(formatter, "\n       % ")?;
        for bucket in &self.buckets {
            write!(formatter, " | {:3.0}", 100. * *bucket as f64 / total as f64)?;
        }
        write!(formatter, "\n")?;
        Ok(())
    }
}

impl std::fmt::Display for PerStaticKind<ProbabilityHistogram> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(formatter, "Static:\n")?;
        macro_rules! with_field { ($(($ident: ident, $name: expr, $user_readable: expr, $bname: expr )),*) => {
                $(
                    write!(formatter, "    {name}:\n", name = $user_readable)?;
                    write!(formatter, "{content}", content = self.$ident)?;
                )*
        } };
        for_field_in_per_static!(with_field);
        Ok(())
    }
}

pub trait DisplayWith<T> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter, data: &T) -> Result<(), std::fmt::Error>;
}

impl DisplayWith</* Total */ BytesAndInstances> for BytesAndInstances {
    fn fmt(
        &self,
        formatter: &mut std::fmt::Formatter,
        total: &BytesAndInstances,
    ) -> Result<(), std::fmt::Error> {
        let bytes = Into::<usize>::into(self.bytes);
        let symbols = Into::<usize>::into(self.instances);
        let total_bytes = Into::<usize>::into(total.bytes);
        let total_symbols = Into::<usize>::into(total.instances);
        write!(formatter, "symbols {symbols} = {symbols_percent:.2}, bytes {bytes} = {bytes_percent:.2} ({bits_per_symbol:.2} bits/symbol)",
            symbols = symbols,
            bytes = bytes,
            symbols_percent = 100.* symbols as f64 / total_symbols as f64,
            bytes_percent = 100.* bytes as f64 / total_bytes as f64,
            bits_per_symbol = 8. * bytes as f64 / symbols as f64,
        )
    }
}

impl std::fmt::Display for PerUserExtensibleKind<BytesAndInstances> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let total = BytesAndInstances {
            bytes: self.iter().map(|(_, data)| data.bytes.clone()).sum(),
            instances: self.iter().map(|(_, data)| data.instances.clone()).sum(),
        };

        write!(formatter, "  User-extensible:\n")?;
        for (field, name) in &[
            (&self.identifier_names, "identifier names"),
            (&self.string_literals, "string literals"),
            (&self.floats, "floats"),
            (&self.unsigned_longs, "unsigned longs"),
            (&self.list_lengths, "list lengths"),
        ] {
            write!(formatter, "    {name}: ", name = name)?;
            field.fmt(formatter, &total)?;
            write!(formatter, "\n")?;
        }
        write!(
            formatter,
            "Total: {} bytes, {} symbols",
            total.bytes, total.instances
        )?;
        Ok(())
    }
}