Two small performance issue in COSString, these are not bugs
------------------------------------------------------------

                 Key: PDFBOX-410
                 URL: https://issues.apache.org/jira/browse/PDFBOX-410
             Project: PDFBox
          Issue Type: Improvement
    Affects Versions: 0.8.0-incubator
         Environment: All
            Reporter: [email protected]
            Priority: Trivial


There were 2 small issues here. Both performance related, but I assume that 
this method createFromHexString is called a lot.

These are not bugs.
>From this article:
http://www.javaworld.com/javaworld/jw-04-1997/jw-04-optimize.html?page=5

Comparing against zero is almost always more efficient in any language since 
the underlying tests are based on < 0,<= 0, == 0, != 0, >= 0 and > 0. 

package org.apache.pdfbox.cos;
// original code.
public static COSString createFromHexString( String hex ) throws IOException
        COSString retval = new COSString();
        StringBuffer hexBuffer = new StringBuffer( hex.trim() );
        //if odd number then the last hex digit is assumed to be 0
        if( hexBuffer.length() % 2 == 1 )
        {
            hexBuffer.append( "0" );
        }
        for( int i=0; i<hexBuffer.length();)
        {
// Same code with 3 small changes applied.
// test for null optional, since it should never happen
// compare to zero instead of 1
// added loop invariant variable hexBufferLength

public static COSString createFromHexString( String hex ) throws IOException
        COSString retval = new COSString();
         // should you add a test for null here?
         if (hex == null)  // this should never happen
   return null; 
            // or 
               return retval; // ??
        StringBuffer hexBuffer = new StringBuffer( hex.trim() );

// slightly changed code to improve effiency of the loop.
int hexBufferLength= hexBuffer.length();
if( hexBufferLength % 2 != 0 )  // add comparison against zero values of mod 2 
can only be 0 or 1
        {
            hexBuffer.append( "0" );
        }
// hexBuffer.length() is a loop invariant, and will not change within the loop,
// calling the method on each iteration is not efficient.
// better to create a local variable and reference it at the top of the loop.
for( int i=0; i<hexBufferLength;)
{



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to