It is currently November 15th, 2019, 11:25 am

Snow simulator optimization [Solved]

Help with creating, editing & fixing problems with skins
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Snow simulator optimization [Solved]

killall-q » December 5th, 2013, 11:42 pm

I made a snow simulator, and, as is usual for this type of skin, the problem is optimization. With this one, however, it doesn't seem to be a problem of too many measures/meters or bangs, but something more mysterious.

In normal operation, this skin will cause Rainmeter to quickly consume increasing amounts of RAM and slowly bog down the whole system, even if I reduce the number of snowflakes. However, while testing, I found a peculiar thing: if the meters were restricted to top of the screen, Rainmeter wouldn't even sneeze at performing 820 !MoveMeter bangs a second, double what I was asking of it. Once I let the flakes go all the way down the screen, or moved the starting Y position further down, CPU usage increased, without changing anything else.

EDIT: It's fast and buttery smooth! Thanks for the replies Smurfier.
Snowfall 0.9 RC.rmskin
EDIT2: I played with calculating coordinates directly instead of using GetX/Y and it seems slightly faster. It's so hard to tell when performance is already so good :D Also tested !MoveMeter again and it's horrible, this wouldn't have been possible without SetX/Y.
You do not have the required permissions to view the files attached to this post.
Last edited by killall-q on December 7th, 2013, 11:01 pm, edited 3 times in total.
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Snow simulator optimization

smurfier » December 6th, 2013, 12:12 am

I am not experiencing any increase in memory usage or cpu load while running your skin. I even changed from using !MoveMeter to using SetX() and SetY() to experiment.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Re: Snow simulator optimization

killall-q » December 6th, 2013, 12:29 am

smurfier wrote:I am not experiencing any increase in memory usage or cpu load while running your skin. I even changed from using !MoveMeter to using SetX() and SetY() to experiment.
You aren't having any problems running full bore? Flakes=40, Rate=5, Rate2=5? Even Update=50? I put instructions in the [Variables] section, it's initially set to safe mode. Did you inadvertently optimize it for me in your testing? :D What's your system specs?

I've tested !MoveMeter, !SetOption, and SetX/Y(), and I've only noticed slight performance differences between them. !SetOption seems to be faster than !MoveMeter, but !MoveMeter is faster than 2 !SetOptions. Can't be certain, but SetX/Y() seems to be slower than both + the overhead of having to GetMeter() every meter.

I am running Rainmeter 64-bit by the way.
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Snow simulator optimization

smurfier » December 6th, 2013, 12:46 am

8gb ram, 2ghz processor.

Changed the variables to what you stated, no change. However, apparently jsmorley had the same issue as you.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Re: Snow simulator optimization

killall-q » December 6th, 2013, 1:00 am

smurfier wrote:8gb ram, 2ghz processor.

Changed the variables to what you stated, no change. However, apparently jsmorley had the same issue as you.
You must have some kind of blessed system. Or you're running AMD. *snickers*

I have a 2.3 GHz processor and 16Gb RAM.

Increased memory usage wasn't so monstrous overall; but while Rainmeter.exe hovered at 45Mb, once the skin loaded it would climb to 60-70Mb, fluctuating to a peak of 120Mb. I accidentally left it running last night and woke up to find that Windows had closed Rainmeter with an out of memory error.
Last edited by killall-q on December 6th, 2013, 1:14 am, edited 1 time in total.
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Snow simulator optimization

smurfier » December 6th, 2013, 1:12 am

I'm not sure why but the changes I made decreased my cpu load by 2%.

Code: Select all

function Initialize()
	t, n, Wind, Flakes, Rate, ScreenW = 0, 0, 0, tonumber(SKIN:GetVariable('Flakes')), tonumber(SKIN:GetVariable('Rate')), tonumber(SKIN:GetVariable('SCREENAREAWIDTH'))
	StartY = tonumber(SKIN:GetVariable('StartY')) --TEST
	tv = math.floor(SKIN:GetVariable('SCREENAREAHEIGHT') / (Flakes * SKIN:GetVariable('Rate2')) + 0.5)
	Meters, Coordinates = {}, {}
	for i = 1, Flakes do
		table.insert(Coordinates, {x = 0, y = 0,})
		local mt = SKIN:GetMeter('F' .. i)
		table.insert(Meters, mt)
		mt:Show()
	end
