I need to create a big string out of a complex structure and the runtime is way too long.
Back in the Java
times I solved similar problems using StringBuffer or StringBuilder which greatly reduce the number of intermediate String objects needed.
Is there something similar in Raku
?
StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer. String concatenation operator (+) internally uses StringBuffer or StringBuilder class.
In contrast to Java's String class, StringBuffer and StringBuilder are both mutable classes. The StringBuilder class and the StringBuffer class have the same API. The sole distinction is that whereas StringBuilder is neither synchronized nor thread-safe, StringBuffer is.
StringBuffer is synchronized and therefore thread-safe. StringBuilder is compatible with StringBuffer API but with no guarantee of synchronization. Because it's not a thread-safe implementation, it is faster and it is recommended to use it in places where there's no need for thread safety.
String buffer and StringBuilder both are mutable classes which can be used to do operation on string objects such as reverse of string, concating string and etc. We can modify string without creating a new object of the string. A string buffer is thread-safe whereas string builder is not thread-safe.
Native string arrays pretty much allow you to do that.
my str @parts;
@parts.push("foo")
...
say @parts.join;
Is that what you're looking for?
Additionally: on the MoarVM backend, when you concat strings, they are not actually concatenated in memory, but rather just "linked" together into a single virtual string. Unfortunately, the moment you want to do a regular expression on a string, if does need to be flattened. Which is one of the reasons regular expressions are relatively expensive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With