Luau Native Code Generation Preview [Studio Beta]

This issue has been fixed, sorry for the confusion we caused.

2 Likes

Code making heavy use of vector3s appear to run worse when using --!native, an extreme example being:

local t: Vector3 = Vector3.new(0, 0, 0)
local a: Vector3 = Vector3.new(1, 2, 3)
for z = 1, 10 do
	wait()
	local t0 = os.clock()
	for i = 1, 1000000 do
		t += a; t += a;
		t += a; t += a;
		t += a; t += a;
		t += a; t += a;
		t += a; t += a;
	end
	print(os.clock() - t0)
end

runs about 20% slower with --!native enabled.

Seeing some recent changes to the Luau GitHub repository, will it be soon that Vector3’s get some much desired performance increases?

Unfortunately, Vector3 support is still lacking and we couldn’t deliver the improvements in December as previously mentioned.

As a workaround, in same cases you can wrap into a function with Vector3 arguments:

--!native

local function work(t: Vector3, a: Vector3)
	for z = 1, 10 do
		wait()
		local t0 = os.clock()
		for i = 1, 1000000 do
			t += a; t += a;
			t += a; t += a;
			t += a; t += a;
			t += a; t += a;
			t += a; t += a;
		end
		print(os.clock() - t0)
	end
end

local t: Vector3 = Vector3.new(0, 0, 0)
local a: Vector3 = Vector3.new(1, 2, 3)

work(t, a)

No, it will not be soon.

2 Likes

I suppose one final question,

When is native Luau planned to release to servers?

1 Like

We plan to release it this year.

5 Likes

“It is written: ‘Lua shall not live on interpreter alone, but on every instruction’”

  • John Lua.

Anyways, Great job on this! Super interesting and I will look through the opensource luau-codegen on GitHub and experiment on it.

One thing I wish for is struct’s and union’s like in nelua, which does compile to C. It could result in huge memory performance increases when used correctly.

Compilers & Transpilers or just low-level programming has been an interest of mine. How does native codegen handle dynamic typing, do they all just have an accompanying variable that holds the type and the value itself is stored as a int?

Each value has a corresponding type tag alongside with it.

2 Likes

Getting this error

Native code generation of script =ReplicatedStorage.WindShake.Octree.OctreeRegionUtils failed: Allocation error. Script will be interpreted.

You need to make your script smaller.

Alternatively, if you have a lot of native scripts, you might have to disable it for some of them to get back under the memory limit.

We’re back, baby. The code I am infamous for is making its return.

In the before times (<2020), code using three local variable numbers was faster than using Vector3.
Then, with the release of Native Vector3 in 2020, Vector3s and three local numbers performance converged, and with that I switched over to using Vector3s for performance sensitive code, just for the increased readability.
Then, in 2023 with the release of Native Code Compilation, three local variables are once again the fastest by a factor of 3x in real code, and I have changed our modules to reflect this.


We’re going from 6us to 2us, amazing for a dynamically typed language like Luau.

I am ecstatic, and cannot wait for the release of --!native.

8 Likes

Any updates on Vector3 performance?

2 Likes

Will we finally be able to create classes?

Native Code Generation is for Luau, it is not Roblox-specific. So, no, I don’t think this would let you create your own Roblox classes.

2 Likes

Vector3 now generates native code when type annotations are used and can be figured out by the compiler.
If type annotations are not used, performance has been improved to not be too far behind the VM.

We will try explain which type annotations work best in the codegen compiler in a future post/documentation.

You example now runs faster in native with no modifications.

8 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.