You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-12Lines changed: 15 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ The project is forked from
9
9
*Persistent* data structure is an immutable structure; the main difference with standard data structures is how you 'write' to them: instead of mutating
10
10
the old structure, you create the new, independent, (slightly) modified copy of it. Typical examples of commonly used Persistent structures are String (in Java, Javascript, Python, Ruby) or Python's Tuple or Java's BigDecimal. [(Not only)](http://www.infoq.com/presentations/Value-Identity-State-Rich-Hickey) we believe such concept could be beneficial also for other data structures such as Maps, Lists/Vectors, Sets.
11
11
12
-
var couple = new PersistentMap.fromMap({'father': 'Homer', 'mother': 'Marge'});
12
+
var couple = new PMap.fromMap({'father': 'Homer', 'mother': 'Marge'});
13
13
// do not (and can not) modify couple anymore
14
14
var withChild = couple.assoc('boy', 'Bart');
15
15
print(couple); // {mother: Marge, father: Homer}
@@ -28,8 +28,8 @@ got a 'great' idea, that instead of just computing response length he also mutat
28
28
Finally, they work as you'd expect. How cool is this:
29
29
30
30
// deeply persist the structure of Maps and Lists
31
-
var a = persist({[1,2]: 'tower', [1,3]: 'water'});
32
-
var b = persist({[1,2]: 'tower', [1,3]: 'water'});
31
+
PMap a = persist({[1,2]: 'tower', [1,3]: 'water'});
32
+
PMap b = persist({[1,2]: 'tower', [1,3]: 'water'});
33
33
assert(a==b);
34
34
// kids, don't try this with standard List, it ain't going to work
35
35
print(a[persist([1, 2])]); // prints 'tower'
@@ -48,21 +48,24 @@ One possible workaround would be to JSONize entries to string and use such strin
48
48
Fast copying or equality done right are nice features, but this is not the only selling point here. Having different ways how to copy (shallow, deep) objects or how to compare them (== vs. equals in Java) introduces new complexity. Even if you get used to it, it still takes some part of your mental capabilities and can lead to errors.
49
49
50
50
### Structure sharing
51
-
var map1 = persist({'a': 'something', 'b': bigMap});
52
-
var map2 = a.assoc('a', 'something completely different');
Suppose you are interested, whether map1['b'] == map2['b']. Thanks to structure sharing, this is O(1) operation, which means it is amazingly fast - no need to traverse through the bigMap. Although it may sound unimportant at the first glance, it is what really enables fast caching of complex functions. Also, this is the reason, why [Om](https://github.com/swannodette/om/) framework is MUCH faster than Facebooks [React](http://facebook.github.io/react/).
54
54
55
55
## And what is the prize for this all
56
-
Short version: size and speed. Although structure sharing makes the whole thing much more effective than naive copy-it-all approach, Persistents still are slower and bigger than their mutable counterparts. Following numbers illustrate, how much less efficient (in terms of speed or memory consumption) are Persistent data structures when benchmarking either on DartVM or Dart2JS on Node:
56
+
In the 2.0 release, we optimized memory consumption such that the only penalty for using Persistent
57
+
comes at lower speed. Although structure sharing makes the whole thing much more effective than naive
58
+
copy-it-all approach, Persistents are still slower than their mutable counterparts (note however, that on
59
+
the other hand, some operations runs significantly faster, so its hard to say something conclusive
60
+
here). Following numbers illustrate, how much slow are Persistent data structures when benchmarking either on DartVM
61
+
or Dart2JS on Node (the numbers are quite independent of the structure size):
57
62
58
-
* DartVM memory: 2.5
59
-
* Dart2JS memory: 6
60
63
* DartVM read speed: 2
61
-
* DartVM write speed: 2.5
62
-
* Dart2JS read speed: 2
63
-
* Dart2JS write speed: 4
64
+
* DartVM write speed: 12 (5 by using Transients)
65
+
* Dart2JS read speed: 3
66
+
* Dart2JS write speed: 14 (6 by using Transients)
64
67
65
-
The good part of the story is, that these numbers are not getting worse, as the Map grows - you get such performance even for Maps with tens of megabytes of data stored within them.
68
+
Although the factors are quite big, the whole operation is still very fast and it probably won't be THE bottleneck which would slow down your app.
66
69
67
70
Some [advanced topics](https://github.com/vacuumlabs/persistent/wiki/Advanced-topics).
0 commit comments