Skip to main content

Module array_splitter

Module array_splitter 

Source
Expand description

Array-splitting mode for SplitRS.

Some Rust source files are dominated by a single oversized static (or const) item whose initializer is one enormous array / slice literal — a large data table, for example a unit catalogue or a force-field parameter set. The default item-granularity splitter cannot break such a file up: the whole table is one AST item, so it always lands intact in a single generated module that remains over the line budget.

This module implements a dedicated pass that splits the elements of such literals across several chunk files, then reconstructs the original pub static NAME: &[T] in a generated mod.rs using compile-time slice concatenation. The reconstruction preserves the exact public type (&'static [T]), so no downstream consumer needs to change.

§Generated layout

For an input units_base.rs containing

pub static ALL_BASE_UNITS: &[UcumAtom] = &[ /* thousands of entries */ ];

the pass produces a directory units_base/ containing:

  • chunk_0.rs, chunk_1.rs, … — each exporting pub(super) static ALL_BASE_UNITS_PART_0: &[UcumAtom] = &[ … ];
  • mod.rs — re-declares the chunk modules, keeps any other top-level items verbatim, and rebuilds pub static ALL_BASE_UNITS: &[UcumAtom] from the parts via a const fn concatenation.

The original units_base.rs is removed. Because the parent module declared pub mod units_base;, the directory module is a drop-in replacement and super::units_base::ALL_BASE_UNITS keeps resolving to the same type.

§Reconstruction technique

Compile-time concatenation of &'static [T] into a single &'static [T] is performed with a const fn that copies every element into a backing [T; N] array. T must be Copy (true for every plain data-table element type — structs of scalars / &'static str, or tuples thereof). The const filler value reuses the first element of the first chunk, so no synthetic default value is required and the technique is fully type-agnostic.

Structs§

ArrayAnalysis
Result of analysing a file for splittable array items.
ArrayItem
A top-level array/slice static or const that is a candidate for element-level splitting.

Functions§

analyse_arrays
Analyse file, classifying its top-level items. Array static/const items whose rendered element list exceeds max_lines are collected as splittable; everything else is preserved.
run_split_arrays
Core entry point: split oversized array literals in input_file into a sub-directory named after the file stem.