picrin icon indicating copy to clipboard operation
picrin copied to clipboard

immutable string by default, use `transient-string` to modify strings efficiently

Open nyuichi opened this issue 11 years ago • 7 comments

nyuichi avatar Feb 18 '14 07:02 nyuichi

code example?

omasanori avatar Feb 19 '14 01:02 omasanori

No concrete code, only proposal. But the API could be like:

string-set!: completely clone the string chunk.
transient-string-set!: performed in O(1) time

also string->transient and transient->string would be provided. With this style we could use some optimization techniques like rope representation against normal strings at the tradeoff of incredibly slow string-set!.

nyuichi avatar Feb 19 '14 05:02 nyuichi

(let* ((a "car") (b (string->transient a)))
  (transient-string-set! b 1 #\d)
  (list a (transient->string b)))

What about this?

Clojure's transients will show

("car" "cdr")

Clojure doesn't provide transient strings, though.

omasanori avatar Feb 19 '14 06:02 omasanori

(What I said "code example" is not actual implementations but use cases in Scheme, sorry.)

omasanori avatar Feb 19 '14 06:02 omasanori

@omasanori

Though they have the same name, clojure's transient structures and picrin's that are fundamentally different things. transient-string is rather similar to Java's StringBuffer, normal string to String class. And in the case of your example, that would be the right behavior. string->transient would create a newly allocated buffer to contain byte (or char) sequences.

I'm not so against to changing the name of transient object.

nyuichi avatar Feb 19 '14 06:02 nyuichi

OK, I like it.

omasanori avatar Feb 19 '14 07:02 omasanori

Hi guys, the internal representation of picrin's string is now switched to rope! ;) 8586dc9

nyuichi avatar Feb 27 '14 13:02 nyuichi