"caching" of values

Here you can post threads specific to the current release of the core (099)
Post Reply
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

"caching" of values

Post by andenixa »

Hello everyone,

As the majority of the shard scripters I store frequently accessed values which would otherwise require a massive computation or many iterations to lookup.
But I noticed that many data structures degrade their performance over the number of entries.

I wonder if a POL Developer or someone who had a chance to look at the sources could answer that. Are there any key->value data structures in pol which have a constant access time?

Thank you very much for your time.
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: "caching" of values

Post by Turley »

Escript Array is an c++ vector
Escript Dictionary is an c++ map
an vector is faster then a map especially since you need strings for map. But this is only a general statement, if you want more i need too know a escript snipet.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: "caching" of values

Post by andenixa »

Thanks a lot for your reply.

The example snippet could be:

Code: Select all

// Get spell id by the name spell_name
function GetSpellId( spell_name )

    spell_name := Trim( lower( spell_name ) );

    var spell_id := GetGlobalProperty( "spell_id_by_name_"+spell_name );

    if( not spell_id )

        // do the extensive lookup through the configs in a loop
          ...

        if( spell_id_found )
            SetGlobalProperty( "spell_id_by_name_"+spell_name,  spell_id_found );
        else
            return error{errortext:="That spell doesn't exist."};
        endif
    endif

   return spell_id;

endfunction
I need the data to be accessible between scripts, so arrays and dicts would not apply (are cfgfiles implemented using C++ map too?). Do you think I should use datafile instead of global properties or maybe cfgfile? I am not sure how the above are implemented and if any of those have a constant time.

I thought about storing the lookup tables inside globals of some script or better a "method_script" of some in-game object, but the lookup for that object would probably make caching unfeasible.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Re: "caching" of values

Post by CWO »

a datafile is great for storing a lot of information that shouldn't be put into globals and cfgfiles.
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: "caching" of values

Post by Turley »

Globals, cfg and datafiles are internal basically the same a map. I would always suggest the use of datafiles. Configfiles aren't designed to modify the data. You can alter it but you never should. Globals are good but keep in mind that they are always stored during worldsave. Datafiles are only saved during a worldsave If they were altered.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: "caching" of values

Post by andenixa »

Oh, thank you both of you. I think datafile would be my choice. I didn't know they aren't saved. The cprops were always a black box for me, so I was very cautious to use those.
Post Reply