Skip to content

Rust-side Variant marshalling#1600

Draft
Bromeon wants to merge 3 commits into
masterfrom
perf/variant-rust-marshal
Draft

Rust-side Variant marshalling#1600
Bromeon wants to merge 3 commits into
masterfrom
perf/variant-rust-marshal

Conversation

@Bromeon

@Bromeon Bromeon commented May 14, 2026

Copy link
Copy Markdown
Member

For Variant holding Copy builtin types (or rather those that don't need an FFI constructor/destructor), we can emulate Godot's Variant layout directly in Rust. This saves a large number of FFI calls for just Variant conversions and has the potential to speed up operations across the board.

@Bromeon Bromeon added c: core Core components performance Performance problems and optimizations labels May 14, 2026
@GodotRust

Copy link
Copy Markdown

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1600

@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch 6 times, most recently from 3a3fc71 to c46ca24 Compare May 18, 2026 19:41
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch from c46ca24 to c9bb01f Compare May 23, 2026 14:38
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch from c9bb01f to edb4f83 Compare May 31, 2026 10:23
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch 4 times, most recently from eeebe71 to e0aefc5 Compare June 12, 2026 22:16
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch 2 times, most recently from e02e35a to 95a6b23 Compare June 28, 2026 22:15
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch from 95a6b23 to 6815624 Compare July 7, 2026 05:02
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch 2 times, most recently from bf27835 to 820f769 Compare July 18, 2026 21:37
Bromeon added 3 commits July 21, 2026 22:32
Manual-mode benches must return `crate::framework::BenchResult`;
missing it previously produced a confusing downstream compile error.
`Extend` pre-allocates from the iterator's size hint to avoid per-element
grows; over-reported hints trim unused trailing slots instead of panicking,
and a saturating add guards against a hostile hint overflowing the target
length. The fast path takes a single base pointer and offsets with raw
pointer arithmetic, instead of one `array_operator_index` FFI call per
element.

`resize_default` complements `resize()` for default-constructible types;
`resize_nil` covers `VarArray`, whose `Variant` element the `Copy` bound of
`resize_default` excludes.

Includes the `PackedFloat32Array` <-> `Array` conversion benchmarks, which
also exercise this array-growth path.
Builtins that are small enough and whose Default/Clone/Drop don't need
FFI (e.g. numbers, vectors, color, RID) now marshal in-place in Variant's
struct layout, skipping the engine's Variant FFI functions.

This is an internal optimization with no user-facing API for now.

RustMarshal relies on Godot's private Variant layout (tag@0, data@8), which
the GDExtension ABI doesn't guarantee. A startup self-check builds INT and
Vector3 variants via raw FFI and compares them against RustVariant reads,
panicking toward the variant-ffi-marshal fallback on mismatch. Codegen
fails fast on a missing builtin size, so a stale size table can't
misclassify a heap-allocated type as in-place-storable.
@Bromeon
Bromeon force-pushed the perf/variant-rust-marshal branch from 820f769 to e9a0a2d Compare July 21, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: core Core components performance Performance problems and optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants