Enable --!native for clients

Problem

As a Roblox developer, it is currently too hard to write performant LUA code that works on low end devices, especially when there’s lots of moving parts on the client or when the client is processing a heavy workload.

Use Cases

The following use cases are presented.

Use Case 1

In my game, depending on the map, I have quite a few items that float up and down on a sine function, and rotate. All the items get this treatment for each tick of RunService.RenderStepped. Therefore, I have to run a for loop inside the event handler. I have done some tricks such as pre-processing as much of the data before hand as possible and not running the loop every time to help alleviate this, but it’s still an issue on lower end machines. This is generally a compute intensive task as the delta time from the engine is used to calculate the angle increment from the parameters on the parts themselves.

Use Case 2

Some on demand effects, such as lightning, are compute intensive as the position and orientation of each part that makes up the bolt must be calculated independently, and it relies on the location and orientation of the previous part so it can link up and look visually correct.

Additional Information

Although Roblox does support Parallel LUA, there are some very strict requirements in place which makes using Parallel LUA for effects mostly impossible since properties of instances cannot be written to in parallel mode, which is a design limitation albeit for thread safety for the engine. This means that even when using Actors to separate code into multiple threads, one is still forced to enter single thread mode to run the effects.

Conclusion

If Roblox is able to address this issue, it would improve my development experience because my code will run more efficiently on lower end devices and not bury the device in work that it will never catch up on.

19 Likes

I don’t understand this feature request. What is it that is being requested? --!native can already be declared at the top of the script to enable native codegen - and it wont always provide that big of the performance boost depending on what is being executed.

6 Likes

It only works for servers, not clients.

11 Likes

iirc, a staff member said they were already looking into doing this

6 Likes

Clients have actors, servers have native codegen.
Sadly its too difficult to write exploit-free native codegen processing code.

e.g. a few months ago a bug existed which shifted variables registers so:

local a = buffer.create(16)
local b = 1

In this code, if you did certain buffer writes, the registers would shift and “a” would get shifted to have the value of “b”. (this was fixed a long time ago dont get too hyped)

Now imagine if this was used to gain access to certain restricted items — maybe this ends up in a sandbox escape — things would go south pretty fast.

Roblox’s reputation would be highly damaged after an attack causes some devices to get infected.

Im guessing the reason this feature was released on servers in the first place was because they are sandboxed and security measures everywhere.

We probably have to wait a few more years until the developers are sure there are no sandbox escapes or native code is fully sandboxed.

8 Likes

Oh I completely overlooked that lol, my bad.

3 Likes

what native does anyway?

llllllll

3 Likes

It’s a directive to the engine to compile the script into native machine code instead of the byte code for interpretation. Machine codes runs directly on the CPU at CPU speed, which significantly increases the performance of the code over interpretation.

3 Likes

--!native is a flag that compiles Luau bytecode to direct machine assembly through a process called JIT compilation. The issue is Apple.

Apple. Apple and its hate for JIT compilation

6 Likes

PHP and JavaScript do this as well. I think other interpreted languages like Python and Ruby also do this. I haven’t heard anything about Apple hating JITC. Would you please elaborate? Have they stated any reasons?

2 Likes

apple only allows jit on web browser apps. they are pretty strict with this stuff as it could lead to security vulnerabilities, being able to generate executable code on the fly

2 Likes

Lua doesn’t use a stack because it’s register-based (it uses a return stack for function calls, but that’s unrelated). I have no idea what bug you are talking about because shifting registers should be impossible since they’re unsequenced.

Luau native codegen isn’t JIT-compiled, it’s AOT-compiled.

Still, Apple does not permit running dynamic assembly on its chips, to the hardware level. This is the same reason Minecraft had to be rewritten in C++, because the JVM wouldn’t work on iOS.

4 Likes

The bug I was trying to explain was on the native codegen announcement

It was also a weird bug, never got to reproduce it with simpler steps as whenever I changed even a loop it stopped functioning that way.

WheretIB had also verified this was a bug (yippe)

Yes, I meant the registers sorry for the misinformation. Will fix that now. :sweat:

3 Likes

Here’s the issue that I have. Since the code is precompiled before the client downloads it, I wouldn’t call that dynamic assembly. The code is compiled on publish and then sent to the device when the device logs into the server. I don’t see the problem here.

I recall something about Roblox looking into client-sided native code.
I will bump this topic because it’s something I absolutely need.

I barely benefit from server-sided native code at all because the heaviest and most expensive logic in on the client.

While they’re at it, it would also be nice if they removed (or increased) the memory and instruction limit.
I want to choose when and what code becomes native.

I don’t use native codegen for everything, but Roblox still decides how many functions and whatnot you can use it on.
If you exceed the limit you have to start un-nativizing functions which is annoying.

What if 50% of my project can benefit from the native codegen? Just let me use it on half the modules then.

5 Likes

as? the native attribute is not for the client? I thought so, because I put it in my scripts and in the scriptProfiler section I get which indicates that it is native. I have lived a lie hahaha

1 Like

As far as I know, native code currently only works on the client in studio.
In live games everything on client is still plain Lua(u).

There’s also an rather annoying limit for how much code you can make native.
Once you exceed a certain memory or instruction threshold it gives a warning or error and some functions don’t become native.

I wish Roblox had a “I know what I’m doing, remove my limits please!” option.

5 Likes

You can use @native to tag functions to use native code gen. Client code gen is not easy since luau doesn’t use llvm

1 Like

I’ve already been using @native, but sometimes it turns out I might have more math functions than I anticipated.

I manually tag all of my functions and modules, function by function.
Tedious, but it gives the most control.

1 Like

This is a bit off-topic, but until we await the native code generation on the client (if it ever will), I think we mind looking into something else called strict type checking. With proper types, we can remove some overhead for the interpreter and improve performance a bit. This works especially great for primitive data types

5 Likes