CameraScript Could Use Some Optimizations For Indexing

I decided to investigate a bit if the Camera Script could use a bit of optimization by storing things like local CFramenew = CFrame.new to prevent the constant index calls, and… well…
Before micro-optimizations:

After:


Before, I had a constant 2.344% (edit: ok, now it is fluctuating between 2.1 and 3.6 when I changed the angle), but after it fluctuates between 0.3% and 1.2% (has gone up to 1.6%-2% though). Both were done in the same Play Solo session, and bizarrely enough had the same song in the background (which isn’t a factor).

Anyway, my point is that the Camera scripts could use micro-optimizations like this, since 2.344% is a probably bigger if you don’t have an i7 4700MQ (Quad core @ 3.25 GHz with Turbo Boost).

I only modified the RootCamera and ClassicCamera scripts by the way.
RootCamera:

local min,max,rad,atan2,abs = math.min,math.max,math.rad,math.atan2,math.abs
local CFramenew = CFrame.new

Classic Camera:

local atan2,rad,min,max,sin,asin,floor,abs = math.atan2,math.rad,math.min,math.max,math.sin,math.asin,math.floor,math.abs
local pi = math.pi
local Vector2new,Vector3new = Vector2.new,Vector3.new
local CFramenew,CFrameAngles = CFrame.new,CFrame.Angles

Yes, this does make the code harder to read, but I think the performance improvement outweighs that.

3 Likes

I think this is a very important point. I’m pretty sure they want the code to be as easily editable and understandable as possible for beginners.

If you arranged things in chunks of things that are related to each other, readability can go up.

local min, max = math.min, math.max
local sin, asin, atan2 = math.sin, math.asin, math.atan2
local floor, abs = math.floor, math.abs
local pi = math.pi
local n2 = Vector2.new
local n3 = Vector3.new
local nc = CFrame.new
local Angles = CFrame.Angles

n2
“n”

why not just
v2 and v3
arent those a bit more readable?

3 Likes

When I optimize stuff like this, I usually use “Vector2_new” and “Vector3_new”

3 Likes

This is a pretty good approach. If I recall correctly, the names of local variables disappear during compilation, so using short names really doesn’t help anything.

It’s a slightly different story for global variables.

2 Likes

When you get error messages it tells you variable names, so it’s stored somewhere.

edit: decided to move my post to the what are you working on thread :stuck_out_tongue:

Local names are stored in the debug data, which can be deleted.
ROBLOX keeps them, so we can get the names of locals when a stacktrace is needed.
(The debug data is also the thing that says “this instruction? that’s this line”)

The length of the name doesn’t matter, both for locals and globals.
(But locals (and upvalues) are still a ton faster than globals)

Yeah basic optimizations should definitely be made as proposed in this thread. I’m surprised it wasn’t done before.

Mind sharing the optimized camerascript?

I don’t have it anymore. I just did “Find and Replace” for CFrame.new, CFame.Angles, Vector3.new, Vector2.new, and math.
If the admins want me to do the index optimizing, then I can do it, as I only did a rough version and ignored parts of the scripts that could be further optimzied (such as Ray.new and Workspace:FindPartOnRay()).

1 Like

You can submit a pull request. GitHub - Roblox/Core-Scripts: All of ROBLOX's core client scripts.

Not exactly much a GitHub user.

How many other script were you running?

That’s a fractional share that you’re looking at. If it’s 2% of idle usage on a small place without many other resource intensive scripts then it really isn’t much… so just the 2% figure doesn’t tell the whole story.

It may not make much of a difference to optimize.

I usually use v2 and v3 for those and n2 and n3 for origins. Not sure why I wrote it opposite like that.

If you want to make some change in CoreScripts, you should submit a PR. If you aren’t a GitHub user it’s never too late to become one.

1 Like

I have a GitHub account it is just I am not 100% familiar with using GitHub.

Neither was I when I wanted some changes for the animation editor and the ROBLOX core UI, but I spent a couple minutes setting up the appropriate things and ended up submitting the pull requests just fine. You’ll need to learn how to use Github anyway if you’re going into the CS field – better to be prepared when you really need it than be rushing to learn how to use it.

You can do it the easy way:

  • Download the CoreScript(s) / PlayerScripts
  • Edit them in your favourite editor, when done, ctrl+a, ctrl+c
  • Press edit online, ctrl+A, delete, ctrl+v
  • Saving will save it to a fork you created.
    (at this point you can quickly look at the “Changed files” to quickly check for mistakes)
    There should be a big button “SubmitPull Request” or something.
    Pressing it (and submitting) is the last step.

That’s how I do it, as the online editor is… less good.
(it’s slow and ctrl+f etc is a pain to use)

(You may have known this all already, but others might not. Doesn’t hurt writing this)

1 Like

:wink:

2 Likes