ChezScheme icon indicating copy to clipboard operation
ChezScheme copied to clipboard

Better immutable string support?

Open amirouche opened this issue 5 years ago • 1 comments

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?

amirouche avatar Jan 07 '20 10:01 amirouche

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.

LiberalArtist avatar Jun 27 '20 07:06 LiberalArtist