Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Jens Axel Søgaard
As Bogdan writes, the problem is repeatedly calling `string-append`. Instead one can make a list of all the small strings and then use `string-join` at the end. #lang racket (require racket/flonum) (define (xy->string x y) (string-append (~r x #:precision 1) "," (~r y #:precision 1)))

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Bogdan Popa
I forgot to mention this earlier, but it might be enlightening to run the two versions of the program with the `PLTSTDERR` environment variable set to "error debug@GC" to see GC debug messages. For example: env PLTSTDERR='error debug@GC' racket flvector-to-coordinates-string.rkt >/dev/null

[racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Alessandro Motta
Hi racket-users! I've recently become interested in Lisp/Scheme and have started to hack in Racket. The excellent documentation, the fast integrated search, and DrRacket have made that a real pleasure. Thank you for that! I've been working on a tool to convert notes from the reMarkable 2 tablet

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Robby Findler
Replacing ` (~r x #:precision 1)` with `(number->string x)` and ditto for `y` eliminates the overhead of contracts and brings about another 4x speedup on my machine. That may not work, tho, depending on Alessandro's original usecase, since the strings are different. I was also going to, as

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Bogdan Popa
Hi Alessandro, Here is a version of your program that is about 30 times faster on my machine (9s -> 300ms): #lang racket/base (require racket/flonum racket/format racket/port) (define (xy-vectors->string x-vec y-vec) (call-with-output-string

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Robby Findler
On Sun, Jun 27, 2021 at 11:58 AM Alessandro Motta wrote: > > I also like Jens' code for its pure functional elegance. I'm surprised > that building up a long list of short strings before joining them is so > much faster. After all, isn't the final `string-join` essentially doing > what the

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Jens Axel Søgaard
Den søn. 27. jun. 2021 kl. 18.58 skrev Alessandro Motta : > I also like Jens' code for its pure functional elegance. I'm surprised > that building up a long list of short strings before joining them is so > much faster. After all, isn't the final `string-join` essentially doing > what the

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread jackh...@gmail.com
Anyone got an implementation of a mutable StringBuilder-like object? I could use it in Rebellion's implementation of `into-string` which currently isn't quadratic, but it definitely has the allocation problem. On Sunday, June 27, 2021 at 11:36:14 AM UTC-7 bogdan wrote: > While I think the

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Bogdan Popa
While I think the complexity piece is important, I feel like it's worth pointing out just how much more expensive the allocation -- and its ramifications, like the resulting GC pressure and CPU cache misses -- is than one might think. Here's a quadratic version of the code that avoids

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Bogdan Popa
Matthew Flatt writes: > At Sun, 27 Jun 2021 21:36:09 +0300, Bogdan Popa wrote: > Your program does run fast on my machine, but I think it's because this > line doesn't have the intended bad effect: > >> (string-copy! dst 0 dst 0 len) ;; intentionally performing pointless >>

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Alessandro Motta
Thank you all for these quick and helpful responses! I've noticed the `call-with-output-string` mechanism while browsing various Racket code bases, but was under the impression that this is "just" a convenient abstraction over strings. Based on Bogdan's code and Robby's explanation, it's now

Re: [racket-users] Speeding up the conversion of flvectors to string

2021-06-27 Thread Matthew Flatt
At Sun, 27 Jun 2021 21:36:09 +0300, Bogdan Popa wrote: > While I think the complexity piece is important, I feel like it's worth > pointing out just how much more expensive the allocation -- and its > ramifications, like the resulting GC pressure and CPU cache misses -- is > than one might think.