Minimizing memory usage from script

Plombo

OpenBOR Developer
Staff member
Scripts in OpenBOR use a ton of memory, far more than they should. This is one of the engine's major shortcomings. But there are ways to mitigate this issue.

It's common to use the same animationscript for several models in a game. There's nothing inherently wrong with this practice; code reuse is good! But every time you load a model using that animationscript, OpenBOR recompiles it and gives it its own storage. In other words, if you have 20 characters using the same animationscript, the contents of that animationscript will be stored in memory 20 times!

There's an easy way around this, though. I'll demonstrate using magggas' fantastic, recently released Double Dragon Reloaded. 113 models in the game all use the animationscript
Code:
data/scripts/ani0020.h
. So let's rename
Code:
ani0020.h
to
Code:
ani0020_actual.h
and create a new file
Code:
ani0020.h
with this one line inside of it:
Code:
#import "data/scripts/ani0020_actual.h"

The
Code:
#import
directive ensures that the script is only compiled and stored in memory once, instead of 113 times. And that's all! No further changes are needed. Just this one change brings the memory usage of Double Dragon Reloaded down from 118.6 MB to 52.5 MB, according to the Windows system monitor.


You can also do this with other kinds of scripts used by several models, with the caveat that script types other than animationscript have a
Code:
main()
function, which the engine won't recognize if it's imported. The workaround is to rename
Code:
main()
to
Code:
actual_main()
in
Code:
scriptname_actual.c
and then have this in
Code:
scriptname.c
:
Code:
#import "data/scripts/scriptname_actual.c"

void main()
{
    actual_main();
}

Or, if your main() function is already small, you can just move all of the other functions to the new file and leave main() where it is.
 
I had changed my mods like this a while ago, it really does help.

Even without, loading hasn't bothered me since the update a while ago that dramatically decreased the loading times.
 
Thanks for the information Plombo, I'm going to replace include with import and see the changes  :D
 
Just to elaborate a bit, keep in mind as you switch to #import, that #import does NOT retain macros (constants and function macros using the #define directive). However, you can still get the best of both worlds by combining the two.

I highly recommend creating libraries of related functions, like say, grappling, and package them into a folder. In that folder create a central file (I like to call mine main) that #imports all the needed functions, and #includes the macros.

Then, in the individual scripts throughout your module, #include that main file if and when you need the functions from the package. Because the main is only a group of #imports and #defines, this gets you everything you need in one shot and is an excellent memory vs. utility compromise.

DC
 
I was thinking about this issue some time ago but I thought I was wrong because I am a newbie, but looks like I wasn't wrong :)
 
I use this function for all my scripts its really helps keep everything nice and tidy.

Any other tips would be appreciated.
 
Really nice system.
Of course if you have similar systems for your entities it can be really good to save memory!
I'll try it!

BTW, is it compatible with pre-2011 Openbor builds?
 
nedflandeurse said:
BTW, is it compatible with pre-2011 Openbor builds?

No. The #import directive was finalized in build 3779, released 2013-01-24.

DC
 
Damon Caskey said:
nedflandeurse said:
BTW, is it compatible with pre-2011 Openbor builds?

No. The #import directive was finalized in build 3779, released 2013-01-24.

DC

Thanks, I'll check builds from this version to see if my mod project is compatible or not.
Hope I can get to find the build if it's still achived somewhere.
 
O Ilusionista said:
I don't want to sound harsh or something, but shouldn't this be fixed on the engine per se?

Ideally, yes, but it's not easy to do for various compatibility reasons.
 
SimonSmith said:
..... Reloaded down from 118.6 MB to 52.5 MB, ......
That's terrible...How could scripts(just texts) take so much memory?
For one script file 500K is very large, isn't it?

The script engine is very, very bad at using memory. A compiled script takes far more memory than its source code.
 
It's worth mentioning that ChronoScript completely fixes the memory usage issues that OpenBOR's script engine has. It's tempting to try backporting it to OpenBOR. There are some challenges to doing that, but it might be worth it in the long run.
 
I gave it a try - and it reduced my memory usage from 178mb to 110mb.
I was using ondeathscript which were importing my animationscript (to reuse some codes) and if I import the grabscript.c (the shortcut) instead of grascript_actual.c (the actual code), the engine closes telling that it can't compile the scripts. So I imported the grascript_actual.c and it works.
 
Back
Top Bottom