Performance Optimization

To achieve maximum execution speed in AutoLang, it is essential to understand how the Virtual Machine handles memory and object instantiation.


1. The Power of Primary Constructors

The most significant optimization you can make is preferring Primary Constructors over Secondary Constructors.

  • Secondary Constructor: Executes multiple SET_FIELD instructions sequentially. Each instruction has its own overhead within the VM loop.
  • Primary Constructor: Triggers a single native bulk-copy. Since members are identified by IDs, the VM allocates and fills the entire object layout in one pass.

2. Pseudo-constructors Pattern

AutoLang allows a unique architecture where static functions can act as constructors. If you define static func A() inside class A, calling A()will automatically resolve to that static function.

The Strategy: Use a Primary Constructor to handle the raw data storage, and use a Pseudo-constructor (static function) to handle complex logic, then return the primary instance.

class Player(val id: Int, val health: Float, val name: String) { // This is the High-Speed Primary Constructor static func Player(name: String): Player { // Complex logic happens here val generatedId = 0 val defaultHealth = 100.0 // Return the primary constructor call to save all data at once return Player(generatedId, defaultHealth, name) } } // Both calls invoke the static pseudo-constructor val p1 = Player.Player("Child guys") val p2 = Player("AutoLang")

💡 Why this is faster:

In traditional OOP, complex constructors often perform calculations followed by multiple this.x = x assignments. In AutoLang, by moving logic to a static function and ending with a Primary Constructor return, you isolate the "logic" from the "storage". The storage part remains a single-pass native operation.

3. Native Class Seamlessness

This architecture makes Native Classes (written in C++/Rust) look and feel like regular AutoLang classes. They don't need "special" constructor handling; they just expose static functions that return instances mapped to the internal memory layout.

// Example of a pseudo-constructor providing multiple ways to create an object // while always hitting the optimized primary path. class Vector(val x: Float, val y: Float) { static func Zero() = Vector(0.0, 0.0) static func Unit() = Vector(1.0, 1.0) }

4. Advanced: Generics Pseudo-constructors

When building low-level generic classes like Map<K, V>, you cannot easily define a function named Map<K, V>(). Furthermore, the underlying C++ Native code needs to know the exact Type IDs of the generics to allocate the correct hash table.

AutoLang solves this elegantly using the Magic Constant __CLASS__ as a function name, combined with the compiler function getClassId().

@no_extends @no_constructor class Map<K, V> { // 1. The actual native allocator expects Type IDs (Int) @native("map_constructor") private static func create(classId: Int, keyId: Int): Map<K, V> // 2. The Pseudo-constructor trick // __CLASS__ is automatically replaced with "Map" at compile time. // When the user calls Map<String, Int>(), it executes this function! static func __CLASS__() = create(getClassId(Map<K, V>), getClassId(K)) /* ... other map methods ... */ } // User just writes normal code: val myMap = Map<String, Int>()

⚡ The Magic Explained

  • The @no_constructor prevents the compiler from generating a default primary constructor.
  • The __CLASS__ macro dynamically names the function based on the class it resides in.
  • getClassId(K) evaluates at compile time, passing the exact integer ID of the generic type (e.g., String) to the C++ backend.