How to write efficient code?

I dont really belive in formatting
To me code that has too much comments is actually unreadable
Same with variable names, its all preferance and all gets compiled into same bytecode* (function named do get saved for debug in some cases though)

Just name variable something reasonable instead of local hi

2 Likes

There is no one way to write efficient code, this is something you learn overtime from naturally wanting to improve.

So instead of recommending you things to learn, I’ll give you one piece of advice: Write everything from scratch. Each time you make a feature/system, it will get better every time since you come across problems and fix it in the next game.

Overtime, you can research and implement one module, library, framework, paradigm, etc, that interests you to make your workflow better/“more efficient”. That way, you don’t get stuck in optimization hell.

1 Like

Yes the essence of programming is optimization and convenience the most important task of a programmer is to maintain balance of optimization and clean code

1 Like

In a normal compiled language like C, the compiler would just inline this automatically and erase the function’s entire existence.
But Luau’s optimizer is… let’s say “quirky.” (dumb as hell but fast) It rarely inlines, and even when it does, it keeps the function proto hanging around :skull_and_crossbones:

So yeah, if you care about optimization, manually inline your code.
And don’t expect loop bodies to get inlined either. Luau won’t do it for you; you gotta do on your own.

1 Like

You can honestly get both, clean code and cracked optimization.
The only thing you’re not getting is free time, because once you start, it’s over :skull:

Tbh you can just slap together a tiny parser/pre-compiler and turn mid code into a five-star meal for the VM.
Rojo sadly doesn’t let you hook into the VSCode → Studio pipeline (without crazy hacks), but imagine it: full-on #include-style headers for Luau getting inlined, and optimized on the way in. Would be cool as hell.

1 Like

I dont really like “mega optimization” but “advanced” luau optimization it is very desirable to know

Among such optimizations I can note:

  • smart corutine pools
  • basic buffer api or “easy to use” buffer wrappers like BinBuffer (ye my module why not)which allow you to maintain the speed and readability of your script
  • Just think where in your code “bottlenecks”
1 Like

i thought it was clear that the comments were just to mention the changes. i never write comments (your code should be the comments)

consistently applying your preference helps with long term maintenance. same applies if you’re working with a group format.

3 Likes

If im getting optimization i want whole thing, that 65% more optimization per optimization.

I’m literally considering building a preprocessor that inlines everything, kills wrapper overhead, and feeds pure code straight into the Luau bytecode compiler.

Might even add a minifier mode that’s specialized for Luau so the compiler stops crying.

And yeah, I’m not fan of modules either. I’d rather have header-style inline chunks with a pseudo-linker. Zero call overhead, zero indirection. Pure speed.

1 Like

I half agree with you, but Im not a fan of “pure” speed.
But its everyone’s choice write the code you like

1 Like

No one in real world programming on Roblox cares about your “bytecode level optimization”. Tools exist to be used, and those libraries are tools that serve a purpose. ECS is a paradigm that follows specific criteria to be considered ECS. If you consider saving data into a table as ECS, then perhaps everyone who works with anything inside of Roblox - is using ECS lol.

If you want to program in a “pure” and “zero-cost” way, then well, I’m afraid that you are in a wrong environment, as you must want to write in assembly at this point, or even in machine code.

Decades ago people created higher and higher level programming languages for a reason, for it to solve the problem of programming being a rollercoaster of steep entry curves, and those abstractions are meant to serve as a friendly layer of interaction with the machine.

You are jumping into a dynamic high level language, and yell in everyone’s ears around that they should ditch all the language’s sugar and features to write “pure” code, that is completely pointless in real world scenarios.

If you like squeezing every single nanosecond of execution time and cut “overhead”, then good for you, but you shouldn’t mislead people into thinking that it’s the “right way” of programming.

6 Likes

that is NOT more efficient than just doing humanoid.Health what are you on

3 Likes

That literally a pseudo code, replace health with some other state
ECS is the same as functional programming but with a mutable state as i said

1 Like

knowing how dedicated you are to glaze ECS, could do in providing a better example

3 Likes

You literally dismiss everything I say without engaging the core points.

Writing in assembly in 2025 is just slow for the sake of being “hardcore.” Want true insight? Write in C/C++, check the disassembly, see what the compiler actually does. You were close to understanding, but then you took a hard wrong turn.

There are no “tools to be used” in the abstract, there’s only efficient vs inefficient. The modules you praised? They add runtime compromises, extra overhead, and slow things down. My method is objectively superior in multiple measurable ways.

Also, you completely justified illogical practices. Optimization isn’t optional; it’s mandatory, ideally automatic or at least semi-automatic. You did neither.

Also, what exactly do you mean by “real programming”? Because right now it reads like fear: fear that you can’t grasp what I actually do. Throwing buzzwords like “high-level sugar” doesn’t counter real performance issues. Interpreted Luau with a weak bytecode compiler != C++ with decades of automatic optimizations. Apples and nuclear reactors.

3 Likes

ngl i kinda agree with yarik on this one
@trigophi

2 Likes

Arguments backed by facts = ragebait

1 Like

it seems you forgot the main topic and are associating “real programming” with average roblox scripting.
You’re right; the average simulator can get away with ignoring micro-optimizations, or runtime practices.

but op literally asks for efficiency tips.

3 Likes

Performance efficiency generally isn’t important for the vast majority of what you’ll ever work on. What’s more important is efficiency in terms of how code is conveyed: how concise it is, how easy it is to understand.

Name your variables well. Organize your code into modules that are by themselves easy to understand. If there’s ever any section of code that seems overly complicated or overly messy, most of the time, there are ways to make it a lot more manageable.

This by itself should get you pretty far along.

2 Likes

Personally, I love using single script architecture even though, there be a slight dip in performance, the modularity and organisation you can gain from it is awesome. I don’t see any need to fully avoid it.

1 Like

You answered own question

Reply:

2 Likes