texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[6221] count_bytes slight optimizations


From: Gavin D. Smith
Subject: [6221] count_bytes slight optimizations
Date: Mon, 13 Apr 2015 16:55:19 +0000

Revision: 6221
          http://svn.sv.gnu.org/viewvc/?view=rev&root=texinfo&revision=6221
Author:   gavin
Date:     2015-04-13 16:55:18 +0000 (Mon, 13 Apr 2015)
Log Message:
-----------
count_bytes slight optimizations

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/tp/Texinfo/Common.pm

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog     2015-04-12 18:20:51 UTC (rev 6220)
+++ trunk/ChangeLog     2015-04-13 16:55:18 UTC (rev 6221)
@@ -1,3 +1,9 @@
+2015-04-13  Gavin Smith  <address@hidden>
+
+       * tp/Texinfo/Common.pm (count_bytes): Add special handling of 
+       case when output encoding is UTF-8, and for other encodings save 
+       result of Encode::find_encoding between function calls.
+
 2015-04-12  Gavin Smith  <address@hidden>
 
        * tp/Texinfo/Convert/Plaintext.pm (new_formatter): Replace

Modified: trunk/tp/Texinfo/Common.pm
===================================================================
--- trunk/tp/Texinfo/Common.pm  2015-04-12 18:20:51 UTC (rev 6220)
+++ trunk/tp/Texinfo/Common.pm  2015-04-13 16:55:18 UTC (rev 6221)
@@ -1667,6 +1667,10 @@
   return %options;
 }
 
+# Used in count_bytes
+my $Encode_encoding_object;
+my $last_encoding;
+
 sub count_bytes($$;$) 
 {
   my $self = shift;
@@ -1677,8 +1681,32 @@
     $encoding = $self->get_conf('OUTPUT_PERL_ENCODING');
   }
 
-  if ($encoding and $encoding ne 'ascii') {
-    return length(Encode::encode($encoding, $string));
+  if ($encoding eq 'utf-8-strict') {
+    if (Encode::is_utf8($string)) {
+      # Get the number of bytes in the underlying storage.  This may
+      # be slightly faster than calling Encode::encode_utf8.
+      use bytes;
+      return length($string);
+
+      # Here's another way of doing it.
+      #Encode::_utf8_off($string);
+      #my $length = length($string);
+      #Encode::_utf8_on($string);
+      #return $length
+    } else {
+      return length(Encode::encode_utf8($string));
+    }
+  } elsif ($encoding and $encoding ne 'ascii') {
+    if (!defined($last_encoding) or $last_encoding ne $encoding) {
+      # Look up and save encoding object for next time.  This is
+      # slightly faster than calling Encode::encode.
+      $last_encoding = $encoding;
+      $Encode_encoding_object = Encode::find_encoding($encoding);
+      if (!defined($Encode_encoding_object)) {
+        Carp::croak "Unknown encoding '$encoding'";
+      }
+    }
+    return length($Encode_encoding_object->encode($string));
   } else {
     return length($string);
     #my $length = length($string);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]