Grass System: looking for advice on how to lower script activity

I suggest creating a custom grass cache.

When you’re creating a handful of parts, it can really slow down the engine. So when the player moves far away from certain grass, instead of destroying it, I suggest parenting it to a folder called “Cache” and setting the primary CFrame of the grass to 99999.

At the line in your code where the script needs more grass, instead of making new grass everytime, check the Cache folder for old grass, then just set the CFrame to where it needs to be.

Bonus points if you do this all on the client, for ~2k parts my operation time (how long it took to generate) went from 0.035 seconds to 0.022. Also dropped my performance stats Receiving and Networking.

3 Likes

I’ll definitely try that out. Would moving it to Lighting or replicated storage also work? Or is it best to just set the CFrame to 99999 as you suggested?

I mean this can help a little bit.
You should cache your functions since you are going to be firing them a lot.

local math_random = math.random
local CFrame_Angles = CFrame.Angles

You can read up on this here


Access to external locals (that is, variables that are local to an enclosing
function) is not as fast as access to local variables, but it is still faster than
access to globals. Consider the next fragment:

function foo (x)
	for i = 1, 1000000 do
		x = x + math.sin(i)
	end
	return x
end
print(foo(10))

We can optimize it by declaring sin once, outside function foo:

local sin = math.sin
function foo (x)
	for i = 1, 1000000 do
		x = x + sin(i)
	end
	return x
end
print(foo(10))

This second code runs 30% faster than the original one.


3 Likes

Didn’t know that, will definitely try this since I use a lot of those throughout my code. Thanks for advice!

Doesn’t matter, I got it to work thanks to you man. It spawns and despawns without any frame drops now!
Thanks for the help!

1 Like

I was able to knock activity down from 15% to 13% so thanks for the advice! Will remember to do that in the future.

1 Like

You can do this but I found it to be a little slower. Setting CFrame to 99999 works better in my opinion.

1 Like

You might also do a global hash table of all the possible Angles for your CFrame and then only change the hash table if weather conditions change.

You should checkout this module: PartCache, for all your quick part-creation needs It should simplify the caching process.
It was created for efficiently “creating” many projectiles inside your game. But can be implemented for other stuff to.

You do realize that the optimizations you mentioned aren’t even relevant in Luau in the first place? Luau VM is way more optimized than Lua currently is.

Benchmark:

local sin = math.sin

function foo (x)
	for i = 1, 1000000 do
		x = x + math.sin(i)
	end
	return x
end

function foos (x)
	for i = 1, 1000000 do
		x = x + sin(i)
	end
	return x
end

e = os.clock()

foo(1)

print(("Not Cached: %s"):format(os.clock() - e))

e = os.clock()

foos(2)

print(("Not Cached: %s"):format(os.clock() - e))

Cached: 0.036631599999964
Not Cached: 0.036271200002375

1 Like

Could you make the grass script a plugin or open sourced? Because it looks cool. And Looks cooler than when I use boatbomber’s WindShake module on grass!

I’ll make it open sourced if I can get the script activity below 5%.
As it stands I’ve added better working grass since this post, but it runs at the cost of 15-17% script Activity.
Heavily researching how to reduce this number, but having very little luck at the moment.

1 Like

Any progress on lowering the activity?

I have lowered it from 15-17% to 9.5-10.5%
Still working on it, but it’s difficult since there isn’t many people willing to put time into figuring it out with me. I’m looking into a new way to simulate a similar style with the movement code by changing it so that 6-8 patches of the grass all move in sink, meaning it’ll take up less activity since it won’t be individual. However I’ll only pursue it after if it looks as good as the current.

You have an enormous amount of if/elseif statements, is there any way you could reduce on those?
Check out the Benchmark plugin, I think it has a profiler to see which operations in your code take up the most.

I have made changes to it since then. I’ll quickly change them.

Is the grass spawned on the server or client? You can switch to spawning the grass entirely on the client for better activity.

It’s on client, since I didn’t want players to see another player’s area of grass which would cause performance issues and wouldn’t look right.

I’ve experimented with a new method for the code, which makes the tick rate a lot more smooth and decreased script activity from 9.5%-10.5% to 6%-7.5%. Very close to finalizing it, but obviously the last 2% needed will still take a while to get working.

Any more progress on lowering the script activity?