Skip Menu |
 

This queue is for tickets about the PDF-API2 CPAN distribution.

Report information
The Basics
Id: 105581
Status: resolved
Priority: 0/
Queue: PDF-API2

People
Owner: Nobody in particular
Requestors: DMITRI [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 2.023
Fixed in: 2.024



Subject: Patch: speed up PDF::API2::Resource::BaseFont::width
Download (untitled) / with headers
text/plain 133b
Speed up PDF::API2::Resource::BaseFont::width by caching results of wxByEnc() method call and taking conditional outside of the loop.
Subject: speed-up-width-patch.txt
diff --git a/lib/PDF/API2/Resource/BaseFont.pm b/lib/PDF/API2/Resource/BaseFont.pm index 1d974b6..4e31b55 100644 --- a/lib/PDF/API2/Resource/BaseFont.pm +++ b/lib/PDF/API2/Resource/BaseFont.pm @@ -624,17 +624,25 @@ is used either in native or utf8 format (check utf8-flag). sub width { my ($self,$text)=@_; my $width=0; + my @widths_cache; if(is_utf8($text)) { $text=$self->strByUtf($text) } - my $lastglyph=''; - foreach my $n (unpack('C*',$text)) - { - $width+=$self->wxByEnc($n); - if($self->{-dokern} && ref($self->data->{kern})) + if ($self->{-dokern} && ref($self->data->{kern})) { + my $lastglyph=''; + foreach my $n (unpack('C*',$text)) { - $width+=$self->data->{kern}->{$lastglyph.':'.$self->data->{e2n}->[$n]}; - $lastglyph=$self->data->{e2n}->[$n]; + $width += ($widths_cache[$n] //= $self->wxByEnc($n)); + if($self->{-dokern} && ref($self->data->{kern})) + { + $width+=$self->data->{kern}->{$lastglyph.':'.$self->data->{e2n}->[$n]}; + $lastglyph=$self->data->{e2n}->[$n]; + } + } + } else { + foreach my $n (unpack('C*',$text)) + { + $width += ($widths_cache[$n] //= $self->wxByEnc($n)); } } $width/=1000;
You can use the script attached to RT #105579 to do timing runs.
Download (untitled) / with headers
text/plain 141b
Sorry, the patch has an extra if ($self->{-dokern} && ref($self->data->{kern})) inside the first loop. Obviously, it should be removed.
Download (untitled) / with headers
text/plain 215b
On Tue Jun 30 15:33:48 2015, DMITRI wrote: Show quoted text
> Sorry, the patch has an extra > > if ($self->{-dokern} && ref($self->data->{kern})) > > inside the first loop. Obviously, it should be removed.
Here's fixed patch.
Subject: speed-up-width-patch2.txt
diff --git a/lib/PDF/API2/Resource/BaseFont.pm b/lib/PDF/API2/Resource/BaseFont.pm index 1d974b6..cf9f29a 100644 @@ -624,18 +615,23 @@ is used either in native or utf8 format (check utf8-flag). sub width { my ($self,$text)=@_; my $width=0; + my @widths_cache; if(is_utf8($text)) { $text=$self->strByUtf($text) } - my $lastglyph=''; - foreach my $n (unpack('C*',$text)) - { - $width+=$self->wxByEnc($n); - if($self->{-dokern} && ref($self->data->{kern})) + if ($self->{-dokern} && ref($self->data->{kern})) { + my $lastglyph=''; + foreach my $n (unpack('C*',$text)) { + $width += ($widths_cache[$n] //= $self->wxByEnc($n)); $width+=$self->data->{kern}->{$lastglyph.':'.$self->data->{e2n}->[$n]}; $lastglyph=$self->data->{e2n}->[$n]; } + } else { + foreach my $n (unpack('C*',$text)) + { + $width += ($widths_cache[$n] //= $self->wxByEnc($n)); + } } $width/=1000; return($width);
Download (untitled) / with headers
text/plain 521b
Nice. I've committed this, with one change that you can see here: https://bitbucket.org/ssimms/pdfapi2/commits/8baf3730a6432e628593d33d060ebf1512eb2c85 It eliminates some repeated code by saving the kern check as a variable. The performance is roughly the same. Tip: The next time you give someone a performance patch, you can make it easier for the committer by mentioning how much it speeds things up. I had to make up some benchmarking code to test this patch, which almost made me put it off. Thanks! :-) Steve
Download (untitled) / with headers
text/plain 802b
On Tue Jun 30 20:41:57 2015, SSIMMS wrote: Show quoted text
> Nice. I've committed this, with one change that you can see here: > https://bitbucket.org/ssimms/pdfapi2/commits/8baf3730a6432e628593d33d060ebf1512eb2c85 > > It eliminates some repeated code by saving the kern check as a > variable. The performance is roughly the same.
I was going for *fast*, so I was OK with a bit of code duplication. Show quoted text
> Tip: The next time you give someone a performance patch, you can make > it easier for the committer by mentioning how much it speeds things > up. I had to make up some benchmarking code to test this patch, which > almost made me put it off.
You must have missed my comment: https://rt.cpan.org/Ticket/Display.html?id=105581#txn-1513105 Show quoted text
> Thanks! :-)
You're very welcome. Looking forward to next release.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.