Board index FlightGear Development Nasal

New in FG 2020 - type check helpers etc

Nasal is the scripting language of FlightGear.

New in FG 2020 - type check helpers etc

Postby jsb » Tue May 05, 2020 5:41 am

Just to notify the community:
There are new helper functions in Nasal starting with FG 2020.1 which is about to be released these days.
Many of them are type checks that up to now had to be coded using typeof() like this
Code: Select all
if (typeof(myVariable) == "vector") {
  print("vector");
}

Now you can use e.g.
Code: Select all
if (isvec(myVariable)) {
  print("vector");
}

which is a bit more efficient as there is no internal type to string conversion and not string comparison involved.

Another new language keyword is
Code: Select all
vecindex()

For details see http://wiki.flightgear.org/Nasal_library#Extension_functions_new_in_FG_2020.1
jsb
 
Posts: 258
Joined: Sat Oct 25, 2014 8:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: New in FG 2020 - type check helpers etc

Postby Hooray » Wed May 06, 2020 3:31 pm

since you mention efficiency, it would be pretty interesting to check whether this is an API that is actually called that frequently, e.g. by overloading typeof() and counting invocations per frame/second to see if there are any obvious hot spots that might benefit from being reviewed for a possible update/migration to use the new nativer helpers instead.

You should be able to obtain a context handle by using caller()/closure() respectively, and get a file name / line number for call sites.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11771
Joined: Tue Mar 25, 2008 8:40 am

Re: New in FG 2020 - type check helpers etc

Postby jsb » Wed May 06, 2020 8:10 pm

Actually, I wrote some debug helpers lately (that are avail on next) to monitor a complete namespace with only a single line of code. This is so new, i did not had the time to document it on the wiki yet, sorry.
Add a file /Nasal/modules/debug.nas and put this into it:
Code: Select all
debug.addProbesToNamespace(canvas.Element, "Element");
debug.addProbesToNamespace(canvas.Group, "Group");

Start FG and open the property browser, navigate to /_debug/nas/_stats to watch the hit counters live. After a while you may want to ctrl-click onto /_debug/nas/_dumpstats in the property browser and have a look to your console window (or logfile) ;)
You may find something like this
Code: Select all
probe-Element-setVisible    535732
probe-Element-_setupCenterNodes    505877
probe-Element-getCenter    503985
probe-Element-setRotation    465980
probe-Element-_getTf    423210
probe-Element-setTranslation    423140
probe-Element-hide    298014
probe-Group-_isElementNode    260777
probe-Element-show    234435
probe-Element-new    78252
probe-Group-_getFactory    78120
probe-Group-_wrapElement    72144
probe-Group-getChildren    24479
probe-Group-getChildrenOfType    24479
probe-Group-setColor    24457
probe-Element-getVisible    4852
probe-Group-getElementById    4443
probe-Element-boundingbox2clip    3242
probe-Element-setClipByBoundingBox    3242
probe-Element-setClipByElement    3238
probe-Element-createTransform    2881
probe-Group-createChild    2129
probe-Element-updateCenter    1825
jsb
 
Posts: 258
Joined: Sat Oct 25, 2014 8:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: New in FG 2020 - type check helpers etc

Postby jsb » Wed May 06, 2020 8:40 pm

Further more, if you are nosy, you can inspect any code by adding a debug.breakpoint (which I revamped also lately) and get a trace tree
Code: Select all
# add a new bp object by using only one of the following lines
var myBP = debug.Breakpoint.new("description", 0);                                       # no inital tokens, activate BP later via the property browser; won't monitor startup of FG but at runtime whenever you like
var myBP = debug.Breakpoint.new("description", 0).enable(10);                   # this will add 10 tokens for backtraces to the console; see hit() below
var myBP = debug.Breakpoint.new("description", 0).enableTracing(1000); # this will add 1000 (other separate) tokens for tracing statistics in the prop tree; see trace() below
# see property browser  at /_debug/nas/bp-description/

#... at the line of interest (e.g. in an API or core lib  function) add either
myBP.hit(); #print bt to console and add trace to prop tree
# or
myBP.trace(); #only  add bt stats to prop tree

As long as there are tokens, the hit() will print backtraces to console and a tracer() will put a backtrace into the prop tree.
If you did trace() and provided tokens, goto /_debug/nas/bp-description/
Again you can set the dump-trace property to true and the listener will dump it to console and reset the dump-trace property.
The result is a backtrace statistic givinge filename[line] in a tree-ish view and how many hits where counted on that trace path.
Code: Select all
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/hits {INT} = 1110
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/hits {INT} = 477
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[154]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[154]/eicas-pri.nas[175]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[155]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[155]/eicas-pri.nas[175]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[156]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[137]/eicas-pri.nas[156]/eicas-pri.nas[175]/hits {INT} = 159
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[171]/hits {INT} = 631
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-pri.nas[171]/eicas-pri.nas[176]/hits {INT} = 631
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-fuel.nas[140]/hits {INT} = 1
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-fuel.nas[140]/globals.nas[119]/hits {INT} = 1
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-fuel.nas[72]/hits {INT} = 1
text.nas[74]/group.nas[138]/group.nas[54]/group.nas[66]/group.nas[94]/eicas-fuel.nas[72]/globals.nas[119]/hits {INT} = 1
jsb
 
Posts: 258
Joined: Sat Oct 25, 2014 8:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest