Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Data Read Performance: Objects, State Memory and Local Memory?

I'm looking at optimizing some of my code, especially where I read from Roll20s Objects.  Does anyone know if it is more expensive (read time) to pull from State memory, Roll20 Objects or Local memory?  
1681777111
The Aaron
Roll20 Production Team
API Scripter
Probably all of those are equal on the read side (state might be slightly faster than an object just because it doesn't go through a function).  Where the expense for objects will come is in the getting of them (findObjs() or getObj()), or in the setting (.set() has to do a sync to Firebase, not sure if there's direct cost there). As far as optimization goes, I'd try to do instrumentation and figure out where you are spending the most time, then address those places.  If you want to post the script, we can perhaps give you some suggestions.
1681797134

Edited 1681798891
That's kind of what I figured as the findObjs() and getObj() have to perform a search function.  I tried posting the code, but it was too long (1600 lines), and wouldn't post.
1681808871
GiGs
Pro
Sheet Author
API Scripter
If your code is long, its best to post it in a gist or pastebin, and link to it here. It's much easier to read!
Here's a link to my Handout Reporter.  I've cleaned it up a lot, and it runs well.  I've removed most references to my custom APIs and Macros.  It only references Tokenmod and Group-Init at this point.   Handout Reporter v0.4
1681914843

