Skip to content
Systems programming for what’s next.
— Vex

filter — stable, allocation-aware filtering ​

The filter package selects values from borrowed contiguous data while making allocation and alias behavior explicit. It provides a fresh-output convenience API, caller-owned storage reuse, and fixed or dynamic mask compaction. Output order is stable in every API.

Predicate filtering ​

vex
import { filter, filterInto } from "filter";

fn isReady(value: &Job): bool {
    return value.ready;
}

let ready = filter(jobs.asSpan(), isReady);

let! scratch = Vec.withCapacity<Job>(jobs.len());
let written = filterInto(jobs.asSpan(), &scratch!, isReady);

filter constructs a fresh Vec<T>. filterInto replaces the logical contents of the supplied destination and reuses its allocation when capacity is sufficient. It returns the destination's final length.

The predicate runs exactly once for every source element. A borrowed Copy value is copied at no ownership cost; an ownership-bearing value requires Clone and every retained value is cloned exactly once. If the source is the destination's complete live view, filterInto uses the in-place Vec.retain path and keeps existing owners. Partial overlap is safely staged before the destination is mutated.

Fixed-mask filtering ​

Use a fixed Mask<N> when selection has already been computed as a vector predicate:

vex
import { filterByMask, filterByMaskInto } from "filter";

let values: [i32; 8] = [4, -1, 8, 0, 3, -6, 9, 2];
let keep = values > [0, 0, 0, 0, 0, 0, 0, 0];

let positive = filterByMask(values, keep);

let! reusable = Vec.withCapacity<i32>(8);
let count = filterByMaskInto(values, keep, &reusable!);

The mask is compacted once through the canonical Mask.compress operation. Selected lanes occupy the output prefix in their original order. The returned count is exact, and the reusable API commits that initialized prefix with one bulk copy rather than an element-by-element push loop.

Fixed-mask filtering accepts numeric Copy lanes. It does not claim SIMD authority for ownership-bearing structs; use predicate filtering for those values.

Dynamic-mask filtering ​

Dynamic comparisons produce a packed Mask whose logical length follows the input. The same API accepts a Span<T> plus that mask:

vex
let keep = samples.asSpan() >= threshold;
let! reusable = Vec.withCapacity<i32>(samples.len());
let count = filterByMaskInto(samples.asSpan(), &keep, &reusable!);

let selected = filterByMask(samples.asSpan(), &keep);

The source and mask lengths must match. The implementation processes full blocks at #Target.simdLanes<T>(), extracts each packed predicate with Mask.block<N>, compacts it with the same Mask.compress, and uses a single fixed-width store when spare Vec capacity covers the complete block. The final short block reads no source lane past the logical end.

Allocation and alias guarantees ​

OperationAllocation policy
filterone fresh upper-bound result allocation
filterInto, independent sourceno allocation when destination capacity covers the source length
filterInto, exact self-viewin-place, no allocation
filterInto, partial overlaptemporary staging to preserve unread values and ownership
filterByMaskone fixed-capacity result allocation
filterByMaskIntono allocation when destination capacity covers N
dynamic filterByMaskone source-upper-bound result allocation
dynamic filterByMaskIntono allocation when destination capacity covers the source length

Reusable destinations retain their allocator or arena binding. Fresh results capture the active allocation region when they are constructed. Capacity is reserved before old destination contents are cleared, so an allocation failure cannot destroy the previous logical value.

CPU and GPU semantics ​

Mask.compress has one backend-independent meaning: stable selected prefix, exact usize count, and a deterministic zero tail in its fixed-array result. CPU SIMD and GPU SIMT backends legalize that semantic operation according to their hardware; the filter API does not expose architecture names or a backend switch.

Dynamic masks use packed-word extraction plus the same fixed-register compact contract; they do not silently fall back to a per-lane compatibility loop. See the SIMD/SIMT unified execution contract for the cross-backend compaction rules.

Choosing an API ​

  • Use Span.filter for the shortest fresh-output spelling.
  • Use filter when package-level discoverability is preferable.
  • Use filterInto in loops, parsers, and services that can reuse a destination.
  • Use filterByMaskInto when a numeric fixed-block comparison already produced a Mask<N> and storage reuse matters.
  • Use the Span/Mask overload when a dynamic comparison already produced a packed runtime mask.

Released under the MIT License.