[plt-scheme] New Match Implementation

From: Dimitris Vyzovitis (vyzo at media.mit.edu)
Date: Tue Apr 1 12:42:55 EDT 2008

On Tue> (compile '(let () z)), 1 Apr 2008, Sam THfŸ=y=z at Pž$"W^Áž##" wrote:

> On Tue, Apr 1, 2008 at 3:34 AM, Dimitris Vyzovitis <vyzo at media.mit.edu> wrote:
> >  Did you break the optimizer?
> More accurate would be to say that I deleted the optimizer.  The
> algorithm is completely different, and there's no longer a pass over
> the generated code to optimize it.  Unfortunately, that optimizer was
> hopelessly broken.  Fortunately, MzScheme has a decent optimizer that
> should help limit the differences.
> >  It now (well, rev 9096) extracts useless elements from compounds for _
> >  patterns.
> If you time these two examples, I think you'll find no measurable
> difference in performance.  I assume that the useless bindings are
> optimized away.  On this topic, see Kent Dybvig's talk on the
> macro-writers bill of rights, which I believe MzScheme supports all
> of.

Not quite.
You are asking the optimizer to drop an application (in a strict language)
because you don't use the result. This is not something you can do in
general, unless the the application is provably pure (and the compiler is
sufficiently smart to prove it).

This may be possible with primitives like car/cdr (but it is *not*
done), but in general it is hopeless for things like structs where the
accessors are generated by make-struct-type or things that may come from
different modules.

See (using matthew's comp=?):
> (comp=?
 (compile '(let ((x (car y))) z))
 (compile '(let () z)))
#"#~\t3.99.0.21\1\0\0\0\1\0\0\0\0#\0\0\0\237$\24f\237\"\20\2\24\30=y\24\30=z\20\0\e\370\26 at P\236$\"W^\27\301\1P\236##"

> (comp=?
 (compile '(lambda (o) (let ((x (car o))) o)))
 (compile '(lambda (o) o)))

> (let ()
  (define-struct test (x y))
   (compile '(lambda (o) (let ((x (test-x o)) (y (test-y o))) o)))
   (compile '(lambda (o) o))))

As for timing, if you only measure trivial 1 arm patterns of course you
won't see any difference. But it will be there in complicated patterns,
which was exactly what the old optimizer handled well.

> This is something that can, and hopefully will, be improved.  It's a
> fairly straightforward optimization, but not one that I've had time to
> implement yet.

If you get around doing that, can you generalize it for (app ...) patterns
and not just the built-in expanders?

-- vyzo

Posted on the users mailing list.