You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2019/01/12 11:46:10 UTC

[myfaces] branch master updated: [perf] applied newest changes from groovy and enabled StringCharArrayAccessor per default Otherwise the performance is even more worst as using the default java classes

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b3bc27  [perf] applied newest changes from groovy and enabled StringCharArrayAccessor per default Otherwise the performance is even more worst as using the default java classes
1b3bc27 is described below

commit 1b3bc27b559a03d7991165935d276da13e2067a9
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Sat Jan 12 12:46:32 2019 +0100

    [perf] applied newest changes from groovy and enabled StringCharArrayAccessor per default
    Otherwise the performance is even more worst as using the default java classes
---
 .../org/apache/myfaces/util/StreamCharBuffer.java  |  70 +++++-------
 .../myfaces/util/StringCharArrayAccessor.java      | 118 +++++++++++----------
 2 files changed, 90 insertions(+), 98 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/util/StreamCharBuffer.java b/impl/src/main/java/org/apache/myfaces/util/StreamCharBuffer.java
index 7a61ba5..db12038 100644
--- a/impl/src/main/java/org/apache/myfaces/util/StreamCharBuffer.java
+++ b/impl/src/main/java/org/apache/myfaces/util/StreamCharBuffer.java
@@ -271,8 +271,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
 
     public StreamCharBuffer()
     {
-        this(DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE_GROW_PROCENT,
-                DEFAULT_MAX_CHUNK_SIZE);
+        this(DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE_GROW_PROCENT, DEFAULT_MAX_CHUNK_SIZE);
     }
 
     public StreamCharBuffer(int chunkSize)
@@ -307,8 +306,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         return preferSubChunkWhenWritingToOtherBuffer;
     }
 
-    public void setPreferSubChunkWhenWritingToOtherBuffer(
-            boolean preferSubChunkWhenWritingToOtherBuffer)
+    public void setPreferSubChunkWhenWritingToOtherBuffer(boolean preferSubChunkWhenWritingToOtherBuffer)
     {
         this.preferSubChunkWhenWritingToOtherBuffer = preferSubChunkWhenWritingToOtherBuffer;
     }
@@ -376,8 +374,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
         else
         {
-            connectedWritersWriter = new SingleOutputWriter(
-                    connectedWriters.get(0));
+            connectedWritersWriter = new SingleOutputWriter(connectedWriters.get(0));
         }
     }
 
@@ -446,8 +443,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
      *
      * @param writeDirectlyToConnectedMinSize
      */
-    public void setWriteDirectlyToConnectedMinSize(
-            int writeDirectlyToConnectedMinSize)
+    public void setWriteDirectlyToConnectedMinSize(int writeDirectlyToConnectedMinSize)
     {
         this.writeDirectlyToConnectedMinSize = writeDirectlyToConnectedMinSize;
     }
@@ -517,8 +513,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
      * @param emptyAfter empties the buffer if true
      * @throws IOException
      */
-    public void writeTo(Writer target, boolean flushTarget, boolean emptyAfter)
-            throws IOException
+    public void writeTo(Writer target, boolean flushTarget, boolean emptyAfter) throws IOException
     {
 
         if (target instanceof StreamCharBufferWriter)
@@ -824,8 +819,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         return spaceLeft;
     }
 
-    private int appendCharBufferChunk(boolean flushInConnected)
-            throws IOException
+    private int appendCharBufferChunk(boolean flushInConnected) throws IOException
     {
         int spaceLeft = 0;
         if (flushInConnected && isConnectedMode())
@@ -933,13 +927,11 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
         else if (growProcent > 0)
         {
-            chunkSize = Math.max(Math.min((totalChunkSize * growProcent) / 100,
-                    maxChunkSize), firstChunkSize);
+            chunkSize = Math.max(Math.min((totalChunkSize * growProcent) / 100, maxChunkSize), firstChunkSize);
         }
     }
 
