HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.14k stars 654 forks source link

ES6 Map vs. haxe.ds.ObjectMap #7349

Closed jozefchutka closed 3 months ago

jozefchutka commented 6 years ago

Hi everyone,

what are the plans with using ES6 build in Map instead of haxe implementation of ObjectMap (and others). I tried to benchmark and compare these two, it seems the access time is similar but custom haxe implementation consumes 15%+ retained memory (in chrome).

Tested with following implementation:

@:multiType(@:followWithAbstracts K)
abstract Map2<K,V>(IMap<K,V>) {
    // ... copy of haxe.Map
    @:to static inline function toObjectMap<K:{ },V>(t:IMap<K,V>):IMap<{ },V> {
        return NativeMap.isSupported() ? new NativeMap<{ },V>() : new ObjectMap<{ }, V>();
    }
    // ... rest of haxe.Map
}

class NativeMap<K,V> implements haxe.Constraints.IMap<K,V> {
    var source:Dynamic;

    public function new(){
        source = untyped __js__('new Map()'); 
    }

    public static function isSupported():Bool{
        return Reflect.hasField(js.Browser.window, "Map");
    }

    public function get(k:K):Null<V>{
        return source.get(k);
    }

    public function set(k:K, v:V):Void{
        source.set(k, v);
    }
    // ...
}
nadako commented 6 years ago

Yeah, the plan is to implement ES6 Map-based haxe maps, hopefully I'll find time for Haxe 4.

Simn commented 4 years ago

@nadako Could you remind me (again) what the outcome was here?

nadako commented 4 years ago

Well, I have a WIP PR #9303 that I have no time to work on.