Edited 1681915045
The Aaron
Roll20 Production Team
API Scripter
With the API_Meta stuff, you'll want to adjust that 4 to be whatever line number that line is on (provided the try/throw/catch all stays on the same line).&nbsp; In this case, it looks like it should be 16.&nbsp; That's how it calculates the actual offset within your script so that you can look up what line is causing an error. (Also, you're missing the Footer portion of the API_Meta setup.)&nbsp; See:&nbsp; <a href="https://app.roll20.net/forum/post/9771314/script-apilogic-gives-if-slash-elseif-slash-else-processing-to-other-scripts/?pageforid=9772430#post-9772430" rel="nofollow">https://app.roll20.net/forum/post/9771314/script-apilogic-gives-if-slash-elseif-slash-else-processing-to-other-scripts/?pageforid=9772430#post-9772430</a> I'd suggest moving everything within the closure of your on('ready',...) event handler.&nbsp; If you don't, you're polluting the global namespace with your own symbols.&nbsp; Things like handleMsg might conflict with other scripts.&nbsp; Roll20 Mod Scripts are all concatenated when the sandbox starts up (hence the need for the API_Meta stuff to figure out where things actually are.&nbsp; If you need to expose functionality so that other scripts can make use of it, switch to the Revealing Module Pattern and return an object with the interface.&nbsp; See:&nbsp; <a href="https://app.roll20.net/forum/post/9787374/token-origin-slash-start-of-turn-location-token/?pageforid=9790892#post-9790892" rel="nofollow">https://app.roll20.net/forum/post/9787374/token-origin-slash-start-of-turn-location-token/?pageforid=9790892#post-9790892</a> Just based on this script, I'm going to guess the reason you're looking to optimize is because you're getting Possible Infinite Loop Detected errors crashing your sandbox?&nbsp; Or possibly just that other scripts are taking a long time to be responsive when your script is active.&nbsp; Your refreshReports() function is called on every change to the turn order and is doing a great deal of work synchronously.&nbsp; Javascript is a single threaded programming language, which means that only one script can be doing something at any given point.&nbsp; If one script is monopolizing the sandbox, it will starve out all the other scripts, making them appear sluggish.&nbsp; If it starves the sandbox too much, it will determine that the script might be in an infinite loop, and will kill the sandbox.&nbsp; The way you avoid both those problems is by building your code in an asynchronous manner.&nbsp; At the fundamental level, that means doing things in smaller chunks and yielding control back to the sandbox periodically so it can reset it's timer and let other scripts do work.&nbsp; The easiest way to do that is to find the places where you iterate on queues of work, and break them up into what I call burndown queues.&nbsp; See:&nbsp; <a href="https://app.roll20.net/forum/permalink/10744870/" rel="nofollow">https://app.roll20.net/forum/permalink/10744870/</a> That's probably enough to get started on further conversation. =D
I really appreciate the guidance.&nbsp; I'm definitely taking your advice on these suggestions.&nbsp;&nbsp; 1) I moved the catch back up to the top, so future comments don't modify the line number 2) I'm still learning how memory scope works in JavaScript, and was wondering if that might be the case. 3) I'm not getting infinite loops, but the RefreshReports function has a lot of looping through tokens, characters, and attributes.&nbsp; It is usually very fast, less than a second to run.&nbsp; But occassionally, it takes a couple seconds to execute, just depends on the state of the API.&nbsp; Last night I added logic to exit out of the RefreshReports if it was called too quickly in succession, which can happen when everyone is adding characters and npcs to the turnorder, or I'm quickly stepping through it.&nbsp; I've been thinking about ways to track in global memory the number of instances running concurrently and have it exit quickly if it detects another reference.&nbsp; That way I don't throttle the API when I get a bunch of turnorder change events in quick succession.&nbsp;&nbsp; I also think there may be a few ways to speed up performance by loading objects that tend to be static into memory via the findObjs function.&nbsp; Things like campaign statusmarkers.&nbsp;&nbsp;
1681920999
The Aaron
Roll20 Production Team
API Scripter
For hysteresis on your update calls, take a look at _.debounce():&nbsp;<a href="https://underscorejs.org/#debounce" rel="nofollow">https://underscorejs.org/#debounce</a>
1681921210
The Aaron
Roll20 Production Team
API Scripter
There are definitely things you can do to speed things up around caching and preloading.&nbsp; Often in computer science you can trade storage for speed.&nbsp; =D
1681931041

Edited 1681931094
The Aaron said: For hysteresis on your update calls, take a look at _.debounce():&nbsp; <a href="https://underscorejs.org/#debounce" rel="nofollow">https://underscorejs.org/#debounce</a> OMG - the _.debounce function is the BOMB.&nbsp; It's great at handling a queue of stacked event requests, and really made my script more responsive.&nbsp;&nbsp;
1681931446
The Aaron
Roll20 Production Team
API Scripter
Excellent!&nbsp; Most of underscore.js's functionality has been obviated by ES6 and above, but it's still got some great gems. =D
1681935990

Edited 1681945415
I've not seen anything published that does what this API module does.&nbsp; I may hit you up on the best way to release it for testing and finally into publication.&nbsp; I recall seeing a WIKI entry for publishing to the API, which I need to read up on.&nbsp;&nbsp; This script basically refreshes a couple handouts as the turnorder changes to give the DM/GM a Combat Dashboard and a Character/NPC Report that always has the Character or NPC at the top of the turnorder.&nbsp; These two sheets get updated in realtime as the turnorder is advanced, or even changed.&nbsp; No more scouring your desktop looking for the Character or NPC character sheet.&nbsp; I pop these handouts onto a second screen (or even another logged in computer), and refer to them in place of the built-in Turnorder dialog.&nbsp; I no longer have to manage a ton of character sheets for all my npcs and all the key character and npc information is presented and refreshed as combat proceeds.&nbsp;&nbsp; From just these handouts I can: Move the turnorder forward or backward. Sort and clear the turnorder&nbsp; Add a round counter, generic counter or static custom entry to the turnorder Roll or whisper-roll various dice, including d20s&nbsp; w/ adv/dis. Move tokens between gm/objects layers Adjust HP Perform saving throws Make attacks / Cast spells Jump to a spell description Hover over a trait and the verbose description appears in a popout.&nbsp;&nbsp; Popup the roll20 character sheet Add an entry to GMNotes on PC sheets and NPC Tokens. Add/Edit/Show/Hide tooltips Have NPCs and Characters roll initiative and add them to the Turnorder (via group-init). Finally - it tracks the time each player (and GM) spends on their turn and tracks it between sessions.&nbsp; On a player's turn, they are informed via a chat dialog how long they are taking on average (over the previous 50 turns) and how long their last turn was.&nbsp;&nbsp; Turn Order Handout Character/NPC Handout (Always shows the character or NPC at the top of the turnorder)
1681947191
The Aaron
Roll20 Production Team
API Scripter
That's extremely badass. =D&nbsp; I would use that.
1681947860
David M.
Pro
API Scripter
Very cool idea, Will!
That's pretty cool!&nbsp; I would give this a spin.
1682000944
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
What they all said. I can do some of that with the Reporter script, but this looks extremely powerful. I'd be more than willing to give it a go.
I'll work to have a good first beta ready this weekend.&nbsp; The link above has my last "stable" developed version as I've incorporated Aaron's recommendations and have removed links to many of my personal scripts and replaced them with Javascript code.&nbsp; It references TokenMod and Group-Init, which I hope to remove at some point as I develop specific functions to replace some of those Mod's functionality.&nbsp;&nbsp;
1682014536
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Bump (One Click) might be another scrip to consider emulating. It's much simpler than switching layers to move GM layer items. If you decide to incorporate condition reporting, you can raid the Condefinitions script for a JS object containing those.
1682054172

Edited 1682112960
DM Dashboard - Beta I think my DM Dashboard is ready for beta testing if anyone is interested.&nbsp; &nbsp;I've removed all dependencies to other mods/APIs and personal macros.&nbsp; Here is a link to the latest code Link: <a href="https://github.com/MadCar-Dev/Default/blob/facf89017a4e9e873e5121d8092353d27192c20f/Javascript/HandoutReporter%20v0.6.js" rel="nofollow">https://github.com/MadCar-Dev/Default/blob/facf89017a4e9e873e5121d8092353d27192c20f/Javascript/HandoutReporter%20v0.6.js</a> Documentation Link: <a href="https://github.com/MadCar-Dev/Default/blob/03fdd8e76c84aab71953d2c218722be735eac782/Javascript/DM%20Dashboard%20User%20Manual.pdf" rel="nofollow">https://github.com/MadCar-Dev/Default/blob/03fdd8e76c84aab71953d2c218722be735eac782/Javascript/DM%20Dashboard%20User%20Manual.pdf</a> To get started type the following into the chat window once its been installed. !tor This will create 3 handouts. DM Turnorder List - Primary dashboard form, keyed off of the Roll20 Turnorder and used to track combat. DM Character Sheet - A summary of the current Player Character or NPC at the top of the Turnorder.&nbsp; DM Turnorder Log - A comma-delimited data file with a record for each time the turnorder is advanced.&nbsp; Includes start and end times so you can compile stats on each of your players and over time gain some insight on how to speed up combat.&nbsp;&nbsp; Add a few players/npcs to the turn order and then pop these handouts out of the Roll20 browser onto a second computer screen, as I they need to be stretched a little wide to get the full benefit.&nbsp;&nbsp; If it looks like it has promise, I'll create a dedicated forum thread that'll include documentation on its use and all the detailed features. A couple of caveats: It assumes that HP is tracked in Bar 1.&nbsp; (I plan to add logic later allowing the player to change it to one of the other bars like I've seen some mods do.) It's based on the D&amp;D 5e by Roll20 sheet.&nbsp; I would like to add other systems, but I have no familiarity with their data fields and structures.&nbsp;&nbsp;
keithcurtis said: Bump (One Click) might be another scrip to consider emulating. It's much simpler than switching layers to move GM layer items. If you decide to incorporate condition reporting, you can raid the Condefinitions script for a JS object containing those. I'll take a look at Bump, I've seen it mentioned in a number of places, but have never researched it.&nbsp;&nbsp;
1682094797
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Getting an error on startup: TypeError: Cannot read properties of undefined (reading 'PrevTO') TypeError: Cannot read properties of undefined (reading 'PrevTO') &nbsp; &nbsp; at apiscript.js:4901:69 &nbsp; &nbsp; at eval (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:168:1), &lt;anonymous&gt;:65:16) &nbsp; &nbsp; at Object.publish (eval at &lt;anonymous&gt; (/home/node/d20-api-server/api.js:168:1), &lt;anonymous&gt;:70:8) &nbsp; &nbsp; at /home/node/d20-api-server/api.js:1563:14 &nbsp; &nbsp; at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Thanks - I’ll take a look. &nbsp;Probably has to do with a non-initialized state variable.&nbsp; &nbsp;
I was referencing a state variable that hadn't been created.&nbsp; I've corrected and published the new version to github.&nbsp;&nbsp;
1682103153
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Thanks! I'll look at it later on today.
FYI - I've added user documentation to for DM Dashboard to my github. <a href="https://github.com/MadCar-Dev/Default/blob/03fdd8e76c84aab71953d2c218722be735eac782/Javascript/DM%20Dashboard%20User%20Manual.pdf" rel="nofollow">https://github.com/MadCar-Dev/Default/blob/03fdd8e76c84aab71953d2c218722be735eac782/Javascript/DM%20Dashboard%20User%20Manual.pdf</a>