-    protected static final void arrayCopy(char[] src, int srcPos, char[] dest,
-            int destPos, int length)
+    protected static final void arrayCopy(char[] src, int srcPos, char[] dest, int destPos, int length)
     {
         if (length == 1)
         {
@@ -963,8 +955,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         boolean increaseCounter = true;
 
         @Override
-        public final void write(final char[] b, final int off, final int len)
-                throws IOException
+        public final void write(final char[] b, final int off, final int len) throws IOException
         {
             if (b == null)
             {
@@ -1039,8 +1030,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
 
         private final int getNewChunkMinSize()
         {
-            if (chunkMinSize <= 0 || allocBuffer.charsUsed() == 0
-                    || allocBuffer.charsUsed() >= chunkMinSize)
+            if (chunkMinSize <= 0 || allocBuffer.charsUsed() == 0 || allocBuffer.charsUsed() >= chunkMinSize)
             {
                 return 0;
             }
@@ -1054,8 +1044,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
 
         @Override
-        public final void write(final String str, final int off, final int len)
-                throws IOException
+        public final void write(final String str, final int off, final int len) throws IOException
         {
             if (len == 0)
             {
@@ -1097,8 +1086,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
                 subBuffer.writeToImpl(connectedWritersWriter, false, false);
             }
             else if (subBuffer.preferSubChunkWhenWritingToOtherBuffer
-                    || subBuffer.isSizeLarger(Math.max(subBufferChunkMinSize,
-                            getNewChunkMinSize())))
+                    || subBuffer.isSizeLarger(Math.max(subBufferChunkMinSize, getNewChunkMinSize())))
             {
                 if (subBuffer.preferSubChunkWhenWritingToOtherBuffer)
                 {
@@ -2128,8 +2116,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
 
         @Override
-        public Writer append(final CharSequence csq, final int start,
-                final int end) throws IOException
+        public Writer append(final CharSequence csq, final int start, final int end) throws IOException
         {
             writer.getWriter().append(csq, start, end);
             return this;
@@ -2138,8 +2125,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         @Override
         public void write(String str, int off, int len) throws IOException
         {
-            StringCharArrayAccessor.writeStringAsCharArray(writer.getWriter(),
-                    str, off, len);
+            StringCharArrayAccessor.writeStringAsCharArray(writer.getWriter(),str, off, len);
         }
     }
 
@@ -2172,8 +2158,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
 
         @Override
-        public void write(final char[] cbuf, final int off, final int len)
-                throws IOException
+        public void write(final char[] cbuf, final int off, final int len) throws IOException
         {
             for (ConnectedWriter writer : writers)
             {
@@ -2182,8 +2167,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
 
         @Override
-        public Writer append(final CharSequence csq, final int start,
-                final int end) throws IOException
+        public Writer append(final CharSequence csq, final int start, final int end) throws IOException
         {
             for (ConnectedWriter writer : writers)
             {
@@ -2197,24 +2181,26 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         {
             for (ConnectedWriter writer : writers)
             {
-                StringCharArrayAccessor.writeStringAsCharArray(
-                        writer.getWriter(), str, off, len);
+                StringCharArrayAccessor.writeStringAsCharArray(writer.getWriter(), str, off, len);
             }
         }
     }
 
     /* Compatibility methods so that StreamCharBuffer will behave more like java.lang.String in groovy code */
 
+    @Override
     public char charAt(int index)
     {
         return toString().charAt(index);
     }
 
+    @Override
     public int length()
     {
         return size();
     }
 
+    @Override
     public CharSequence subSequence(int start, int end)
     {
         return toString().subSequence(start, end);
@@ -2233,14 +2219,12 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         {
             parentBuffers = new HashSet<SoftReference<StreamCharBufferKey>>();
         }
-        parentBuffers.add(new SoftReference<StreamCharBufferKey>(
-                parent.bufferKey));
+        parentBuffers.add(new SoftReference<StreamCharBufferKey>(parent.bufferKey));
     }
 
     boolean bufferChanged(StreamCharBuffer buffer)
     {
-        StreamCharBufferSubChunk subChunk = dynamicChunkMap
-                .get(buffer.bufferKey);
+        StreamCharBufferSubChunk subChunk = dynamicChunkMap.get(buffer.bufferKey);
         if (subChunk == null)
         {
             // buffer isn't a subchunk in this buffer any more
@@ -2259,13 +2243,12 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
 
     void notifyBufferChange()
     {
-        if (parentBuffers == null)
+        if (parentBuffers == null || parentBuffers.isEmpty())
         {
             return;
         }
 
-        for (Iterator<SoftReference<StreamCharBufferKey>> i = parentBuffers
-                .iterator(); i.hasNext();)
+        for (Iterator<SoftReference<StreamCharBufferKey>> i = parentBuffers.iterator(); i.hasNext();)
         {
             SoftReference<StreamCharBufferKey> ref = i.next();
             final StreamCharBuffer.StreamCharBufferKey parentKey = ref.get();
@@ -2282,8 +2265,8 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
     }
 
-    public void readExternal(ObjectInput in) throws IOException,
-            ClassNotFoundException
+    @Override
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
     {
         String str = in.readUTF();
         reset();
@@ -2293,6 +2276,7 @@ public class StreamCharBuffer implements /*Writable,*/CharSequence,
         }
     }
 
+    @Override
     public void writeExternal(ObjectOutput out) throws IOException
     {
         String str = toString();
diff --git a/impl/src/main/java/org/apache/myfaces/util/StringCharArrayAccessor.java b/impl/src/main/java/org/apache/myfaces/util/StringCharArrayAccessor.java
index 5451cd9..9ef696a 100644
--- a/impl/src/main/java/org/apache/myfaces/util/StringCharArrayAccessor.java
+++ b/impl/src/main/java/org/apache/myfaces/util/StringCharArrayAccessor.java
@@ -25,8 +25,8 @@ import java.lang.reflect.Field;
 /**
  * Provides optimized access to java.lang.String internals
  *
- * - Optimized way of creating java.lang.String by reusing a char[] buffer
- * - Optimized way of writing String to java.io.Writer
+ * - Optimized way of creating java.lang.String by reusing a char[] buffer -
+ * Optimized way of writing String to java.io.Writer
  *
  * java.lang.String creation reusing a char[] buffer requires Java 1.5+
  *
@@ -36,21 +36,16 @@ import java.lang.reflect.Field;
  * Read JSR-133, "9.1.1 Post-Construction Modification of Final Fields"
  * http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf
  *
- * @author Lari Hotari, Sagire Software Oy
- * see org.codehaus.groovy.grails.web.util.StreamCharBuffer
- *      file licensed under ASL v2.0 
- *      Copyright 2009 the original author or authors.
+ * @author Lari Hotari, Sagire Software Oy see
+ * org.codehaus.groovy.grails.web.util.StreamCharBuffer file licensed under ASL
+ * v2.0 Copyright 2009 the original author or authors.
  */
 public class StringCharArrayAccessor
 {
 
-    //static volatile boolean enabled = !Boolean
-    //        .getBoolean("oam.stringchararrayaccessor.disabled");
-    // In Google Application Engine this hack is not valid. We should
-    // set this one as default disabled.
-    static volatile boolean enabled = Boolean
-            .getBoolean("oam.stringchararrayaccessor.enabled");
-
+    static volatile boolean enabled = !Boolean.getBoolean("oam.stringchararrayaccessor.disabled");
+    static volatile boolean jdk7String = false;
+    
     static Field valueField;
     static Field countField;
     static Field offsetField;
@@ -63,61 +58,69 @@ public class StringCharArrayAccessor
             {
                 valueField = String.class.getDeclaredField("value");
                 valueField.setAccessible(true);
-
+            }
+            catch (Exception e)
+            {
+                enabled = false;
+                handleError(e);
+            }
+        }
+        if (enabled)
+        {
+            try
+            {
                 countField = String.class.getDeclaredField("count");
                 countField.setAccessible(true);
 
                 offsetField = String.class.getDeclaredField("offset");
                 offsetField.setAccessible(true);
             }
+            catch (NoSuchFieldException e)
+            {
+                jdk7String = true;
+            }
             catch (Exception e)
             {
                 enabled = false;
-                System.err
-                        .println("Unable to use direct char[] access of java.lang.String");
-                e.printStackTrace();
+                handleError(e);
             }
         }
     }
 
+    private StringCharArrayAccessor()
+    {
+    }
+
     /**
-     * Writes a portion of a string to a target java.io.Writer with direct access to the char[] of the java.lang.String
+     * Writes a portion of a string to a target java.io.Writer with direct
+     * access to the char[] of the java.lang.String
      *
-     * @param  writer
-     *            target java.io.Writer for output
+     * @param writer target java.io.Writer for output
      *
-     * @param  str
-     *         A String
+     * @param str A String
      *
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException If an I/O error occurs
      */
-    static public void writeStringAsCharArray(Writer writer, String str)
-            throws IOException
+    static public void writeStringAsCharArray(Writer writer, String str) throws IOException
     {
         writeStringAsCharArray(writer, str, 0, str.length());
     }
 
     /**
-     * Writes a portion of a string to a target java.io.Writer with direct access to the char[] of the java.lang.String
+     * Writes a portion of a string to a target java.io.Writer with direct
+     * access to the char[] of the java.lang.String
      *
-     * @param  writer
-     *            target java.io.Writer for output
+     * @param writer target java.io.Writer for output
      *
-     * @param  str
-     *         A String
+     * @param str A String
      *
-     * @param  off
-     *         Offset from which to start writing characters
+     * @param off Offset from which to start writing characters
      *
-     * @param  len
-     *         Number of characters to write
+     * @param len Number of characters to write
      *
-     * @throws  IOException
-     *          If an I/O error occurs
+     * @throws IOException If an I/O error occurs
      */
-    static public void writeStringAsCharArray(Writer writer, String str,
-            int off, int len) throws IOException
+    static public void writeStringAsCharArray(Writer writer, String str, int off, int len) throws IOException
     {
         if (!enabled)
         {
@@ -126,11 +129,14 @@ public class StringCharArrayAccessor
         }
 
         char[] value;
-        int internalOffset;
+        int internalOffset = 0;
         try
         {
             value = (char[]) valueField.get(str);
-            internalOffset = offsetField.getInt(str);
+            if (!jdk7String)
+            {
+                internalOffset = offsetField.getInt(str);
+            }
         }
         catch (Exception e)
         {
@@ -141,8 +147,7 @@ public class StringCharArrayAccessor
         writer.write(value, internalOffset + off, len);
     }
 
-    private static void writeStringFallback(Writer writer, String str, int off,
-            int len) throws IOException
+    private static void writeStringFallback(Writer writer, String str, int off, int len) throws IOException
     {
         writer.write(str, off, len);
     }
@@ -159,7 +164,10 @@ public class StringCharArrayAccessor
         try
         {
             value = (char[]) valueField.get(str);
-            internalOffset = offsetField.getInt(str);
+            if (!jdk7String)
+            {
+                internalOffset = offsetField.getInt(str);
+            }
         }
         catch (Exception e)
         {
@@ -179,10 +187,11 @@ public class StringCharArrayAccessor
     }
 
     /**
-     * creates a new java.lang.String by setting the char array directly to the String instance with reflection.
+     * creates a new java.lang.String by setting the char array directly to the
+     * String instance with reflection.
      *
-     * @param charBuf
-     *        char array to be used as java.lang.String content, don't modify it after passing it.
+     * @param charBuf char array to be used as java.lang.String content, don't
+     * modify it after passing it.
      * @return new java.lang.String
      */
     public static String createString(char[] charBuf)
@@ -192,24 +201,26 @@ public class StringCharArrayAccessor
             return createStringFallback(charBuf);
         }
 
-        String str = "";
+        String str = new String();
         try
         {
-            // try to prevent possible final field setting execution reordering in JIT 
+            // try to prevent possible final field setting execution reordering in JIT
             // (JSR-133/JMM, "9.1.1 Post-Construction Modification of Final Fields")
             // it was a bit unclear for me if this could ever happen in a single thread
             synchronized (str)
             {
                 valueField.set(str, charBuf);
-                countField.set(str, charBuf.length);
+                if (!jdk7String)
+                {
+                    countField.set(str, charBuf.length);
+                }
             }
             synchronized (str)
             {
                 // safety check, just to be sure that setting the final fields went ok
                 if (str.length() != charBuf.length)
                 {
-                    throw new IllegalStateException(
-                            "Fast java.lang.String construction failed.");
+                    throw new IllegalStateException("Fast java.lang.String construction failed.");
                 }
             }
         }
@@ -229,12 +240,9 @@ public class StringCharArrayAccessor
     private static synchronized void handleError(Exception e)
     {
         enabled = false;
-        System.err
-                .println("Unable to use direct char[] access of java.lang.String. Disabling this method.");
         valueField = null;
         countField = null;
         offsetField = null;
-        e.printStackTrace();
     }
 
     static public boolean isEnabled()