[plt-scheme] ->d Performance

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sat Apr 11 15:55:59 EDT 2009

->d actually evaluates the expressions (and/c
exact-nonnegative-integer? (</c (matrix-rows matrix))) each time you
call the function, and that evaluation involves building up a contract
combinator (and doing various error checking) before actually checking
the contract. That's where the extra overhead comes from. In
comparison with the -> contract, all of that work is done once, before
the contract is even attached to the value. I have experimented with
various optimizations to avoid this work, but there is a lot to do to
make that happen.

Robby

On Sat, Apr 11, 2009 at 2:51 PM, Doug Williams
<m.douglas.williams at gmail.com> wrote:
> Yes, the -> is fine. And, so is doing the same bounds check in my own
> procedure. I was surprised that the ->d was so much slower. I use contracts
> regularly and was trying to expand my usage of them.
>
> On Sat, Apr 11, 2009 at 12:46 PM, Robby Findler
> <robby at eecs.northwestern.edu> wrote:
>>
>> ->d is definitely substantially slower than the other because the
>> wrappers are more complex. Are you finding the performance overhead of
>> the ordinary -> acceptable?
>>
>> Robby
>>
>> On Sat, Apr 11, 2009 at 1:17 PM, Doug Williams
>> <m.douglas.williams at gmail.com> wrote:
>> > I would like to use ->d to impose a precondition for a function.  For
>> > example:
>> >
>> >  (matrix-ref
>> >   (->d ((matrix matrix?)
>> >         (i (and/c exact-nonnegative-integer? (</c (matrix-rows
>> > matrix))))
>> >         (j (and/c exact-nonnegative-integer? (</c (matrix-cols
>> > matrix)))))
>> >        ()
>> >        (result any/c)))
>> >
>> > or
>> >
>> >  (matrix-ref
>> >   (->d ((matrix matrix?)
>> >         (i exact-nonnegative-integer?)
>> >         (j exact-nonnegative-integer?))
>> >        ()
>> >        #:pre-cond (and (< i (matrix-rows matrix))
>> >                        (< j (matrix-cols matrix)))
>> >        (result any/c)))
>> >
>> > instead of just
>> >
>> >  (matrix-ref
>> >   (-> matrix? exact-nonnegative-integer? exact-nonnegative-integer?
>> > any/c))
>> >
>> > The first two do work, but are really, really slow.
>> >
>> > I like having the bounds check in the contract (as opposed to bounds
>> > check
>> > in the matrix-ref code, but can't accept the performance hit. Any ideas?
>> >
>> > Doug
>> >
>> >
>> > _________________________________________________
>> >  For list-related administrative tasks:
>> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>> >
>> >
>
>


Posted on the users mailing list.