[racket] substring without the copy

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Apr 11 06:12:19 EDT 2014

I don't see any type specs in srfi 13 for substring/shared so I doubt very much that the slowdown has anything to do with TR. Avoid jumping to conclusions or explain more so we can judge the problem. If you are writing code in TR, run it thru Vincent's feature profiler to find out whether it really is the string? predicate generated from the String type. Also consider conducting a small 'untyped' experiment. If it is the contract, please do report back. 

-- Matthias





On Apr 4, 2014, at 10:23 PM, Spencer florence wrote:

> 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.
> 
> 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?.
> 
> 
> On Fri, Apr 4, 2014 at 4:12 PM, John Clements <clements at brinckerhoff.org> wrote:
> 
> 
> On Apr 3, 2014, at 7:08 PM, Spencer Florence <spencer at florence.io> wrote: 
> 
> > 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. 
> 
> Seems like it would be fairly easy to roll this yourself: 
> 
> #lang racket 
> 
> (require rackunit) 
> 
> ;; represent a lazy substring operation: 
> (struct lazysubstring (str start end)) 
> 
> ;; force a lazy substring 
> (define (lss-force s) 
> (substring (lazysubstring-str s) 
> (lazysubstring-start s) 
> (lazysubstring-end s))) 
> 
> ;; get the length of a lazy substring: 
> (define (lss-len s) 
> (- (lazysubstring-end s) (lazysubstring-start s))) 
> 
> ;; create one: 
> (define source-text "othho tot stnh ontuh .nt") 
> 
> (define lss1 (lazysubstring source-text 3 9)) 
> (check-equal? (lss-len lss1) 6) 
> 
> Obviously, you have to “roll your own” for any operation that you want to perform that retains the laziness. 
> 
> John 
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140411/8c98a9a3/attachment.html>

Posted on the users mailing list.