When to use Ropes over StringBuilders?

Technology CommunityCategory: StringsWhen to use Ropes over StringBuilders?
VietMX Staff asked 3 years ago

Rope is excellent if there are lots of prefixing and potentially better for insertions; StringBuilders use continuous memory, so only work efficiently for appending.

  • StringBuilder is great for building strings by appending fragments – a very normal use-case. As developers need to do this a lot, StringBuilders are a very mainstream technology.
  • Ropes are great for edit buffers, e.g. the data-structure behind, say, an enterprise-strength TextArea. So (a relaxation of Ropes, e.g. a linked list of lines rather than a binary tree) is very common in the UI controls world, but that’s not often exposed to the developers and users of those controls.

You need really really big amounts of data and churn to make the rope pay-off – processors are very good at stream operations, and if you have the RAM then simply realloc for prefixing does work acceptably for normal use-cases.