Skip to content

DensityCompiler 5.46.0 ​

final class · pocketmine\world\generator\vanilla\compiler\DensityCompiler · source

Compiles seed-bound density function graphs into straight-line PHP methods (no objects or closures in the evaluation path), so that the JIT can turn them into native float code.

The generated source only depends on the shape of the graph and on seed-independent constants (frequencies, amplitudes, spline points...). Seed-dependent data (noise permutations and offsets) is read from properties filled by the generated constructor, so one compiled file serves every seed.

Two forms are generated:

  • scalar: name(float $x, float $y, float $z) : float. Named functions and caches become memoized methods (per (x, z) column when the value does not depend on y, like vanilla's flat cache; per position otherwise);
  • batch: name(float $x, float $z, array $Y) : array, evaluating a whole column of cell corners. Every node is a short loop over the active points, and lazy nodes split the point list instead of branching. This is the shape PHP's JIT compiles best, and it lets x/z dependent work be done once per column. Both keep vanilla's lazy evaluation and short-circuits (range_choice, interval_select, lerp, mul, min/max), so they compute exactly the same values as the interpreted graph (checked by tests).

Properties ​

$inlineOctaves ​

php
public static bool $inlineOctaves

Inline the octave sampling code instead of calling ImprovedNoise::sampleRaw(). PHP 8.4's tracing JIT fails to compile ("JIT compilation error") loops that contain the inlined sampler, while it compiles (and inlines) the call just fine; the function JIT prefers inlined code.

$unrollOctaves ​

php
public static bool $unrollOctaves

Generate octave stacks unrolled (one block of code per octave) instead of as loops. Unrolled code is faster without JIT, loops are faster with the tracing JIT (long linear traces get aborted).

Methods ​

addMethod() ​

php
public function addMethod(string $source) : void

Adds a hand-written method (e.g. a chunk filling loop using compileInline() fragments).

addProperty() ​

php
public function addProperty(string $declaration) : void

buildClass() ​

php
public function buildClass(string $className, string $namespace) : string

Returns the full class source. The constructor takes the seed-bound data returned by getBindings().

compileBatchRoot() ​

php
public function compileBatchRoot(\pocketmine\world\generator\vanilla\density\DensityFunction $root, string $name) : void

Compiles $root into public function $name(float $x, float $z, array $Y) : array, returning the values for every y in $Y (keys 0..count-1).

compileInline() ​

php
public function compileInline(\pocketmine\world\generator\vanilla\density\DensityFunction $root, array $channelVars, string $prefix) : array

Compiles an expression into a code fragment for inlining into a hand-written loop: returns [statements, result]. The fragment may use $x, $y, $z and the given channel variables (values of interpolated functions).

compileRoot() ​

php
public function compileRoot(\pocketmine\world\generator\vanilla\density\DensityFunction $root, string $name) : void

Compiles $root into a public method $name(float $x, float $y, float $z) : float.

float() ​

php
public static function float(float $v) : string

getBindings() ​

php
public function getBindings() : \pocketmine\world\generator\vanilla\compiler\KernelBindings

VanillaMine is a fork of PocketMine-MP, maintained by Luxury Nodes. Not affiliated with the PMMP team or Mojang.