Oolite Bulletins

For information and discussion about Oolite.
It is currently Fri Sep 22, 2017 9:56 am

All times are UTC




Post new topic  Reply to topic  [ 51 posts ]  Go to page Previous 1 2 3 4
Author Message
 Post subject: Re: OXP Performance tips
PostPosted: Sat Jul 08, 2017 9:15 am 
Offline
---- E L I T E ----
---- E L I T E ----
User avatar

Joined: Tue Mar 03, 2015 11:35 am
Posts: 482
Location: Paris
Quote:
Cool! And certainly 'not obvious' (to me, at least). Remember most of us are self-taught wrt JScript - at first I thought that was another keyword, like self in Python :)
I had the same problem the first time I encountered "that" :lol:
Quote:
So you're basically storing a (far) reference as a function's property. Would it be safe to say, for clarity, that
Code:
var floor = (that.floor = that.floor || Math.floor);
is logically equivalent to
Code:
if( that.floor === undefined ) that.floor = Math.floor;
var floor = that.floor;
Exactly!
Quote:
You get some of the speed gain of a closure (where these references are instead, stored in persistent local variables), without all the overhead ( & drama!), with WorldScriptsGetProperty being a most expensive one. This should definitely appear before any discussion of closures in your wiki.

As an example, I put the above change on a single call to _dump_map(), which is in a different .js file and went from:
Code:
Total time: 6.541 ms
JavaScript: 3.824 ms, native: 2.711 ms
Counted towards limit: 5.06183 ms, excluded: 1.47917 ms
Profiler overhead: 1.719 ms
                                                        NAME  T  COUNT    TOTAL     SELF  TOTAL%   SELF%  SELFMAX
                                 (cagsdebug.js:28) _dump_map  J      1     5.85     2.55    89.5    39.0     2.55
                                                   GlobalLog  N      2     0.81     0.74    12.4    11.4     0.39
                                (cagsdebug.js:36) number_str  J     51     0.44     0.44     6.7     6.7     0.20
                       (telescope.js:796) _RelativeDirection  J     17     0.96     0.30    14.6     4.6     0.06
                                           EntityGetProperty  N    166     0.54     0.29     8.2     4.4     0.01
                                      (cagsdebug.js:29) dist  J     17     0.90     0.23    13.7     3.5     0.06
                                     WorldScriptsGetProperty  N      3     0.25     0.23     3.8     3.5     0.12
-[NSObject(OOJavaScriptConversion) oo:jsDescriptionWithClassName:]  N     17     0.22     0.22     3.4     3.4     0.02
                      (telescope.js:1269) _detect_distanceTo  J     17     0.56     0.21     8.5     3.3     0.04
                                                          ...
to:
Code:
Total time: 4.622 ms
JavaScript: 1.981 ms, native: 2.634 ms
Counted towards limit: 3.11484 ms, excluded: 1.50716 ms
Profiler overhead: 1.411 ms
                                                        NAME  T  COUNT    TOTAL     SELF  TOTAL%   SELF%  SELFMAX
                                 (cagsdebug.js:28) _dump_map  J      1     4.08     0.99    88.2    21.5     0.99
                                                   GlobalLog  N      2     0.86     0.79    18.6    17.2     0.48
                       (telescope.js:796) _RelativeDirection  J     17     0.94     0.31    20.3     6.7     0.08
                                           EntityGetProperty  N    166     0.52     0.27    11.3     5.9     0.01
-[NSObject(OOJavaScriptConversion) oo:jsDescriptionWithClassName:]  N     17     0.24     0.24     5.1     5.1     0.03
                      (telescope.js:1269) _detect_distanceTo  J     17     0.55     0.21    11.8     4.6     0.04
                                     WorldScriptsGetProperty  N      2     0.20     0.19     4.4     4.2     0.11
                                      (cagsdebug.js:29) dist  J     17     0.83     0.19    18.0     4.1     0.05
                                (cagsdebug.js:36) number_str  J     51     0.19     0.19     4.0     4.0     0.03
                                                            ...
Not only did I lose an expensive WorldScriptsGetProperty but the functions that _dumpmap() itself called (in that same .js file) also run faster (due to interpreter optimizations).

