[racket] How can I speed up this code?

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jan 14 16:07:33 EST 2013

Just now, Danny Yoo wrote:
> Also, I have not been able to find the unit tests for the json
> library.  Does anyone know where they are?

They're in the "tests" subdirectory.


> 1.  First, pull all the content of the input port into a string
>     port.  This cut down the runtime from 52 seconds to 45 seconds.
>     (15% improvement)

I don't think that this is a good idea -- it looks lie a dangerous
assumption for a generic library to do, instead of letting users
decide for themselves if they want to do so and hand the port to the
library.


> 2.  Modified read-list so it avoids using regular expressions when
> simpler peek-char/read-char operations suffice.  Reduced the runtime
> from 45 seconds to 40 seconds.  (12% improvement)

This is a questionable change, IMO.  The thing is that keeping things
with regexps makes it easy to revise and modify in the future, but
switching to a single character thing makes it hard and in addition
requires the code to know when to use regexps and when to use a
character.  I prefer in this case the code readability over
performance.

I was hoping that you'll get a more performant code by using one of
the parsers instead, which would also be as manageable as regexps (or
maybe even more).


> 3.  Looked at the profiler, which pointed out that read-string was
> very expensive.  Looked and found the regular expression:
> 
>     rx"^(.*?)(\"|\\\\(.))"
> 
> which is performance-hungry.  Replaced with a char-complement
> version to avoid the "?" part of the pattern:
> 
>     #rx"^([^\"\\]*)(\"|\\\\(.))"
> 
> which cut down the runtime from 40 seconds to 36 seconds.  (11%
> improvement)

This looks fine, but I'll feel better if it comes with tests.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.