You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mi...@apache.org on 2004/04/27 19:57:06 UTC

cvs commit: xml-xalan/java/src/org/apache/xml/utils Trie.java

minchau     2004/04/27 10:57:06

  Modified:    java/src/org/apache/xml/serializer ToHTMLStream.java
               java/src/org/apache/xml/utils Trie.java
  Log:
  
  PR: bugzilla 28435
  Submitted by:	Brian Minchau
  Reviewed by:	Henry Zongaro
  
  Revision  Changes    Path
  1.30      +21 -2     xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java
  
  Index: ToHTMLStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- ToHTMLStream.java	1 Apr 2004 18:45:30 -0000	1.29
  +++ ToHTMLStream.java	27 Apr 2004 17:57:06 -0000	1.30
  @@ -546,6 +546,24 @@
               return (ElemDesc)obj;
           return m_dummy;
       }
  +    
  +    /**
  +     * A Trie that is just a copy of the "static" one.
  +     * We need this one to be able to use the faster, but not thread-safe
  +     * method Trie.get2(name)
  +     */
  +    private Trie m_htmlInfo = new Trie(m_elementFlags);
  +    /**
  +     * Calls to this method could be replaced with calls to
  +     * getElemDesc(name), but this one should be faster.
  +     */
  +    private ElemDesc getElemDesc2(String name)
  +    {
  +        Object obj = m_htmlInfo.get2(name);
  +        if (null != obj)
  +            return (ElemDesc)obj;
  +        return m_dummy;
  +    }
   
       /**
        * Default constructor.
  @@ -702,7 +720,8 @@
           
           try
           {
  -            ElemDesc elemDesc = getElemDesc(name);
  +            // getElemDesc2(name) is faster than getElemDesc(name)
  +            ElemDesc elemDesc = getElemDesc2(name);
               int elemFlags = elemDesc.getFlags();
   
               // deal with indentation issues first
  
  
  
  1.7       +100 -4    xml-xalan/java/src/org/apache/xml/utils/Trie.java
  
  Index: Trie.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/utils/Trie.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Trie.java	17 Feb 2004 04:21:14 -0000	1.6
  +++ Trie.java	27 Apr 2004 17:57:06 -0000	1.7
  @@ -157,11 +157,10 @@
   //           }
           default :
               {
  -                key.getChars(0, len, m_charBuffer, 0);
  -                // copy string into array
                   for (int i = 0; i < len; i++)
                   {
  -                    final char ch = m_charBuffer[i];
  +                    // A thread-safe way to loop over the characters
  +                    final char ch = key.charAt(i);
                       if (ALPHA_SIZE <= ch)
                       {
                           // the key is not 7-bit ASCII so we won't find it here
  @@ -200,4 +199,101 @@
       /** The value.   */
       Object m_Value;
     }
  +  /**
  +   * Construct the trie from another Trie.
  +   * Both the existing Trie and this new one share the same table for
  +   * lookup, and it is assumed that the table is fully populated and
  +   * not changing anymore.
  +   * 
  +   * @param existingTrie the Trie that this one is a copy of.
  +   */
  +  public Trie(Trie existingTrie) 
  +  {
  +      // use the same table
  +      m_Root = existingTrie.m_Root;
  +      
  +      // get a buffer just big enough to hold the longest key in the table.
  +      int max = existingTrie.getLongestKeyLength();
  +      m_charBuffer = new char[max];      
  +  }  
  +
  +  /**
  +   * Get an object that matches the key.
  +   * This method is faster than get(), but is not thread-safe.
  +   *
  +   * @param key must be a 7-bit ASCII string
  +   *
  +   * @return The object that matches the key, or null.
  +   */
  +  public Object get2(final String key)
  +  {
  +
  +      final int len = key.length();
  +
  +      /* If the name is too long, we won't find it, this also keeps us
  +       * from overflowing m_charBuffer
  +       */
  +      if (m_charBuffer.length < len)
  +          return null;
  +
  +      Node node = m_Root;
  +      switch (len) // optimize the look up based on the number of chars
  +      {
  +          // case 0 looks silly, but the generated bytecode runs
  +          // faster for lookup of elements of length 2 with this in
  +          // and a fair bit faster.  Don't know why.
  +          case 0 :
  +              {
  +                  return null;
  +              }
  +
  +          case 1 :
  +              {
  +                  final char ch = key.charAt(0);
  +                  if (ch < ALPHA_SIZE)
  +                  {
  +                      node = node.m_nextChar[ch];
  +                      if (node != null)
  +                          return node.m_Value;
  +                  }
  +                  return null;
  +              }
  +          default :
  +              {
  +                  /* Copy string into array. This is not thread-safe because
  +                   * it modifies the contents of m_charBuffer. If multiple
  +                   * threads were to use this Trie they all would be
  +                   * using this same array (not good). So this 
  +                   * method is not thread-safe, but it is faster because
  +                   * converting to a char[] and looping over elements of
  +                   * the array is faster than a String's charAt(i).
  +                   */
  +                  key.getChars(0, len, m_charBuffer, 0);
  +
  +                  for (int i = 0; i < len; i++)
  +                  {
  +                      final char ch = m_charBuffer[i];
  +                      if (ALPHA_SIZE <= ch)
  +                      {
  +                          // the key is not 7-bit ASCII so we won't find it here
  +                          return null;
  +                      }
  +
  +                      node = node.m_nextChar[ch];
  +                      if (node == null)
  +                          return null;
  +                  }
  +
  +                  return node.m_Value;
  +              }
  +      }
  +  }
  +
  +  /**
  +   * Get the length of the longest key used in the table. 
  +   */
  +  public int getLongestKeyLength() 
  +  {
  +      return m_charBuffer.length;
  +  }  
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org