Closed Varriount closed 1 year ago
If I understand it correct, you can do this only when ref count is 1.
@trustable-code Yes, this only applies to strings with a recount of 1. Thankfully, due to strings being value types, this is the case 95% of the time.
Interestingly, this could be a good idea for the upcoming strings based on destructors too.
We have moves now, in the example the addresses of b
and c
are both sustained when moving into a
Bites with COW-strings which hopefully will come to Nim, eventually.
Currently, whenever a string value is assigned to a variable, the string value is copied into a new piece of memory, regardless of the contents of the previous value. This can be optimized in certain cases - if assigning a value to an already initialized string variable, and the value being assigned is the same length or smaller than the already initialized string, than a simple memory copy can be done, rather than a dereference of the old string and creation of a new string.
Test case:
Though this means checking the length of the old string variable on each assignment, this overhead is probably much less than that of allocating memory for a new string and garbage collecting the old string.