Oolite Bulletins

For information and discussion about Oolite.
It is currently Wed Sep 20, 2017 2:01 am

All times are UTC




Post new topic  Reply to topic  [ 51 posts ]  Go to page Previous 1 2 3 4 Next
Author Message
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 12:25 am 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
This is great stuff. I think some of it will do wonders for my code.

I had trouble getting the profiler to work in the past, but I think I can see, without it, which bits of my code are the most time critical.

Happily, I'd started rewriting - mainly just stylistically - already.

If I could get someone to give my newly rewritten Interstellar Tweaks OXP a test run, that would be great. I should be able to have it - the rewritten and ready-for-second-party test - version ready in a few days. There should not be that much to do because, though the OXP allows a great many scenarios, I haven't rewritten that much (and I'll test it myself somewhat).


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 2:43 am 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
Day, this is throwing me:
Code:
var z = myArray.length;
while (z--) {
    var myElement = myArray[z];
    // my code
}
What exactly does that do? Suppose that the length of myArray is 3, i.e. myArray has three members, the indices of which are:
Code:
myArray[0]
myArray[1]
MyArray[2]
Does your loop proceed backwards through the members of myArray, starting at myArray[2] and proceeding to myArray[0]? The loop through an error in my code, which makes me think its trying to access myArray[-1] or myArray[3], but probably there's a mistake elsewhere in my code.

EDIT: Here are the relevant bits of my code, i.e. the bits that threw the error.
Code:
this.maxBattleships = 2;

this.$isBattleship = function(e) {
	return e.isShip && e.isThargoid && e.name === "Thargoid Thargorn Battleship"
}
// Note: the battleship lacks a distinct role, so we have to use its name.

if ( this.bugThreat_Installed && Math.random() <= this.chanceLimitBattleshipsInOtherwiseUnmodified ) {
	s = system.filteredEntities(this, this.$isBattleship);
	i = s.length - this.maxBattleships;
	while ( i-- ) {
		s[i].remove(true);
	}
}
The error is:
Code:
JavaScript exception (IST_masterScript 5.38): TypeError: s[i] is undefined


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 6:17 am 
Offline
Commodore
Commodore
User avatar

Joined: Tue Jan 21, 2014 10:37 pm
Posts: 1701
Location: [p]laying [h]ard and [k]icking [b]utt somewhere in G7...
I think the problem is with this line:
Code:
i = s.length - this.maxBattleships;
You're setting "i" up to be an index of the "s" array, but then you're adjusting the starting point. If "s" ends up having 0 elements (ie because there are no battleships returned through the filteredEntities call), then i is going to start with at index -2. And, therefore, "undefined".

_________________
My OXP's


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 8:31 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:
Day, this is throwing me:
Code:
var z = myArray.length;
while (z--) {
    var myElement = myArray[z];
    // my code
}
What exactly does that do? Suppose that the length of myArray is 3, i.e. myArray has three members, the indices of which are:
Code:
myArray[0]
myArray[1]
MyArray[2]
Does your loop proceed backwards through the members of myArray, starting at myArray[2] and proceeding to myArray[0]?
Exactly.
Quote:
I think the problem is with this line:
Code:
i = s.length - this.maxBattleships;
You're setting "i" up to be an index of the "s" array, but then you're adjusting the starting point. If "s" ends up having 0 elements (ie because there are no battleships returned through the filteredEntities call), then i is going to start with at index -2. And, therefore, "undefined".
Exactly. The starting point should be the length of the list you want to iterate through.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 8:35 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:
This is great stuff. I think some of it will do wonders for my code.
Thank you :P


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 12:32 pm 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
phkb, Day

Thank you for your explanations.

About the 'undefined' error: I had been under the impression that if i <= 0, any text within
Code:
while ( i-- ) {
}
would not run at all.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 4:04 pm 
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:
phkb, Day

Thank you for your explanations.

About the 'undefined' error: I had been under the impression that if i <= 0, any text within
Code:
while ( i-- ) {
}
would not run at all.
Well, 0 is considered false, and other numbers, including negative ones, are true.
So for i = 0, the code within won't run (the -- is operated after the while comparison).
For i < 0, the code within will an inifinite amount of times.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Wed Jun 21, 2017 7:21 pm 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
Ah, right. Sometimes I will have to add some checks for that, resulting in slightly less optimisation and - where I do add a check - a slightly more fragile script. Oh well.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Thu Jun 22, 2017 1:16 am 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
I've a question about functions.