end

function Update()
	t = (t + 1) % Rate
	if t == 0 then
		n = n % Flakes + 1
		Coordinates[n].x = 0
		Coordinates[n].y = tv + math.random(0, 6) --Comment out random factor as desired for testing.
		Wind = math.abs(Wind) < 24 and Wind + math.random(-4, 4) or math.random(-20, 20)
		Meters[n]:SetX(math.random(-Wind * 20, ScreenW - Wind * 20))
		Meters[n]:SetY(StartY)
	end
	for i, meter in ipairs(Meters) do
		Coordinates[i].x = Coordinates[i].x + math.random(-1, 1)
		meter:SetX(meter:GetX() + (Coordinates[i].x + Wind))
		meter:SetY(meter:GetY() + Coordinates[i].y)
	end
end
Note that your meters will need to be named 1 through 40 instead of being 0 through 39. You'll also need to add DynamicWindowSize=1.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Re: Snow simulator optimization

killall-q » December 6th, 2013, 2:36 am

Those are definitely logic improvements. I have yet to wrap my head around the special behaviors of and/or in lua.

You must be running a dev version of RM, as SetX/Y() doesn't work for me for the last for loop. Flakes just blink into existence at the top of the screen and disappear on both 3.0.2 and 3.1 r2187. Once I swapped it out for !MoveMeter it worked much the same as before, with memory usage rising to 60Mb+ within 30 seconds of refreshing on unsafe settings. The system feels slightly less stuttery while bogged down, though.

Edit: Whoops, didn't see the DynamicWindowSize=1. Added that with SetX/Y() and it held on for much longer, CPU roaring but system not getting slowed till almost 2 minutes later.
Last edited by killall-q on December 6th, 2013, 3:09 am, edited 1 time in total.
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Snow simulator optimization

smurfier » December 6th, 2013, 2:59 am

Did you add DynamicWindowSize=1 to the skin's [Rainmeter] section? The !MoveMeter bang forces Rainmeter to recalculate the size of the skin window while SetX\Y does not.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Re: Snow simulator optimization

killall-q » December 6th, 2013, 8:13 pm

PROBLEM. &#*$^. SOLVED.

I replaced DynamicWindowSize with a dummy meter that preallocates space for the skin.

Code: Select all

[AllocSpace]
Meter=Image
W=#SCREENAREAWIDTH#
H=#SCREENAREAHEIGHT#
All symptoms of buffer overflow vanished. I'm able to push it to 40 frames a second and 86 particles (the max allowed before meters start disappearing prematurely) with the CPU almost idling. That's 6880 SetX/Y operations a second. I also applied a few Lua optimizations from here. See OP for new version.

I'm pondering on the best way to add multi-monitor support, which would require communicating Wind to child instances whenever it's changed.
User avatar
fonpaolo
Moderator
Posts: 1343
Joined: April 11th, 2013, 8:08 pm
Location: Italy

Re: Snow simulator optimization [Solved]

fonpaolo » December 7th, 2013, 9:48 am

Sorry if I'm a little OT, even if it's somewhat related.
I have always forgot to ask, since I'm not using much skins designed by others and I have created a skin suite with 22 individual skins (not counting sub-skins expansions) and one skin to control/setup of everything, by testing it on some systems (32 and 64 bit), I have noticed a memory usage varying from 50 up to 120 Mb, normally around 80/90 Mb. (64 bit seems uses more) depending on how much and what skins are used more (music player is using more), while the CPU utilization is at more or less one or two minutes after a day.
I use DynamicVariables=-1 on almost everything isn't updated (background, simple text, icons and some measures that aren't changing)
Is this a good optimization?