f# - Proper way to define a large immutable type with pieces that get updated? -
i'm designing game object in f#. in c++, make classes represent graphical aspect, physical etc each dynamic values, , add instances of classes gameobject. use same design in f# mutable types, i'm trying keep immutable. if used same design recreating thousands of large objects each frame, , spend cpu time allocating.
is there way can define type link object's values , supply new ones, cut down on allocations?
eg change colour of box, want use old box's memory new graphics piece, , use old graphics piece's memory new colour:
let box = ... ... let changedbox = {box graphics = { box.graphics colour = blue} }
yes, exactly. assuming "the big object" in fact reference graph of many smaller objects, typical "update of immutable" creates 1 or 2 new small objects , references same old ones rest.
for example, if person has name , address, , address has street , city , state , zip, , need update zip of person, need create new objects spanning tree root zip. see e.g. red-black color diagrams in this blog. in person example, name, street, city, , state objects reused, zip new object, , address new object (since contains new zip), , person new object (since contains new address). make sense?
Comments
Post a Comment