Previously, the following was typical in my code.
Code:
this.$timedDamageNonPlayerViaRadiation = function $timedDamageNonPlayerViaRadiation() {
	var i;
	var s;
	var damage;
	var shipsToDamage;

	if ( !system.isInterstellarSpace ) { this.$removeRadiationTimers(); return; }
	if ( !player.ship ) { this.$removeRadiationTimers(); return; }
	s = system.allShips;

	function $toDamage(e) {
		return e.isShip && !e.isPlayer
	}

	shipsToDamage = s.filteredEntities(this, $toDamage);
Now I am getting rid of functions-within-functions. But I am unsure of the syntax for entity filtering and I've been getting an error message. Is the following format right, please?
Code:
this.$toDamage = function(e) {
	return e.isShip && !e.isPlayer
}

this.$timedDamageNonPlayerViaRadiation = function $timedDamageNonPlayerViaRadiation() {
	var i;
	var s;
	var damage;
	var shipsToDamage;

	if ( !system.isInterstellarSpace ) { this.$removeRadiationTimers(); return; }
	if ( !player.ship ) { this.$removeRadiationTimers(); return; }
	s = system.allShips;
	shipsToDamage = s.filteredEntities(this, this.$toDamage); // DOES THIS WORK?
I am unsure about the 'this' in 'this.$toDamage'. On the other hand, perhaps the problem is with variable declaration . .


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Thu Jun 22, 2017 2:10 am 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
your error is coming from
Code:
s = system.allShips;
...
shipsToDamage = s.filteredEntities(this, $toDamage);
allShips is just an array i.e., a property of system, and has no methods.
filteredEntities is a method of system.
So, instead, just use
Code:
shipsToDamage = system.filteredEntities(this, $toDamage);

_________________
"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 Jun 22, 2017 2:28 am 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
the following is faster than indexOf when dealing with arrays:
Code:
this._index_in_list = function( item, list ) { // for arrays only
    var k = list.length;
    while( k-- ) {
        if( list[ k ] === item ) return k;
    }
    return -1;
}
so,
Code:
if( targets.indexOf( ship ) ...
becomes
Code:
if( ws._index_in_list( ship, targets ) ...
I replaced all (29) of the indexOf used on arrays in telescope.js and got 3 more frames/sec!

_________________
"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 Jun 22, 2017 2:55 am 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
cag: thanks a lot. About your suggestion that I use:
Code:
shipsToDamage = system.filteredEntities(this, $toDamage);
Is the '(this, $toDamage)' right? I don't need '(this, this.$toDamage)'?

(As you can see, I don't really understand this stuff, though somehow I manage to have written a 1, 000 line OXP that works! Well, it worked before I started the latest bit of fiddling with it, anyway . .)


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Thu Jun 22, 2017 3:03 am 
Offline
Competent
Competent

Joined: Fri Mar 17, 2017 1:49 am
Posts: 44
Yes, you do need that this!
Code:
shipsToDamage = system.filteredEntities(this, this.$toDamage);
The 1st (lone) this tells filteredEntities where to operate in, in what 'scope' or enviroment to use (ie. this tells it to run using your script file's variables).
You absolutely need the 2nd one on $toDamage, or you'll get a reference error (the interpreter won't be able to find the function)

_________________
"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 Jun 22, 2017 3:13 am 
Offline
---- E L I T E ----
---- E L I T E ----

Joined: Sat Sep 12, 2009 11:58 pm
Posts: 945
Location: Essex (mainly industrial and occasionally anarchic)
OK, great. Thank you. I did not need the second 'this' when the function in question was defined within the function that I was in already. (See an earlier post of mine if you need explanation.) But, as per one of the performance tips earlier in this thread. I was trying to get rid of such nested (if that's the word I want) functions.


Top
   
 Post subject: Re: OXP Performance tips
PostPosted: Thu Jun 22, 2017 9:16 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:
the following is faster than indexOf when dealing with arrays:
Code:
this._index_in_list = function( item, list ) { // for arrays only
    var k = list.length;
    while( k-- ) {
        if( list[ k ] === item ) return k;
    }
    return -1;
}
so,
Code:
if( targets.indexOf( ship ) ...
becomes
Code:
if( ws._index_in_list( ship, targets ) ...
I replaced all (29) of the indexOf used on arrays in telescope.js and got 3 more frames/sec!
Wow!
I would never have thought that the native method would be slower than the reimplementation :shock:
ToBeInserted => Done


Last edited by Day on Mon Jun 26, 2017 9:31 am, edited 1 time in total.

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

All times are UTC


Who is online

Users browsing this forum: No registered users and 15 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