xmath-usage

Eliminate per-frame allocations in Defold math code with in-place xmath operations.

11|Updated Oct 10, 2024
One-click install
npx skills add https://github.com/selimanac/defold-daabbcc3d --skill xmath-usage
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: xmath-usage
Source: https://github.com/selimanac/defold-daabbcc3d/tree/main/.agents/skills/xmath-usage
Command: npx skills add https://github.com/selimanac/defold-daabbcc3d --skill xmath-usage

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

In Lua-based Defold projects, hot-path math frequently allocates objects, triggering GC pauses. xmath enables in-place mutation and reuses pre-allocated scratch variables, eliminating per-frame allocations and reducing GC pressure.

Core Features & Use Cases

  • In-place mutation: all xmath operations write results into the first argument, enabling scratch buffers to be reused across frames.
  • Pre-allocated scratch variables: store transient vectors/quaternions at module scope or init to avoid per-frame allocations.
  • Type support: xmath handles vector3, vector4, and quaternion operations with a consistent first-argument output convention.
  • Integration pattern: pair xmath with vmath to initialize scratch objects, then apply a sequence of operations to compute final results.
  • Use cases: physics nudges, animation tweaks, and other hot-path math in gameplay loops.

Quick Start

Pre-allocate scratch buffers and rewrite hot math paths to mutate outputs in place using xmath.

Frequently Asked Questions about xmath-usage

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I eliminate per-frame allocations in Defold math loops?

To eliminate per-frame allocations in Defold, adopt xmath for zero-allocation in-place operations. It writes results into the first argument using pre-allocated scratch buffers, preventing transient vector and quaternion objects from triggering garbage collection pauses.

Why does my Defold game stutter during physics and gameplay loops?

Defold games stutter during physics loops because standard math operations allocate transient objects, triggering garbage collection pauses. Using xmath for in-place mutation eliminates these per-frame allocations by reusing pre-allocated scratch buffers for vector math.

How do I use pre-allocated scratch buffers for vector3 and quaternion math in Lua?

To use pre-allocated scratch buffers for vector3 and quaternion math, initialize your buffer objects with vmath, then apply xmath operations to mutate them in place. The first argument serves as the output target, allowing buffer reuse across frames.

Does xmath work with standard vmath functions in Defold?

Yes, xmath works with vmath by using it to initialize scratch objects at module scope or during initialization. You then apply xmath operations to perform in-place mutations on those pre-allocated objects within your performance-critical loops.

What is the first-argument output pattern in zero-allocation math?

The first-argument output pattern in zero-allocation math writes the computation result directly into the first argument passed to the function. This enables in-place mutation of existing pre-allocated objects rather than returning new instances that allocate memory.

When should I use in-place mutation for Defold math operations?

You should use in-place mutation for Defold math operations in performance-critical hot paths like physics nudges, animation tweaks, and gameplay logic. This zero-allocation approach prevents garbage collection spikes during intensive per-frame calculations.