Skip to content

Conversation

@HEIHUAa
Copy link

@HEIHUAa HEIHUAa commented Jan 10, 2026

Yes, it involves an additional automatic optimization step just before runtime, after the HScript parsing is completed.

This includes:

  • Simplifying expressions like x = 10 + 20 to x = 30.
  • Constant folding.
  • Converting division to multiplication.
  • Dead code elimination.
  • Replacing some code with more efficient implementations.

For example:

function x() {
 if (x == 0) {
  trace(1);
 }
}

becomes

function x()
 if (x == 0)
  trace(1);

...among others.

Newly Added Parameters:

  • optimizeEnabled: Bool
  • optimizeLevel: Int
  • enableConstantFolding: Bool
  • enableExpressionSimplification: Bool
  • enableDeadCodeElimination: Bool
  • enableBranchOptimization: Bool
  • optimizerDebug: Bool

Total: 7 options.

Most are self-explanatory. Let me describe the ones that might be less clear:

  • optimizeLevel: Sets the maximum number of optimization passes. It determines how many times the optimizer will attempt to optimize the HScript code. However, there is a built-in check to see if further optimization is possible. In most cases, only 1 to 3 passes are needed.
  • optimizerDebug: When enabled, it outputs the code structure (AST) both before and after optimization.

The optimization process happens within the execute phase and does not affect runtime performance at all.

Regarding optimization speed: for a large script (22KB), three optimization passes take about 22 milliseconds, averaging 7.33ms per pass. I believe this should be perfectly acceptable.

Additionally, I have tested whether the optimization results remain consistent with the original code. I can only say that I tested as thoroughly as possible, and no issues were found—even when running large mods.

@HEIHUAa HEIHUAa marked this pull request as draft January 10, 2026 02:40
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.

1 participant