Anyone who has multiple .js files in their oxp should try this as a first step; the speed gain is quite high vs the effort it takes!
:mrgreen:


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Tue Jul 11, 2017 12:16 am 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
We should make a list (a reference, not a tutorial) of faster alternatives:
  • ship.checkScanner() is "usually considerably quicker than system.filteredEntities()" according to the wiki
    There are also system.entitiesWithScanClass, system.shipsWithRole and system.shipsWithPrimaryRole which are much faster too.
    (entitiesWithScanClass is about 10x faster). And if all you need is the #, there are partner fns countEntitiesWithScanClass,
    countShipsWithRole, and countShipsWithPrimaryRole
  • missionVariables are extremely slow, some say 60 x's (profiling shows their only half as slow as WorldScriptsGetProperty :shock: )
  • x < 0 ? -x : x; instead of Math.abs(x), is over 10 x's faster.
  • x < y ? x : y; instead of Math.min(x, y), is over 12 x's faster (same for max).
  • x.toFixed() instead of Math.round(x), is almost 2 x's faster, if you don't mind the result being a string. (same for floor((x-0.5).toFixed()) & ceil((x+0.5).toFixed())). We're gaining microseconds, so disregard :?
  • don't use delete to free memory for garbage collection, x = null; will do
I'm sure there are lots of others out there but no central repository. Sifting thru BB search results has to be one of the most inefficient methods of information gathering Man has ever devised.

_________________
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Thu Aug 17, 2017 3:35 am 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
I wrote a utility that monitors the frame rate in an effort to tailor my code's impact to the player's setup.
So on a slower machine, I can do less or do it less often or spread the work out over several frames.
It's called fps_monitor and since I had to learn how, it's an oxz:

https://www.dropbox.com/s/0maqstcxuq0el ... r.oxz?dl=0

With it, you can track the fps rate, as well as the median, mode, mean, high & low values.
You can set up to 3 time frames and report to the log file and/or the in-game console.
You can access the data from an oxp so as to adjust to current conditions.

The readme has several examples of how I'm using it. I'm hoping for some feedback and discussion on using this (or something else) to reclaim our frame rate.

Edit: updated link for ver. 1.1
- added callback functionality, stutter monitoring, fix for paused game.

_________________
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.


Last edited by cag on Wed Sep 13, 2017 7:52 pm, edited 2 times in total.

Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Fri Sep 08, 2017 3:35 pm 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
from the wiki on Vector3D methods:

All Oolite-provided functions which take a vector as an argument may instead be passed an array of three numbers, or an Entity (in which case the entity’s position is used) [emphasis added]

So, use
Code:
ps.position.distanceTo( ent )
instead of
Code:
ps.position.distanceTo( ent.position )
it profiles as 15% faster and you don't have to type positoin as often :) Sometimes it does pay to rtfm

_________________
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Fri Sep 08, 2017 5:47 pm 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sun Jul 21, 2013 12:26 pm
Posts: 471
Quote:
I'm sure there are lots of others out there but no central repository. Sifting thru BB search results has to be one of the most inefficient methods of information gathering Man has ever devised.
Indeed, I have a few OXPs that need an optimization pass and having to peruse a 4 pages thread for this isn't a priori a pleasant perspective. A wiki page would be more practical I think. The discussions should stay here because it's more comfortable than the discussions tab on the wiki. We can just pour into it the tips that have been collected so far, and worry about a "logical enough for everyone" organization for this page later.

I can do the work once I've gathered enough antilazynium to do my optimizations, if everyone is ok with it.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Fri Sep 08, 2017 6:26 pm 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
Quote:
I can do the work once I've gathered enough antilazynium to do my optimizations, if everyone is ok with it.
Ok with it? Your kidding, right? Didn't anyone ever teach you to never volunteer? Maybe you can trade some of your excess masochismium for antilazynium :lol:

That said, if you want anything profiled, let me know. I've been doing an awful lot of that, lately.

_________________
"Better to be thought a fool, boy, than to open your trap and remove all doubt." - Grandma [over time, just "Shut your trap... fool"]
"The only stupid questions are the ones you fail to ask." - Dad
How do I...? Nevermind.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 51 posts ]  Go to page Previous 1 2 3 4

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Limited