Skip to main content

mlt_core/encoder/
optimizer.rs

1use bitvec::vec::BitVec;
2
3use crate::decoder::Morton;
4#[cfg(feature = "unstable-v2")]
5use crate::encoder::encode02;
6use crate::encoder::model::{CurveParams, StagedLayer};
7use crate::encoder::{
8    Codecs, Encoder, EncoderConfig, SortStrategy, WireVersion, encode01,
9    spatial_sort_likely_to_help,
10};
11use crate::tile::{PropKind, TileLayer};
12use crate::{MltError, MltResult, PropValue};
13
14impl StagedLayer {
15    /// Encode and serialize the layer directly into `enc`, without creating any
16    /// intermediate representation.
17    ///
18    /// This is the hot path inside `TileLayer::encode`: each sort-strategy
19    /// trial calls this method on its own fresh `Encoder`, and only the
20    /// `Encoder` with the smallest `total_len()` is kept.
21    #[hotpath::measure]
22    pub fn encode_into(self, enc: Encoder, codecs: &mut Codecs) -> MltResult<Encoder> {
23        if self.name.is_empty() {
24            return Err(MltError::MissingLayerName);
25        }
26        match enc.config().wire_version() {
27            WireVersion::V01 => encode01::encode_into01(self, enc, codecs),
28            #[cfg(feature = "unstable-v2")]
29            WireVersion::V02 => encode02::encode_into02(self, enc, codecs),
30        }
31    }
32}
33
34/// Seed the encoder's curve-derived caches so the Hilbert/Morton dictionary
35/// builders skip their min/max scan. `Morton::new` returns `Err` when bits > 16;
36/// `dict_may_be_beneficial` reads `morton_cache.is_none()` and falls back to
37/// a Vec2-only path in that case.
38fn seed_curve_caches(enc: &mut Encoder, curve_params: CurveParams) {
39    enc.hilbert_cache = Some(curve_params);
40    enc.morton_cache = Morton::new(curve_params.bits, curve_params.shift).ok();
41}
42
43/// Feature-count threshold above which the spatial trial is subject to the
44/// bounding-box pruning heuristic.
45const SORT_TRIAL_THRESHOLD: usize = 512;
46
47impl TileLayer {
48    /// Encode a [`TileLayer`] to bytes, automatically optimizing all encoding choices.
49    ///
50    /// This is the primary encoding entry point. It:
51    /// 1. Determines which sort strategies to try based on `cfg`
52    /// 2. Tries each sort strategy, encoding and measuring the output size
53    /// 3. Returns the smallest encoding as a complete layer record (including tag and length prefix)
54    ///
55    /// All encoding choices - sort order, per-stream integer encodings, string compression,
56    /// vertex buffer layout - are selected automatically to minimize output size.
57    #[hotpath::measure]
58    pub fn encode(self, cfg: EncoderConfig) -> MltResult<Vec<u8>> {
59        if self.name().is_empty() {
60            return Err(MltError::MissingLayerName);
61        }
62        if self.features().is_empty() {
63            return Ok(Vec::new());
64        }
65
66        let mut sort_by = vec![SortStrategy::Unsorted];
67        let try_spatial_sort =
68            cfg.attempt_spatial_morton_sort() || cfg.attempt_spatial_hilbert_sort();
69        if try_spatial_sort
70            && (self.feature_count() < SORT_TRIAL_THRESHOLD || spatial_sort_likely_to_help(&self))
71        {
72            if cfg.attempt_spatial_morton_sort() {
73                sort_by.push(SortStrategy::SpatialMorton);
74            }
75            if cfg.attempt_spatial_hilbert_sort() {
76                sort_by.push(SortStrategy::SpatialHilbert);
77            }
78        }
79        if cfg.attempt_id_sort() {
80            sort_by.push(SortStrategy::Id);
81        }
82
83        let stats = self.analyze(cfg.allow_shared_dict())?;
84        // Bounds are order-invariant, so this scan is shared across every
85        // sort trial and the encoder's Hilbert/Morton dictionary builders.
86        let curve_params = self.curve_params();
87
88        // `Encoder::preserve_results` clears caches only on the moved-out
89        // archive, so a single seeding here serves every trial that reuses
90        // `enc`.
91        let mut enc = Encoder::new(cfg);
92        seed_curve_caches(&mut enc, curve_params);
93
94        let (last, init) = sort_by.split_last().expect("at least one strategy");
95        if init.is_empty() {
96            let mut codecs = Codecs::default();
97            StagedLayer::from_tile(self, *last, &stats, cfg.tessellate(), curve_params)
98                .encode_into(enc, &mut codecs)?
99        } else {
100            let mut codecs = Codecs::default();
101            enc = {
102                let first = init[0];
103                StagedLayer::from_tile(self.clone(), first, &stats, cfg.tessellate(), curve_params)
104                    .encode_into(enc, &mut codecs)?
105            };
106            let mut best = enc.preserve_results();
107            // Clone for all-but-last strategies
108            for &sort in &init[1..] {
109                let layer = StagedLayer::from_tile(
110                    self.clone(),
111                    sort,
112                    &stats,
113                    cfg.tessellate(),
114                    curve_params,
115                );
116                enc = layer.encode_into(enc, &mut codecs)?;
117                if enc.total_len() < best.total_len() {
118                    best = enc.preserve_results();
119                } else {
120                    // Drop the losing trial's bytes, or the next trial would append to them and overcount its total_len.
121                    enc.clear_results();
122                }
123            }
124            // Last strategy: consume self, no clone
125            let layer = StagedLayer::from_tile(self, *last, &stats, cfg.tessellate(), curve_params);
126            enc = layer.encode_into(enc, &mut codecs)?;
127            if enc.total_len() < best.total_len() {
128                best = enc.preserve_results();
129            }
130            best
131        }
132        .into_layer_bytes()
133    }
134}
135
136/// Row-order-independent presence classification for IDs and properties.
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub enum Presence {
139    /// No feature has a value for this logical column.
140    AllNull,
141    /// Every feature has a value for this logical column.
142    AllPresent,
143    /// Some, but not all, features have a value for this logical column.
144    Mixed,
145    /// Mixed presence with the same per-feature mask as an earlier property column.
146    ///
147    /// Only tells the stager the column is nullable, same as [`Self::Mixed`]. Which
148    /// columns actually end up sharing one presence bitfield is decided per wire
149    /// format at write time, from the staged masks - see `SharedPresence` in
150    /// `encoder::encode02`.
151    SameAsProp(usize),
152}
153impl Presence {
154    /// Create presence value
155    #[must_use]
156    pub fn from_bits(bits: &BitVec<u8>, existing: &[(BitVec<u8>, usize)]) -> Self {
157        if bits.not_any() {
158            Self::AllNull
159        } else if bits.all() {
160            Self::AllPresent
161        } else if let Some((_, idx)) = existing.iter().find(|(v, _)| v == bits) {
162            Self::SameAsProp(*idx)
163        } else {
164            Self::Mixed
165        }
166    }
167}
168
169/// How a property participates in a shared dictionary group.
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub enum SharedDictRole {
172    /// The property is encoded as a standalone column.
173    None,
174    /// The property is the first column in this group and emits the shared dictionary with this prefix.
175    Owner(String),
176    /// The property is emitted by the group owner at this property index.
177    Member(usize),
178}
179
180/// Row-order-independent facts for a single property column.
181#[derive(Debug, Clone, PartialEq, Eq)]
182pub struct PropertyStats {
183    pub presence: Presence,
184    pub stats: PropertyTypedStats,
185}
186
187/// Row-order-independent layer facts computed once before sort trials.
188#[derive(Debug, Clone, PartialEq, Eq)]
189pub struct LayerStats {
190    pub id: Option<PropertyStats>,
191    pub properties: Vec<PropertyStats>,
192}
193
194/// Row-order-independent value statistics for a property column.
195#[derive(Debug, Clone, Default, PartialEq, Eq)]
196pub enum PropertyTypedStats {
197    /// No present values.
198    #[default]
199    None,
200    Bool,
201    Signed {
202        min: i64,
203        max: i64,
204    },
205    Unsigned {
206        min: u64,
207        max: u64,
208    },
209    F32,
210    F64,
211    String {
212        shared_dict: SharedDictRole,
213    },
214}
215
216impl PropertyTypedStats {
217    #[must_use]
218    pub fn values_fit_u32(&self) -> bool {
219        match self {
220            Self::None | Self::Bool | Self::F32 | Self::F64 | Self::String { .. } => false,
221            Self::Signed { min, max } => *min >= 0 && u32::try_from(*max).is_ok(),
222            Self::Unsigned { max, .. } => u32::try_from(*max).is_ok(),
223        }
224    }
225
226    /// Returns `true` if every value fits in an `i32`.
227    /// Unlike [`Self::values_fit_u32`] this admits negative values.
228    #[must_use]
229    pub fn values_fit_i32(&self) -> bool {
230        match self {
231            Self::None | Self::Bool | Self::F32 | Self::F64 | Self::String { .. } => false,
232            Self::Signed { min, max } => i32::try_from(*min).is_ok() && i32::try_from(*max).is_ok(),
233            Self::Unsigned { max, .. } => i32::try_from(*max).is_ok(),
234        }
235    }
236
237    #[must_use]
238    pub fn shared_dict(&self) -> SharedDictRole {
239        match self {
240            Self::String { shared_dict, .. } => shared_dict.clone(),
241            _ => SharedDictRole::None,
242        }
243    }
244
245    pub(crate) fn set_shared_dict(&mut self, role: SharedDictRole) {
246        match self {
247            Self::String { shared_dict, .. } => *shared_dict = role,
248            _ => debug_assert_eq!(role, SharedDictRole::None),
249        }
250    }
251
252    pub(crate) fn push(
253        &mut self,
254        prop: &PropValue,
255        column_idx: usize,
256        property_name: &str,
257    ) -> MltResult<bool> {
258        match prop {
259            PropValue::Bool(Some(_)) => {
260                self.merge_same_kind(Self::Bool, column_idx, property_name)?;
261            }
262            PropValue::I8(Some(v)) => {
263                self.merge_signed(i64::from(*v), column_idx, property_name)?;
264            }
265            PropValue::U8(Some(v)) => {
266                self.merge_unsigned(u64::from(*v), column_idx, property_name)?;
267            }
268            PropValue::I32(Some(v)) => {
269                self.merge_signed(i64::from(*v), column_idx, property_name)?;
270            }
271            PropValue::U32(Some(v)) => {
272                self.merge_unsigned(u64::from(*v), column_idx, property_name)?;
273            }
274            PropValue::I64(Some(v)) => self.merge_signed(*v, column_idx, property_name)?,
275            PropValue::U64(Some(v)) => self.merge_unsigned(*v, column_idx, property_name)?,
276            PropValue::F32(Some(_)) => {
277                self.merge_same_kind(Self::F32, column_idx, property_name)?;
278            }
279            PropValue::F64(Some(_)) => {
280                self.merge_same_kind(Self::F64, column_idx, property_name)?;
281            }
282            PropValue::Str(Some(_)) => self.merge_string(column_idx, property_name)?,
283            _ => return Ok(false),
284        }
285        Ok(true)
286    }
287
288    fn merge_signed(
289        &mut self,
290        value: i64,
291        column_idx: usize,
292        property_name: &str,
293    ) -> MltResult<()> {
294        match self {
295            Self::None => {
296                *self = Self::Signed {
297                    min: value,
298                    max: value,
299                };
300            }
301            Self::Signed { min, max } => {
302                *min = (*min).min(value);
303                *max = (*max).max(value);
304            }
305            _ => return mixed_prop_err(column_idx, property_name),
306        }
307        Ok(())
308    }
309
310    fn merge_unsigned(
311        &mut self,
312        value: u64,
313        column_idx: usize,
314        property_name: &str,
315    ) -> MltResult<()> {
316        match self {
317            Self::None => {
318                *self = Self::Unsigned {
319                    min: value,
320                    max: value,
321                };
322            }
323            Self::Unsigned { min, max } => {
324                *min = (*min).min(value);
325                *max = (*max).max(value);
326            }
327            _ => return mixed_prop_err(column_idx, property_name),
328        }
329        Ok(())
330    }
331
332    fn merge_string(&mut self, column_idx: usize, property_name: &str) -> MltResult<()> {
333        match self {
334            Self::None => {
335                *self = Self::String {
336                    shared_dict: SharedDictRole::None,
337                };
338            }
339            Self::String { .. } => {}
340            _ => return mixed_prop_err(column_idx, property_name),
341        }
342        Ok(())
343    }
344
345    fn merge_same_kind(
346        &mut self,
347        kind: Self,
348        column_idx: usize,
349        property_name: &str,
350    ) -> MltResult<()> {
351        match self {
352            Self::None => *self = kind,
353            Self::Bool if matches!(kind, Self::Bool) => {}
354            Self::F32 if matches!(kind, Self::F32) => {}
355            Self::F64 if matches!(kind, Self::F64) => {}
356            _ => return mixed_prop_err(column_idx, property_name),
357        }
358        Ok(())
359    }
360}
361
362impl TileLayer {
363    /// Analyze a [`TileLayer`] and return reusable ID/property facts for the optimizer.
364    #[hotpath::measure]
365    pub(crate) fn analyze(&self, allow_shared_dict: bool) -> MltResult<LayerStats> {
366        let mut property_bits = Vec::with_capacity(self.property_names().len());
367        let mut properties = self.analyze_properties(&mut property_bits)?;
368        let id = self.analyze_ids(&property_bits);
369        if allow_shared_dict {
370            self.group_string_properties(&mut properties);
371        }
372        Ok(LayerStats { id, properties })
373    }
374
375    fn analyze_ids(&self, property_bits: &[(BitVec<u8>, usize)]) -> Option<PropertyStats> {
376        let mut min = u64::MAX;
377        let mut max = 0u64;
378        let mut bits = BitVec::<u8>::with_capacity(self.feature_count());
379        for feature in self.features() {
380            if let Some(id) = feature.id() {
381                min = min.min(id);
382                max = max.max(id);
383                bits.push(true);
384            } else {
385                bits.push(false);
386            }
387        }
388        let presence = Presence::from_bits(&bits, property_bits);
389        if presence == Presence::AllNull {
390            None
391        } else {
392            Some(PropertyStats {
393                presence,
394                stats: PropertyTypedStats::Unsigned { min, max },
395            })
396        }
397    }
398
399    fn analyze_properties(
400        &self,
401        property_bits: &mut Vec<(BitVec<u8>, usize)>,
402    ) -> MltResult<Vec<PropertyStats>> {
403        self.property_names()
404            .iter()
405            .enumerate()
406            .map(|(col_idx, name)| -> MltResult<PropertyStats> {
407                let mut kind = None;
408                let mut stats = PropertyTypedStats::default();
409                let mut bits = BitVec::<u8>::with_capacity(self.feature_count());
410                for feature in self.features() {
411                    let prop = feature.properties().get(col_idx);
412                    if let Some(prop_kind) = prop.map(PropKind::from) {
413                        match kind {
414                            Some(kind) if kind != prop_kind => {
415                                return mixed_prop_err(col_idx, name.as_str());
416                            }
417                            None => kind = Some(prop_kind),
418                            _ => {}
419                        }
420                    }
421                    if let Some(prop) = prop
422                        && stats.push(prop, col_idx, name)?
423                    {
424                        bits.push(true);
425                    } else {
426                        bits.push(false);
427                    }
428                }
429
430                let presence = Presence::from_bits(&bits, property_bits);
431                if presence == Presence::Mixed {
432                    property_bits.push((bits, col_idx));
433                }
434                Ok(PropertyStats { presence, stats })
435            })
436            .collect()
437    }
438}
439
440#[inline]
441fn mixed_prop_err<T>(column_idx: usize, property_name: &str) -> MltResult<T> {
442    Err(MltError::MixedPropertyTypes(
443        column_idx,
444        property_name.to_owned(),
445    ))
446}