Profiling

From GiderosMobile

Gideros comes with an integrated native lua profiler.

For those who don't know what is a code profiler, it is a tool that measure effectiveness of the code and outputs statistics about the CPU usage: number of times each function was called, how much time it took to execute, and where was it called from.

Gideros provides two means of enabling profiling:

  • By launching a profiling session directly through 'Profile' menu item or toolbar icon from Gideros Studio
  • By calling Core.profiler* API from lua

In the former case, Gideros Studio will display the profiling results when the stop button is hit in Gideros Studio toolbar. In the latter, you have to handle profiling data collection and display by yourself.

Note that even if you started profiling through Gideros Studio, you can still use Core.profilier* API within your code. Gideros profiling API available from lua is:

  • Core.profilerStart(), which instructs gideros start collecting profiling data
  • Core.profilerStop(), which pause collecting
  • Core.profilerReset(), which clears collected data
  • Core.profilerReport(), which returns a table containing collected profiling data


Enough said about the internals, let's see how profiling can help us!

So suppose you are running the following code:

local function myFunction(a)
	local function c(a) return math.cos(a*math.pi/180) end
	return c(a)+c(a+1)
end

and happen to use it very often. It is far from being optimized (on purpose, right) and turns out to be slow, but how can we do better ?

Lets ask the profiler how does it run, say we want to run it 100000 times:

for i=1,100000 do myFunction(i) end

This code outputs:

#TimeRatioCountFunctionLocation
[1] 423 0% 1callFile=[C] 00421900(callFile)
423100% 1Unknown@main.lua:0:033FAC64
 
423100% 1callFile=[C] 00421900(callFile)
[2] 423 4% 1Unknown@main.lua:0:033FAC64
405 96%100000myFunction@main.lua:26:033FD87C
 
405100%100000Unknown@main.lua:0:033FAC64
[3] 405 35%100000myFunction@main.lua:26:033FD87C
265 65%200000c@main.lua:27:033FC328
 
265100%200000myFunction@main.lua:26:033FD87C
[4] 265 60%200000c@main.lua:27:033FC328
107 40%200000cos=[C] 6E416FE0(cos)
 
107100%200000c@main.lua:27:033FC328
[5] 107100%200000cos=[C] 6E416FE0(cos)
 
[6] 2100% 313gideros_enterFrame=[C] 00423050(gideros_enterFrame)
 
[7] 0100% 1broadcastEvent=[C] 00423340(broadcastEvent)
 

This is a list of all functions ran by the code sorted by CPU usage (highest first). The first chunk is about a function called callFile. It is an internal Gideros Function which loads each lua file. In our case it was used to launch main.lua, which corresponds to block [2]. But let's focus on our function myFunction, we can see it is covered by block [3]:

#TimeRatioCountFunctionLocation
405100%100000Unknown@main.lua:0:033FAC64
[3] 405 35%100000myFunction@main.lua:26:033FD87C
265 65%200000c@main.lua:27:033FC328

So the function myFunction took, as expected, most of the processing time. Figures tell us that it was called 100000 times and consumed 405ms on CPU. We also learn that it was called from an unnamed function located at main.lua:0, which is the toplevel lua code of main.lua file, and that all the time (100%) spent in myFunction was due to being called by main.lua. More interesting, we discover that 65% of the time (265ms) was spent in calls to function c located at main.lua:27, and that c was called 200000 times, twice per call.

The next chunk is about function c: 40% of the time of c was spent in math.cos(), and 60% in the function itself.

We now that calling a function is expensive, and that c function is rather simple. Lets try to inline it:

local function myFunction(a)
	return math.cos(a*math.pi/180)+math.cos((a+1)*math.pi/180)
end

Now profiler report looks like this: (truncated)

#TimeRatioCountFunctionLocation
65100%100000Unknown@main.lua:0:033FC4FC
[3] 65 73%100000myFunction@main.lua:26:033FB670
17 27%200000cos=[C] 6E416FE0(cos)
 
17100%200000myFunction@main.lua:26:033FB670
[4] 17100%200000cos=[C] 6E416FE0(cos)

myFunction is already faster: 65ms now where we had 405ms before. We still call math.cos two times. Lets make it (and math.pi) local.

local function myFunction(a)
	local cos=math.cos
	local pi=math.pi
	return cos(a*pi/180)+cos((a+1)*pi/180)
end

and profiler report:

#TimeRatioCountFunctionLocation
60100%100000Unknown@main.lua:0:033FAC64
[3] 60 71%100000myFunction@main.lua:26:033FD1E8
17 29%200000cos=[C] 6E416FE0(cos)

We saved time in myFunction, while cos() took the same time.

What if we use new gideros deg to rad operator instead of math.pi and 180 constant ?

local function myFunction(a)
	local cos=math.cos
	return cos(^<a)+cos(^<(a+1))
end

and profiler report:

#TimeRatioCountFunctionLocation
57100%100000Unknown@main.lua:0:033FC4FC
[3] 57 69%100000myFunction@main.lua:26:033FB9DC
17 31%200000cos=[C] 6E416FE0(cos)

We saved a few more precious milliseconds!

As you can see, the profiler is an useful tool to spot CPU bottlenecks in your app, and some times a few minor changes in the code can make big differences: avoid table access, avoid function calls, avoid duplicate computations.