-
Notifications
You must be signed in to change notification settings - Fork 248
Conversation
@mhevery - for review. Still need to add docs though - at least class description and why this map exists in the first place... |
Great! Do you have benchmarks? |
|
||
void _copyAllElementsToMap() { | ||
var i = 0; | ||
if (delegate==null) delegate = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{} are known to be slow, the type of the delegate should be configurable.
|
||
bool containsKey(Object key) { | ||
if (elements != null) { | ||
for(var i=0; i<elements.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should read i<count
, otherwise you are iterating unnecessarily over null
s.
@jbdeboer Added a benchmark.. but the results are not promissing. HashMap still beat all other types of maps...
|
I'm also thinking of chaning the strategy with this arrays. I can try keeping them sorted so lookups could be a lot faster.... |
@markovuksanovic I am sorry for not making the request clearer. A list will never perform as fast as a field. What I am looking for is 20 field keys and 20 field values, just as they were in my original benchmarks. (It needs to implement Map) This is fast for several reasons. 1) field access is relatively fast, 2) fields are located next to each other in memory layout therefore they will have high cache hit rate. Yes, it feels a bit strange to have 40 fields, but compared to the cost of map with 1 key it is still better since Map has to have several objects List and Buckets to scale, which add up to way than 40 fields. |
@mhevery I've refactored this to use fields and the results are noticeable now. |
} | ||
} | ||
|
||
class FastMap<K,V> implements Map { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete the FastMap from the benchmark.
Nice work. Please take care of the comments. What are your benchmark numbers. How much faster are we? |
…ay and after a number of elements is inserted switches to a HashMap implementation. MicroMap is intended to be used when one expects that the number of elements will be quite low. In this setting it's better to use an array and use linear search to locate elements rather then using the actual HashMap. After a certain number of elements have been inserted HashMap will perform better then array and linear search. Worst case scenario will be when the number of elements keeps oscillating around threshold value which will cause the map to keep switching from array to HashMap (and vice versa). Switching form array to hashMap and the other way around is an expensive operation. Closes dart-archive#1201
@rkirov I'm considering that option too... I worked on this today and I think I'll need a couple of more iterables (e.g. ListIterable, SubList iterable, whileTakeItrable, MappedIterable,...) I used internal implemnations in dart as a reference for MicroIterable. Turns out implementing iterable is a bit more challenging then I initially thought :) |
Actually, I got it working... The code should be fine now, although I need to write some more tests.... |
|
||
import 'dart:collection'; | ||
|
||
const int _LIST_ELEMENTS = 20; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused ?
@markovuksanovic @mhevery I'm a bit concerned that the lack We should investigate the perf impact for supporting |
We can continue the discussion on #1230. It is based on this PR and has a few extra features - supporting null keys, doesn't go back to fields mode once map mode is triggered. Also it replaces HashMap at a few key places in the framework. I think iterators need more work (they should be views into underlying micromap, instead of holding their own copies of the data). |
An implementation of the Map which initially uses array and after a number of elements is inserted switches to a HashMap implementation.
MicroMap is intended to be used when one expects that the number of elements will be quite low. In this setting it's better to use an array and use linear search to locate elements rather then using the actual HashMap. After a certain number of elements have been inserted HashMap will perform better then array and linear search.
Worst case scenario will be when the number of elements keeps oscillating around threshold value which will cause the map to keep switching from array to HashMap (and vice versa). Switching form array to hashMap and the other way around is an expensive operation.