-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance issues with type-constraint checking #42
Comments
Method::Signatures is not intended to replace Mouse accessors. And your benchmark is not a fair fight. Method::Signature is doing more than the Mouse check. With METHOD_SIGNATURES_DEBUG set you can see that it's injecting...
Which is to say:
Doing that in the Mouse test looks like this:
Which is equivalent to this MS code.
I removed the set semantics since that's not what we're testing here. With that check, MS still turns out abysmally slow. 13 secs vs 4 seconds. So that ain't right. Lemme dive in and see what's gone wrong. |
I profiled the type checking with:
and discovered that we're getting clobbered inside the type_check() by two lines:
Those numbers are:
What it tell me is those two lines, and not the subroutines they're calling, are sucking down 40ms. The time spent in type_check() is about 55ms. Ouch. So some micro-optimization is needed. |
Oh, you know what it is? Mouse accessors and type checks are compiled XS code. Method::Signature's pure Perl type_check wrapper is competing with XS code. If you run with So the docs are correct, we're as fast as calling That said, |
In addition to what Schwern pointed out, I would also note the following:
Which is not to say that we can't still be faster, of course. :) @schwern: Perhaps store the type check routine in a local var? Call |
That extra subroutine call was slowing things down. With XS Mouse they're about 40% faster. With Moose they're about 20% faster. If type_check() is overriden, we can't unroll it. For #42
I inlined the contents of type_check(). Looks like this:
Eliminating the method call slashes off 40% of the runtime when using XS Mouse and 20% with Moose. I'm pretty happy with that. :-) Made sure to check if type_check has been overridden first. |
Sounds good. Shall we close this issue? |
Yeah, I'm happy with the result. @rsimoes, if you have more to add just reply and we can reopen. It can probably be made it faster once #34 is done. Then we know the type check mechanism at compile time and don't need the cache... well, that will require some changes to the way class/role/type is resolved. I'll worry about that later, I should be working on Test::Builder. |
Adapting Michael Schwern's benchmarking script mentioned in a blog post, I used it to test
Method::Signatures
type constraints. According toMethod::Signature
's documentation, the "run-time penalty if you do declare types should be very similar to usingMouse::Util::TypeConstraints
(orMoose::Util::TypeConstraints
) directly." I may have made a mistake in my own testing, but this appears to not be the case:In order to get
Method::Signatures
to useMouse
, I commented outMoose
and then reran the test with it included (incidentally,Method::Signature
's time jumped to 30 seconds when it usedMoose
). My fork of the benchmark script can be found at https://gist.github.com/1321873 .The text was updated successfully, but these errors were encountered: