hashlink icon indicating copy to clipboard operation
hashlink copied to clipboard

Maps are slow in hashlink

Open apxapob opened this issue 2 years ago • 1 comments

Here is my test code:

function mapTest() {
    var start = Timer.stamp();
    var N = 1000000;

    var map:Map<String, Int> = [];
    for (i in 0...N){//set
      map.set(i+'', i);
    }
    trace("1 step time:", Timer.stamp() - start);
    for (key in map.keys()){//get
      var a = map.get(key);
      a++;
    }
    trace("2 step time:", Timer.stamp() - start);
    for (i in 0...N){//remove
      map.remove(i+'');
    }
    trace("total time:", Timer.stamp() - start);
  }

And here are my results for N = 1 000 000: 1 step time: 0.400216579437256 2 step time: 0.55933403968811 total time: 20.15642786026

Hashlink: 1000 => total time: 0.000833272933959961 10000 => total time: 0.0115585327148438 100000 => total time: 0.325639247894287 1000000 => total time: 20.6692686080933 10000000 => total time: ???

the same code compiled for Javascript: 1000 => total time: 0.0005000000000000004 10000 => total time: 0.001600000001490104 100000 => total time: 0.015400000002235181 1000000 => total time: 0.16799999999999998 10000000 => total time: 2.606199999999255

Also I tried IntMaps they're faster at setting and getting but slower at removing.

Anyway set and get are also slow. Javascript completes the whole test 2 times faster than Hashlink only step 1.

apxapob avatar Jul 14 '23 19:07 apxapob

Thanks for the benchmark results, that's interesting indeed to see how both implementations are scaling wrt large number of elements. I think it would be better first to benchmark IntMaps to factor our String allocation and hashing, but I agree that the benchmark result might be related to the current implementation of maps, here: https://github.com/HaxeFoundation/hashlink/blob/master/src/std/maps.h

I wonder what kind of structure is used on JS VM and how it compares also in terms of memory usage.

ncannasse avatar Jul 17 '23 13:07 ncannasse