

Persistent Data Structures for Objective-C - astashov
https://github.com/astashov/persistent.objc

======
SeoxyS
Objective-C data structures are already immutable by default. What do these
persistent data structures bring to the table that the default immutable
NSDictionary, NSArray, NSSet, etc., don't?

~~~
astashov
They are immutable by default, but it's expensive to get a new data structure
with some changes in it. That's what persistent data structures for. I
personally like this explanation -
[https://github.com/vacuumlabs/persistent#what-are-
persistent...](https://github.com/vacuumlabs/persistent#what-are-persistent-
data-structures)

~~~
jawngee
Expensive how?

Your classes are 10-20x slower in every other way, which definitely would
offset any imaginary expense you think calling `mutableCopy` costs.

I have to tell you in 10+ years of writing Objective-C where I was like
[myArray mutableCopy] is super slow, I need a replacement. I've wished for an
NSOrderedDictionary though.

~~~
astashov
10-20x slower means it makes sense to use a persistent map when the size of
your Dictionary is > 20 records.

Then,

    
    
      // yourDictionary is NSMutableDictionary, [yourDictionary count] > 20
      NSMutableDictionary *mutable = [yourDictionary mutableCopy];
      mutable[@"old_key"] = @"new_value";
      NSDictionary *changedDict = [NSDictionary dictionaryWithDictionary:mutable];
    

will be slower than

    
    
      // yourDictionary is AAPersistentHashMap, [yourDictionary count] > 20
      AAPersistentHashMap *changedDict = [yourDictionary setObject:@"new_value" forKey:@"old_key"];
    

Remember, you need to get another immutable map, which is a modified version
of the original, preferably memory-efficient, which means we want to reuse the
unchanged keys/values of the original map.

------
spenen2000
Does anybody know if there is anything similar for golang?

~~~
grayrest
The underlying datastructure is a Hash Array Mapped Trie. Searching for
"golang hamt" and "golang immutable" turns up a number of results but I didn't
look into any of them but they're generally clones of Clojure's version.

