Ok, looking more at the commit, I think this is not actually a bug
anywhere, but really an unfortunate combination of things that I don't
have an idea for improving at the moment. Sorry for not realizing that
earlier.

First, I think you should just go back to using `Any` in the type for
`pack` and for `pack-hash` and `pack-sequence`. I don't think that
will cause any problems for you or users of your code, and will make
the performance problem go away. For functions where `Packable`
appears in the _result_, such as `unpack`, you should keep using
`Packable` -- that will be both faster and will avoid contract errors
that your users might otherwise encounter.

More generally, what happened here was that you made the types you
export _more restrictive_ by changing `Any` to `Packable` in the
argument types. That is, in the old code, anyone could pass any value
to `pack` and either get the result or a dynamic error. That means
that Typed Racket could generate a very cheap contract for `pack`. By
changing it to a more restrictive type, you get to assume in the body
of `pack` that the input is `Packable`, but Typed Racket then
generates a very expensive contract to check that. From your code, it
doesn't look like you're making use of that additional assumption, so
it's just costing you a lot of performance.

As to why that contract is so expensive, the short answer is that
contracts for mutable data like procedures and hash tables have to
construct wrapper objects, which involves a lot of extra allocation
and indirection, on top of the usual expense of contract checking.
That's why things got faster with Ben's modifications.

Finally, why use `Packable` in the result type of `unpack`? Here, the
contract for `Any` isn't simple and inexpensive, since you're sharing
potentially arbitrary values with untyped code, so Typed Racket
constructs a complicated contract (called `any-wrap/c`) in order to
protect it. That contract will also error in cases where it doesn't
know what to do, which the contract for `Packable` won't.

Sam

On Sun, Dec 3, 2017 at 6:53 PM, HiPhish <hiph...@openmailbox.org> wrote:
> Anything more I can do?
> On Sunday, December 3, 2017 at 6:11:42 PM UTC+1, Sam Tobin-Hochstadt wrote:
>>
>> Thanks, that's very helpful. It's clear that the contract optimization is
>> working in the old code but not the new code, and we need to fix that.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to