FrankenNumPy

Docs/Guarantees and recipes

Memory Footprint and Allocation Patterns

On this page 3 sections

Per-dtype storage cost

The 16 variants of pub enum ArrayStorage (in crates/fnp-dtype/src/lib.rs); the table below groups the signed/unsigned integer pairs into one row each for brevity:

DType ArrayStorage variant Bytes per element
Bool Bool(Vec<bool>) 1
I8 / U8 I8(Vec<i8>) / U8(Vec<u8>) 1
I16 / U16 I16(Vec<i16>) / U16(Vec<u16>) 2
I32 / U32 I32(Vec<i32>) / U32(Vec<u32>) 4
I64 / U64 I64(Vec<i64>) / U64(Vec<u64>) 8
F16 F16(Vec<half::f16>) 2
F32 F32(Vec<f32>) 4
F64 F64(Vec<f64>) 8
Complex64 Complex64(Vec<(f32, f32)>) interleaved 8
Complex128 Complex128(Vec<(f64, f64)>) interleaved 16
Str String(Vec<std::string::String>) (heap-backed) variable + 24-byte fat pointer per element
Structured Structured(StructuredStorage) typed by StructuredField definitions variable per record

DateTime64 and TimeDelta64 dtypes are not separate ArrayStorage variants; they reuse ArrayStorage::I64(Vec<i64>) (8 bytes per element, ticks since epoch), with the time unit (DateTimeUnit) carried alongside the dtype. NaT is encoded as i64::MIN, matching NumPy.

These are the natural Rust-side owning storage types. A few PyO3-boundary fast paths take layout-checked borrowed byte views for copying into NumPy, but the arrays are not stored internally as flattened Vec<u8>. A reader of crates/fnp-dtype/src/lib.rs can see exactly where each dtype's values live.

When operations copy vs view

Operation Behavior
reshape to a contiguously-compatible shape View (zero copy) when possible; copy when strides cannot be expressed
transpose / swapaxes / moveaxis Always a view (just a stride permutation)
broadcast_to View with zero strides on the broadcast axes
as_strided, sliding_window_view Always a view, bounds-checked
flatten Always copies (the result is always a fresh contiguous 1-D array)
ravel(order='C') on a C-contiguous array View
ravel(order='C') on a non-contiguous array Copy
copy() / array(a, copy=True) Always copies
Arithmetic (a + b, a * b) Always allocates a new output
reduce_* methods Always allocates a new output (typically a scalar or smaller array)
sort / argsort / partition Always allocates (non-mutating)
In-place mutation via MaskedArray::set_*, fill_diagonal, place, putmask No copy; mutates in place

UFuncArrayView uses an Arc<RwLock<Vec<f64>>> backing store, so multiple views into the same array share one allocation. The overlap-detection machinery prevents an in-place operation from corrupting another view that shares memory.

Two memory-safety guard rails

  1. MAX_HEADER_BYTES = 65,536, MAX_ARCHIVE_MEMBERS = 4,096, MAX_ARCHIVE_UNCOMPRESSED_BYTES = 2 GiB, MAX_TEXT_ELEMENTS = 16,777,216. Hard caps on fnp-io parser memory usage prevent untrusted inputs from triggering allocation bombs. These are public pub const values you can reference from your own code if you want the same bounds applied to layer-above text/binary parsers.
  2. No uninitialized memory in the 10 core crates. #![forbid(unsafe_code)] makes MaybeUninit and unchecked Vec::set_len unreachable there. UFuncArray::empty(shape) allocates and zero-fills; the Rust API has no analogue of NumPy's np.empty returning uninitialized bytes. The performance cost (zeroing vs not) is small and the safety benefit (no UB from reading uninitialized) is large. The Python module is outside this rule: fnp_python.empty calls numpy.empty, and native kernels write their results into fresh numpy.empty buffers.

This section in the README on GitHub