Skip to content

Optimize OOP class inheritance and element property access - #5290

Open
MohabCodeX wants to merge 3 commits into
multitheftauto:masterfrom
MohabCodeX:feat/oop-inheritance-flattening
Open

Optimize OOP class inheritance and element property access#5290
MohabCodeX wants to merge 3 commits into
multitheftauto:masterfrom
MohabCodeX:feat/oop-inheritance-flattening

Conversation

@MohabCodeX

@MohabCodeX MohabCodeX commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Reading and writing element properties in OOP (like ped.health and element.dimension) is significantly slower than procedural calls due to runtime metatable climbing and dispatch overhead. Under continuous script loops, this caused main thread stalls, packet backlog, and client network trouble frame drops.

This PR addresses this in two steps:

  1. Parent class methods, getters, and setters are now flattened into derived class metatables upon boot-time registration in lua_registerclass. This turns recursive inheritance climbing into a direct O(1) table lookup on every access while preserving derived overrides.
  2. High-frequency element properties (health, dimension, interior, alpha, frozen, doubleSided, model) now use a direct C++ fast-path in CLuaClassDefs::Index and NewIndex, bypassing intermediate argument reader frames. All other properties fall through to the flattened metatables cleanly.

Roadmap Context & Recommendations

Recommended merge order in the OOP and vector optimization series:

  1. Allow Vector and Matrix Data Types in Network Events and RPCs #5278
  2. Add in-place math methods (set, add, sub, mul) to Vector and Matrix classes #5279
  3. Store Vector and Matrix objects as inline userdata #5280
  4. This PR - Optimize OOP class inheritance lookups and add C++ fast-path for element properties

Together, these four PRs resolve ~95% of OOP and vector overhead across networking, heap allocations, GC churn, and property dispatch. The remaining ~5% (native SIMD vector bytecode math, previously explored in #1728 by @Pirulax) can be approached as a standalone future step.

Test plan

Run test_oop_benchmark.zip resource.

Benchmarks (100,000 Iterations)

Scenario Master OOP Procedural (PP) This PR Difference
Property Read: .health 130 ms 41 ms 6 ms ~21x faster than master (~6.8x faster than PP)
Property Read: .dimension 101 ms 40 ms 7 ms ~14x faster than master (~5.8x faster than PP)
Property Read: .armor (Fallthrough) 65 ms 41 ms 66 ms Inheritance lookup flattened to O(1)
Property Write: .health = 100 2523 ms 1108 ms 782 ms Eliminated tick stalls and network trouble frame drops

…rties

- Flatten parent class methods, getters, and setters into derived class metatables upon registration

- Preserve derived class method overrides and polymorphism without modifying Lua script semantics

- Fast-path high-frequency element properties (health, dimension, interior, alpha, frozen, doubleSided, model) directly in C++

- Retain __parent lookup fallback for dynamic class runtime extensions
@MohabCodeX MohabCodeX changed the title feat(lua): optimize OOP class inheritance and fast-path element prope… Optimize OOP class inheritance and element property access Aug 30, 2026
@Lpsd

Lpsd commented Aug 31, 2026

Copy link
Copy Markdown
Member

@r12750 https://github.com/multitheftauto/mtasa-blue/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants