Ticket #2475: timer.diff

File timer.diff, 2.6 KB (added by sanderd17, 10 years ago)

Test for the timer component. Results in a ~20% speed improvement in combat demo (huge) when no actions are executed.

  • binaries/data/mods/public/simulation/components/Timer.js

     
    77{
    88    this.id = 0;
    99    this.time = 0;
    10     this.timers = {};
     10    this.timers = new Map();
    1111};
    1212
     13Timer.prototype.Serialize = function()
     14{
     15    var r = {};
     16    r.id = this.id;
     17    r.time = this.time;
     18    r.timers = [];
     19    for (var t of this.timers.entries())
     20        r.timers.push(t);
     21    print(uneval(r));
     22    return r;
     23};
     24
     25Timer.prototype.Deserialize = function(data)
     26{
     27    this.id = data.id
     28    this.time = data.time;
     29    this.timers = new Map(data.timers);
     30};
     31
    1332/**
    1433 * Returns time since the start of the game, in integer milliseconds.
    1534 */
     
    2746Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data)
    2847{
    2948    var id = ++this.id;
    30     this.timers[id] = [ent, iid, funcname, this.time + time, 0, data];
     49    this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]);
    3150    return id;
    3251};
    3352
     
    4564    if (typeof repeattime != "number" || !(repeattime > 0))
    4665        error("Invalid repeattime to SetInterval of "+funcname);
    4766    var id = ++this.id;
    48     this.timers[id] = [ent, iid, funcname, this.time + time, repeattime, data];
     67    this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]);
    4968    return id;
    5069};
    5170
     
    5473 */
    5574Timer.prototype.CancelTimer = function(id)
    5675{
    57     delete this.timers[id];
     76    this.timers.delete(id);
    5877};
    5978
    6079
     
    6685    // Collect the timers that need to run
    6786    // (We do this in two stages to avoid deleting from the timer list while
    6887    // we're in the middle of iterating through it)
    69     var run = [];
    70     for (var id in this.timers)
     88    var run = new Set();
     89    for (let id of this.timers.keys())
    7190    {
    72         if (this.timers[id][3] <= this.time)
    73             run.push(id);
     91        if (this.timers.get(id)[3] <= this.time)
     92            run.add(id);
    7493    }
    75     for (var i = 0; i < run.length; ++i)
     94    for (let id of run)
    7695    {
    77         var id = run[i];
    78 
    79         var t = this.timers[id];
     96        var t = this.timers.get(id);
    8097        if (!t)
    8198            continue; // an earlier timer might have cancelled this one, so skip it
    8299
     
    84101        if (!cmp)
    85102        {
    86103            // The entity was probably destroyed; clean up the timer
    87             delete this.timers[id];
     104            this.timers.delete(id);
    88105            continue;
    89106        }
    90107
     
    103120            t[3] += t[4];
    104121            // Add it to the list to get re-executed if it's soon enough
    105122            if (t[3] <= this.time)
    106                 run.push(id);
     123                run.add(id);
    107124        }
    108125        else
    109126        {
    110127            // Non-repeating time - delete it
    111             delete this.timers[id];
     128            this.timers.delete(id);
    112129        }
    113130    }
    114131}