Hachi's Memory Management Strategy

Static Lifetime Analysis by Static Scoping (SLASS) in Hachi

Hachi employs a unique memory management strategy known as Static Lifetime Analysis by Static Scoping (SLASS). This approach is the backbone of Hachi’s ā€œAuto-Releaseā€ feature, which automatically frees memory when variables go out of scope. It is similar in concept to the ā€œAuto-Freeā€ mechanism found in the V programming language but is implemented entirely at compile time.


Overview

SLASS leverages Hachi’s static (lexical) scoping rules to determine the lifetime of variables during compilation. The Hachi transpiler analyzes where variables are declared within the code’s block structure, determines when they will go out of scope, and automatically inserts explicit cleanup code (such as calls to free() or invocations of destructors) into the generated C++ output. This process eliminates the need for runtime garbage collection or reference counting.

SLASS


How SLASS Works

1. Static Analysis of Code Scope

2. Lifetime Determination

3. Automatic Cleanup Code Insertion (Auto-Release)


Implementation Details

Injected at the end of the block where myString is defined. In some cases, types such as StringArray are defined with destructors that automatically free their internal buffers when the object is destroyed.

Usage Considerations

Summary

Static Lifetime Analysis by Static Scoping (SLASS) in Hachi provides an efficient, compile-time memory management system that automatically frees heap-allocated resources when variables go out of scope. By analyzing the program’s static structure, Hachi inserts explicit cleanup code into the generated C++ output, eliminating the need for runtime garbage collection or reference counting. While SLASS works seamlessly for most Hachi code, developers must take extra care when embedding C++ code to ensure that memory is managed safely.

Recap