• Ok, thanks for trying to narrow this down. I think really I'd need to see an example of this being used, but nothing looks that bad space-wise, unless what you're pushing into it ends up including a bunch of extra info (maybe via the scope it was defined in).

    Honestly though, this is not the way to write code for Espruino. It's going to be so slow! By using Queue you're basically just reimplementing what Array does but adding a whole extra layer of abstraction which is eating memory and CPU time.

    I mean, you look at isEmpty, it's calling this.any() which calls _this.length which is a getter which calls this.internalArray.length. It's just a nightmare - it's like someone wrote the code specifically to waste CPU cycles - even on Node.js it's going to suck.

    On Espruino as far as I can see you could just use Array directly, and if you really need to limit the size of the queue you could override .push to just call splice after it to limit the length.

    Just a quick example:

    var q = new Queue(20);
    t=getTime();for (var i=0;i<20;i++)q.push(i);print(getTime()-t­);
    // 0.082 seconds
    var q = [];
    t=getTime();for (var i=0;i<20;i++)q.push(i);print(getTime()-t­);
    // 0.0102 seconds
    

    So it's 8x slower than just using an Array, and honestly I'm amazed it's that good.

  • So it's 8x slower than just using an Array, and honestly I'm amazed
    it's that good.

    At first I did just use an array but I need to limit the size otherwise the mem is very quickly consumed (think wanting to sample gps data, but only recent as you want to reduce noise). You are speed testing two different things. You are saying pushing to an array is quicker than pushing to an array and then pruning it. Which is obviously true. If you have a quicker way of pushing and then resizing that would be more constructive.

    On Espruino as far as I can see you could just use Array directly, and
    if you really need to limit the size of the queue you could override
    .push to just call splice after it to limit the length.

    I am literally doing the same thing as splice after push (via shift). Originally I extended Array rather than having an internal array variable but the espruino compiler isn't releasing memory when splice or shift.

    I mean, you look at isEmpty, it's calling this.any() which calls
    _this.length which is a getter which calls this.internalArray.length. It's just a nightmare - it's like someone wrote the code specifically
    to waste CPU cycles - even on Node.js it's going to suck
    If I want to know if my array is empty I need to do a check in my code. I still need to call array.length. I could repeat this code every time I want to check, it wouldn't speed up the processing, or I can add an additional layer which can be individually unit tested and optimised further in future.

    Ultimately you might save some cpu time with large chunks of repeated code. It will limit you from creating good stable apps which are easy to maintain and extend. Most of the apps in the current app library are not extendable or maintainable. You need to be able to split the code into small segments to unit test. Otherwise you are taking a step back to pre object orientated test driven development.

  • but the espruino compiler isn't releasing memory when splice or shift.

    it is interpreter, compilers are supposed to compile, not manage runtime memory

    Just tried slice, splice, shift and also pop, at least in simple cases it works

    
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v19.182 (c) 2023 G.Williams
    
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    
    >a=[];i=0
    =0
    >process.memory()
    ={ free: 13978, usage: 22, total: 14000, history: 9,
      gc: 0, gctime: 2.469, blocksize: 18 }
    >for (var i=0;i<100;i++)a.push(i)
    =undefined
    >process.memory()
    ={ free: 13878, usage: 122, total: 14000, history: 13,
      gc: 0, gctime: 2.435, blocksize: 18 }
    >while(a.length)a.shift()
    =undefined
    >process.memory()
    ={ free: 13978, usage: 22, total: 14000, history: 16,
      gc: 0, gctime: 1.872, blocksize: 18 }
    >for (var i=0;i<100;i++)a.push(i)
    =undefined
    >process.memory()
    ={ free: 13878, usage: 122, total: 14000, history: 16,
      gc: 0, gctime: 1.664, blocksize: 18 }
    >for (i=0;i<100;i++)a.push(i)
    =undefined
    >process.memory()
    ={ free: 13778, usage: 222, total: 14000, history: 20,
      gc: 0, gctime: 0.697, blocksize: 18 }
    >while(a.length)a.shift()
    =undefined
    >process.memory()
    ={ free: 13978, usage: 22, total: 14000, history: 20,
      gc: 0, gctime: 2.424, blocksize: 18 }
    >for (i=0;i<100;i++)a.push(i)
    =undefined
    >process.memory()
    ={ free: 13878, usage: 122, total: 14000, history: 20,
      gc: 0, gctime: 2.41, blocksize: 18 }
    >while(a.length)a.pop()
    =undefined
    >process.memory()
    ={ free: 13978, usage: 22, total: 14000, history: 23,
      gc: 0, gctime: 0.525, blocksize: 18 }
    >for (i=0;i<100;i++)a.push(i)
    =undefined
    >a=a.slice(50)
    =[ 50, 51, 52, 53, 54,  ... 95, 96, 97, 98, 99 ]
    >process.memory()
    ={ free: 13928, usage: 72, total: 14000, history: 25,
      gc: 0, gctime: 0.528, blocksize: 18 }
    >while(a.length)a.pop()
    =undefined
    >process.memory()
    ={ free: 13978, usage: 22, total: 14000, history: 25,
      gc: 0, gctime: 0.687, blocksize: 18 }
    >
    >a=[1,2,3,4,5]
    =[ 1, 2, 3, 4, 5 ]
    >process.memory()
    ={ free: 13973, usage: 27, total: 14000, history: 30,
      gc: 0, gctime: 0.812, blocksize: 18 }
    >a.splice(3,2,10,11)
    =[ 4, 5 ]
    >a
    =[ 1, 2, 3, 10, 11 ]
    >process.memory()
    ={ free: 13973, usage: 27, total: 14000, history: 30,
      gc: 0, gctime: 2.526, blocksize: 18 }
    >a.splice(3,2)
    =[ 10, 11 ]
    >process.memory()
    ={ free: 13975, usage: 25, total: 14000, history: 34,
      gc: 0, gctime: 2.294, blocksize: 18 }
    >a
    =[ 1, 2, 3 ]
    >
    

    check usage, goes up and down as expected

    It will limit you from creating good stable apps

    For me good and stable app is not bloated app that runs slow (=drains more battery) and needs lot of memory.

    which are easy to maintain and extend. Most of the apps in the current app library are not extendable or maintainable.

    This is debatable. Smaller code without unneeded abstractions can be easy to maintain and extend too. Someone else may see your perfectly testable and extensible code hard too. Also the size of typical watch app is not that big and people write it for fun and for free so extendable and maintainable may not be even the goal here.

About

Avatar for charlie @charlie started