NetLogo / Tortoise

Compiler and runtime engine for NetLogo models that runs in JavaScript 🐢
https://netlogoweb.org
Other
56 stars 27 forks source link

Refactor: The Variable Manager #248

Open CIVITAS-John opened 1 year ago

CIVITAS-John commented 1 year ago

Related emails:

I was thinking about how to allow a "temporary model" that goes above the current world - e.g. thinking about AI generating a snippet with breed definitions and procedures, yet we don't want that to override the model unless the user wants it. Then I realized that there are some checks there that prevent it - because although I can compile things like

globals [ a-temporary-variable ]

There is no way to initialize it in advance. So I started to read the relevant code again, and find a real issue: user-defined variables can override the built-in methods of VarManager (!) since we are defining properties during initialization!

So, try adding has to any NLW model as a turtle variable, and you immediately break it. That's not good. So right now I am working on a major refactor to clear all that mess, switch to Map-based implementation, and reduce memory consumption. I am not sure if this is going to increase performance - I mean it is going to benefit agent initialization as I plan to do it lazily, but the access could be slightly more complicated.

===

The performance should be on par with before the refactoring, or slightly improve, depending on the nature of the model.

The good thing is that we eliminate a ton of unnecessary memory allocation and ease the burden of GC. We also eliminate the has bug. It is now easier to dynamically add turtle/breed variables - the very reason I am doing this. So, if there is a model that involves a large amount of variable reading, it is going to benefit. Otherwise, the speed should stay similar, with a lowered memory footprint for each agent.

===

I just realized that Map is not always better than Object in terms of serving as a HashTable; especially, if the table is going to have no more than a few keys. however, if we ever want to use an Object for that purpose, we need to start from empty new Object(null) and use a separate object for the purpose.

In some models I made, I saw around 10% performance gain; they both heavily use turtle, patch, or link variables, which is the main reason.

TheBizzle commented 2 months ago

@CIVITAS-John, is this still being worked on?

CIVITAS-John commented 2 months ago

It has been in TU for a while. Last time we talked about this, there was a need to systematically evaluate the performance difference before we can incorporate it into the main branch.

I wonder if we want to make it simpler to do that - I just have too many things on my plate right now.

On Fri, Jun 28, 2024 at 10:47 AM Jason Bertsche @.***> wrote:

@CIVITAS-John https://github.com/CIVITAS-John, is this still being worked on?

— Reply to this email directly, view it on GitHub https://github.com/NetLogo/Tortoise/pull/248#issuecomment-2197212185, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC523NYNKM5OAG7MF24MA43ZJWARZAVCNFSM6AAAAABKCEMHSSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJXGIYTEMJYGU . You are receiving this because you were mentioned.Message ID: @.***>