ChezScheme
ChezScheme copied to clipboard
Better immutable string support?
I am looking at Chez immutable string support to implement SRFI-135 aka. (scheme text) as part of arew.
The main problem I see with current immutable string support is that string-fu procedures, given an immutable string will return a mutable string which in turns require to copy the string into an immutable string via string->immutable-string.
In particular it is the case of substring:
https://github.com/cisco/ChezScheme/blob/a3ea77d5a81acbef7542942d3e2e261f02b58bcd/s/5_4.ss#L19-L31
Which will be copied to convert it to a immutable string:
https://github.com/cisco/ChezScheme/blob/a3ea77d5a81acbef7542942d3e2e261f02b58bcd/s/5_4.ss#L112-L121
This is very innefficient.
How can Chez scheme better support immutable strings?
You can avoid the copy by using the primitive string-set-immutable!, as in the implementation of string->immutable-string. This primitive is unsafe because it breaks the assumption that a string's mutability status is fixed, like its length. It should be used only if no other reference to its argument exists. A safe library for immutable strings should provide versions of functions like string-append that use string-set-immutable! on the freshly-allocated mutable strings they return.