Performant Design | Spend less time optimizing your code later, and design it so you don't need to optimize

this post is targeted towards intermediate/experienced scripters, not beginners

A big problem with the common Roblox’s developer on performance is “you shouldn’t optimize something unless you need to”, and while this is somewhat okay for less ambitious games, you really should focus on designing your code to not require optimization in the first place.

For example, let’s design a small physics-networking system similar to Roblox’s. No code yet, so let’s just lay down some design:

Step 1: Each physics object checks if players are near it
Step 2: If there is a player nearby, transfer network ownership
Step 3: Client receives that, and starts doing physics calculations for the object.
Step 4: The client sends updates every frame.
Step 5: The server runs checks to make sure the client is accurate- if it’s inaccurate, send an update to the client.

If we’re trying to design an efficient system, there’s a lot of issues with this model of network ownership.


Step 1 has an issue: why is each physics object getting the closest players? Assuming there’s more physics objects than players, it would make more sense for the server to check if there’s players in a certain radius.

Takeaway: Design your code to do less- if there’s a route to overall do less, then take it. Sometimes you don’t need to do certain operations when you can design your code to work less.


Step 4 has an issue: why do we need to calculate it every frame? Shouldn’t we just update it when it changes, instead of wasting bandwith on the same value?

Takeaway: Try to take away redundant code. If something is the same, then don’t update it. Don’t put out needless updates with things you already know.


Step 5 has an issue: Why are we sending an update to the client when the client is either 1. exploiting or 2. incorrect- in which case the calculations will instantly realize it’s wrong, and adjust accordingly?

Takeaway: You don’t need the full context to do something. You can rely on systems you wrote to do the work for you- you don’t need to be perfect with 100% accuracy every time.


Overall, you shouldn’t need to traditionally “optimize” your code. You should have already done it by thinking “hey, this probably isn’t the best idea, I should try something more efficient” before you even write it. This means you don’t need to gut out systems and you’ll have less issues with performance later on, because you thought about performance before even writing the code. Just a simple “this is probably bad, I should reduce the load” is enough to save time later rewriting and optimizing code, with zero effort spent on actually optimizing it.

7 Likes

It’s easier to know the fastest and most efficient ways you could’ve done something after you’ve done something, usually much longer after. Hindsight is 20/20 and this is misleading.

Those who are learning can’t immediately “optimize” their unwritten code when they have no benchmarks to compare, support or understanding of coding to begin with. Yes, there may always be a “more efficient/optimized way” but it’s not something every programmer ever can immediately account for or do.

The rest of the post doesn’t raise any flags for me but it might for someone else.

3 Likes

This is partially true, but only at a lower level than what I am talking about. It should be common sense that 25 distance checks is better than 50 distance checks, less operations is faster than more operations, etc. etc. I’m not talking about method-specific things like octrees vs. spatial hashing, I’m talking about really basic design choices.

Literally the very point of the post is “design your game in a way where you don’t need to worry about how fast specific ways of doing things are”.

Also this post isn’t targeted towards those learning to script, this is targeted towards intermediates/advanced scripters.

1 Like

It’s in Community Tutorials with no targeted audience laid out in the post. Not only that but technically 50 distance checks would be less performant and contradictory to your post. It’d be better for security (depending on the method) but worse processing-wise.

Current version of OP’s Post as of: September 7, 2022
(turns out this is broken, the timestamp was set for 08:41AM on 22-09-08

Please don’t argue semantics, you already know that was a small mistake.

Also no, in this specific case it would mean literally nothing because if you’re doing any radius checks, it’s impossible to need to do anything larger than the radius check.

Don’t be hostile and argue semantics / small mistakes like that, it’s not appropriate for the forums, and it shows you’re not interested in being constructive.

1 Like

I stated it as a fact of the matter rather than hostilities.
If that’s how you interpret me just flagging down something then that’s a red flag itself.

I’m not continuing this discussion anymore, but it’s not appropriate to comment on an extremely small mistake like that and then point out it’s “better” in one way which is false. You don’t like this post’s philosophy, great. Don’t take it personally and start trying to attack replies because of that.

Alongside that, the ninja edit was really unnecessary, and pretty hostile. I was going to reply “valid point” or something along the lines of that- I didn’t even have time to do that before you edited :joy:. That makes it extremely clear you care about the optics on this discussion, which is pretty immature- I’d rather it be deleted off this post because it’s unnecessary, and for the most part pointless.

1 Like

All due respect, I don’t disagree with the post itself. It’s a good resource.

Either way, I hope you expand on this tutorial with more use cases and in depth explanation for more people. Pre-consideration before script-writing is an important issue that many advanced programmers run into. It’s one of those things where it’s not only situational but may lead to learning about new methods and means for sorting functions, objects and scripts as a whole.

You could also expand into Don’t-Repeat-Yourself principles themselves and talk about creating libraries that can be used by multiple scripts for functions that can be universally used and optimized (e.g. Region/Bounds checking library modulescripts). There’s several things you could expand on further for intermediate and advanced programmers who could use the information in a collected tutorial.

as for the ninja edit, it wasn’t even hidden. I didn’t add the timestamp was all. It was there even from the first version of the post, something you could’ve clearly caught from the nameless details arrow.

For the " extremely small mistake like that and then point out it’s “better” in one way which is false" What I said was in reference to the means of what someone were to do with 50 “distance checks” (which at the time wasn’t in reference to radius checking).

1 Like

I think you are misinterpreting what the OP is trying to say. The post to my knowledge is talking about the systems’ efficiency, which is something always targeted at advanced (generally software engineers) scripters than beginners.


I think that would just be putting information in a place it doesn’t belong to. This post is talking about designing systems efficiently, not just “tips to optimize”.


edit: spelling fixes

2 Likes

D-R-Y principles could be linked to. Even if the idea of splitting off repeated functions into accessible scripts (as an example of D-R-Y) is not considered as part of the topic by OP, it would still be a useful thing for intermediate-advanced scripters to begin with and it isn’t something beginner scripters generally learn (for better or worse) and ties into both optimization as well as prior thought/design.

[EDIT] It is generally considered intermediate/advanced scripters would know this stuff but DRY as part of the learning process is surprisingly hit and miss depending on various factors.

DRY is important, but it isn’t still under the scope of the post, and therefore, shouldn’t be talked about or even linked to imho.

3 Likes