composablesys / collabs

Collabs library monorepo
https://collabs.readthedocs.io/
Apache License 2.0
256 stars 11 forks source link

CRichText (Peritext implementation) #249

Closed mweidner037 closed 1 year ago

mweidner037 commented 1 year ago

Adds CRichText to @collabs/crdts and @collabs/collabs, with reasonably thorough tests.

Still todo:

I plan to leave shared cursors and editor bindings for a future PR.

mweidner037 commented 1 year ago

Fun performance bug I found by accident: I forgot to use StringSerializer for CRichText's internal list, so it was using DefaultSerializer instead. This resulted in much larger save sizes & longer save times, but it somehow reduced memory usage by 50% relative to CText.

It turns out that StringSerializer was using a different library than DefaultSerializer for strings (buffer vs protobufjs). The former somehow outputs deserialized strings that use twice as much memory, perhaps because they are stored as UTF-16 chars instead of 1-byte ASCII (even though their content is usually ASCII).

I changed StringSerializer to use protobufjs instead, so now CText's memory usage has been halved (12MB -> 6MB on Martin's text trace). I also fixed the original bug (CRichText custom serializer), so its behavior is now comparable to CText when there is no formatting, as expected.