Subject: | validate_pos() is very slow in some situations (PP.pm) |
Date: | Thu, 18 Jun 2015 13:03:57 +0200 |
To: | bug-params-validate [...] rt.cpan.org |
From: | "Klaus Rindfrey" <k-rindfrey [...] gmx.de> |
Hi Dave,
here's a problem with validate_pos() along with a patch that solves it:
After adding some missing validate_pos() calls to a script that generates complex xml i found the script running horribly slow:
It run ca 20 seconds before adding validate_pos() but 970 seconds after - about 46 times slower!
I have something like this:
sub do_something {
validate_pos(@_, {type => OBJECT});
#...
}
and pass XML::LibXML::* objects to that subroutine.
I analyzed the situation using Devel::NYTProf and it turned out that the problem is in statement in line 102 of PP.pm (note that i run the pure perl implementation):
my $value = defined $p[$_] ? qq|"$p[$_]"| : 'undef';
Expression "$p[$_]" is problematic because XML::LibXML overloads the ""-operator, so "$p[$_]" triggers a complex method call. In case you pass a node (or document) having a large and complex structure, this takes a while to be computed and produces a large string. However, looking at your code i found that the result is normally thrown away and only used in case of an error. So i did a patch that evaluates "$p[$_]" only when needed (patched PP.pm is attached to this mail).
I also created a small example script (attachment miniexample.pl, not really minimal, but small enough to see what's going on). On my machine (Win7 running strawberry perl 5.18) this runs:
- ca 3 seconds if Params::Validate is completey commented out
- ca 360 seconds with Params::Validate 1.19 (unpatched)
- ca 3.1 seconds with Params::Validate 1.19 containing the patch attached to this mail
Hope you can fix the problem in a future version of Params::Validate. Till then i'll use the patch.
Regards,
Klaus
Message body is not shown because sender requested not to inline it.
Message body is not shown because sender requested not to inline it.