clojure - How to design an api to a persistent collection in C#? -
i thinking creating persistent collection (lists or other) in c#, can't figure out api.
i use 'persistent' in clojure sense: persistent list list behaves if has value semantics instead of reference semantics, not incur overhead of copying large value types. persistent collections use copy-on-write share internal structure. pseudocode:
l1 = persistentlist() l1.add("foo") l1.add("bar") l2 = l1 l1.add("baz") print(l1) # ==> ["foo", "bar", "baz"] print(l2) # ==> ["foo", "bar"] # l1 , l2 share common structure of ["foo", "bar"] save memory
clojure uses such datastructures, additionally in clojure data structures immutable. there overhead in doing copy-on-write stuff clojure provides workaround in form of transient datastructures can use if sure you're not sharing datastructure else. if have reference datastructure, why not mutate directly instead of going through copy-on-write overhead.
one way efficiency gain keep reference count on datastructure (though don't think clojure works way). if refcount 1, you're holding reference updates destructively. if refcount higher, else holding reference that's supposed behave value type, copy-on-write not disturb other referrers.
in api such datastructure, 1 expose refcounting, makes api less usable, or 1 not refcounting, leading unnecessary copy-on-write overhead if every operation cow'ed, or api loses it's value type behaviour , user has manage when cow manually.
if c# had copy constructors structs, possible. 1 define struct containing reference real datastructure, , incref()/decref() calls in copy constructor , destructor of struct.
is there way reference counting or struct copy constructors automatically in c#, without bothering api users?
edit:
- just clear, i'm asking api. clojure has implementation of written in java.
- it possible make such interface using struct reference real collection cow'ed on every operation. use of refcounting optimisation avoid unnecessary cowing, apparently isn't possible sane api.
what you're looking isn't possible, strictly speaking. close using static functions reference counting, understand that isn't terrible palatable option.
even if possible, i stay away this. while semantics describe may useful in clojure, cross between value type , reference type semantics confusing c# developers (mutable value types--or types value type semantics mutable--are considered evil).
Comments
Post a Comment