<span id="mailbox-conversation">Unfortunately that eventually does require running the true substring operation. All of my substrings are short-lived values that are used immediately, so being lazy doesn't help much. My issue is that Im running substring several million times to generate very short-lived values that are almost immediately ready to be collected.<div><br></div>
<div>substring/shared does seem to be what I want, although for some reason using that instead of substring is actually much (~30%) slower. My initial guess as to why is TR contract boundary shenanigans, although that also seems odd given that the only contract that should be generated by requiring substring/shared is string?.</div></span><br><br><div class="gmail_quote"><p>On Fri, Apr 4, 2014 at 4:12 PM, John Clements <span dir="ltr"><<a href="mailto:clements@brinckerhoff.org" target="_blank">clements@brinckerhoff.org</a>></span> wrote:<br></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>On Apr 3, 2014, at 7:08 PM, Spencer Florence <spencer@florence.io> wrote:
<br><br>> Is there any way to perform `substring` without creating a new string? I'm working with lots of very large and (for all intents and purposes) immutable strings and would like to avoid the extra allocations and copy time.
<br><br>Seems like it would be fairly easy to roll this yourself:
<br><br>#lang racket
<br><br>(require rackunit)
<br><br>;; represent a lazy substring operation:
<br>(struct lazysubstring (str start end))
<br><br>;; force a lazy substring
<br>(define (lss-force s)
<br> (substring (lazysubstring-str s)
<br> (lazysubstring-start s)
<br> (lazysubstring-end s)))
<br><br>;; get the length of a lazy substring:
<br>(define (lss-len s)
<br> (- (lazysubstring-end s) (lazysubstring-start s)))
<br><br>;; create one:
<br>(define source-text "othho tot stnh ontuh .nt")
<br><br>(define lss1 (lazysubstring source-text 3 9))
<br>(check-equal? (lss-len lss1) 6)
<br><br>Obviously, you have to “roll your own” for any operation that you want to perform that retains the laziness.
<br><br>John
<br><br></blockquote></div><br>