GitHub Documentation

ArraySchema is very slow.



  • I did some measurements and found that in my game it takes tens of thousands of cycles to address a single member of an array of numbers. Is this usual behavior? And is there some workaround to prevent this?



  • In fact, the larger the array, the slower is a single address. This is also true on the client side.



  • It looks like it is a known issue. What was the intended way to fix this?

    colyseus/schema repository, ArraySchema.ts, line 147

    at(index: number) {
        //
        // FIXME: this should be O(1)
        //
        const key = Array.from(this.$items.keys())[index];
        return this.$items.get(key);
    }


  • It looks like CollectionSchema has the same issue?


  • administrator

    Hi @swifton, welcome. Which type of data are you using arrays for in your game?

    Arrays/collections are not optimized for speed at the moment. Arrays are particularly complicated because there are so many different operations that could happen, and keeping the local indexes of all clients is difficult (there's also @filterChildren() where different clients may access different data with the same index)

    The @colyseus/schema repository has a PerformanceTest file containing some thresholds for speed when encoding/decoding, I'm afraid improving it would require a massive effort to change the structure we currently have during encoding/decoding: https://github.com/colyseus/schema/blob/master/test/PerformanceTest.ts



  • I was using it to store tiled maps. I ended up replacing it with a MapSchema -- it works pretty well for my needs so far.

    For the general problem, maybe you could make this easier by getting rid of some more advanced functionality? Taking linear (or even superlinear?) time to look at one element of an array is a very odd thing to see. I think it's more important to fix this basic functionality than to have all these operations. It's your decision to make though.


 

© 2021 Lucid Sight, Inc