Repository: logging-log4j2
Updated Branches:
  refs/heads/master 202f0f73e -> 3c2e063c7


LOG4J2-1278     added support for reusable boxing (to prevent auto-boxing), 
with unit tests


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3c2e063c
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3c2e063c
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3c2e063c

Branch: refs/heads/master
Commit: 3c2e063c797ae5452c7745f80818b35102828056
Parents: 202f0f7
Author: rpopma <[email protected]>
Authored: Fri Apr 1 00:16:16 2016 +0900
Committer: rpopma <[email protected]>
Committed: Fri Apr 1 00:16:16 2016 +0900

----------------------------------------------------------------------
 .../org/apache/logging/log4j/util/Unbox.java    | 178 +++++++++++++++++++
 .../apache/logging/log4j/util/UnboxTest.java    | 155 ++++++++++++++++
 2 files changed, 333 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c2e063c/log4j-api/src/main/java/org/apache/logging/log4j/util/Unbox.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Unbox.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/Unbox.java
new file mode 100644
index 0000000..6981fb8
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Unbox.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.util;
+
+import static org.apache.logging.log4j.util.Unbox.box;
+
+/**
+ * Utility for preventing primitive parameter values from being auto-boxed. 
Auto-boxing creates temporary objects
+ * which contribute to pressure on the garbage collector. With this utility 
users can convert primitive values directly
+ * into text without allocating temporary objects.
+ * <p>
+ * Example usage:
+ * </p><pre>
+ * import static org.apache.logging.log4j.util.Unbox.box;
+ * ...
+ * long longValue = 123456L;
+ * double doubleValue = 3.14;
+ * // prevent primitive values from being auto-boxed
+ * logger.debug("Long value={}, double value={}", box(longValue), 
box(doubleValue));
+ * </pre>
+ */
+@PerformanceSensitive("allocation")
+public class Unbox {
+    private static final int MASK = 16 - 1;
+
+    private static class State {
+        private final StringBuilder[] ringbuffer = new StringBuilder[16];
+        private int current;
+        State() {
+            for (int i = 0; i < ringbuffer.length; i++) {
+                ringbuffer[i] = new StringBuilder(21);
+            }
+        }
+
+        public StringBuilder getStringBuilder() {
+            final StringBuilder result = ringbuffer[MASK & current++];
+            result.setLength(0);
+            return result;
+        }
+
+        public boolean isBoxedPrimitive(final StringBuilder text) {
+            for (int i = 0; i < ringbuffer.length; i++) {
+                if (text == ringbuffer[i]) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+    private static ThreadLocal<State> threadLocalState = new ThreadLocal<>();
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(float value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(double value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(short value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(int value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(char value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(long value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(byte value) {
+        return getSB().append(value);
+    }
+
+    /**
+     * Returns a {@code StringBuilder} containing the text representation of 
the specified primitive value.
+     * This method will not allocate temporary objects.
+     *
+     * @param value the value whose text representation to return
+     * @return a {@code StringBuilder} containing the text representation of 
the specified primitive value
+     */
+    @PerformanceSensitive("allocation")
+    public static StringBuilder box(boolean value) {
+        return getSB().append(value);
+    }
+
+    public static boolean isBoxedPrimitive(final StringBuilder text) {
+        return getState().isBoxedPrimitive(text);
+    }
+
+    private static State getState() {
+        State state = threadLocalState.get();
+        if (state == null) {
+            state = new State();
+            threadLocalState.set(state);
+        }
+        return state;
+    }
+
+    private static StringBuilder getSB() {
+        return getState().getStringBuilder();
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c2e063c/log4j-api/src/test/java/org/apache/logging/log4j/util/UnboxTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/test/java/org/apache/logging/log4j/util/UnboxTest.java 
b/log4j-api/src/test/java/org/apache/logging/log4j/util/UnboxTest.java
new file mode 100644
index 0000000..966fa6a
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/UnboxTest.java
@@ -0,0 +1,155 @@
+package org.apache.logging.log4j.util;/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests the Unbox class.
+ */
+public class UnboxTest {
+
+    @Test
+    public void testBoxHas16Slots() throws Exception {
+        StringBuilder[] probe = new StringBuilder[16 * 3];
+        for (int i = 0; i <= probe.length - 8; ) {
+            probe[i++] = Unbox.box(true);
+            probe[i++] = Unbox.box('c');
+            probe[i++] = Unbox.box(Byte.MAX_VALUE);
+            probe[i++] = Unbox.box(Double.MAX_VALUE);
+            probe[i++] = Unbox.box(Float.MAX_VALUE);
+            probe[i++] = Unbox.box(Integer.MAX_VALUE);
+            probe[i++] = Unbox.box(Long.MAX_VALUE);
+            probe[i++] = Unbox.box(Short.MAX_VALUE);
+        }
+        for (int i = 0; i < probe.length - 16; i++) {
+            assertSame("probe[" + i +"], probe[" + (i + 16) +"]", probe[i], 
probe[i + 16]);
+            for (int j = 1; j < 15; j++) {
+                assertNotSame("probe[" + i +"], probe[" + (i + j) +"]", 
probe[i], probe[i + j]);
+            }
+        }
+    }
+
+    @Test
+    public void testBoxBoolean() throws Exception {
+        assertEquals("true", Unbox.box(true).toString());
+        assertEquals("false", Unbox.box(false).toString());
+    }
+
+    @Test
+    public void testBoxByte() throws Exception {
+        assertEquals("0", Unbox.box((byte) 0).toString());
+        assertEquals("1", Unbox.box((byte) 1).toString());
+        assertEquals("127", Unbox.box((byte) 127).toString());
+        assertEquals("-1", Unbox.box((byte) -1).toString());
+        assertEquals("-128", Unbox.box((byte) -128).toString());
+    }
+
+    @Test
+    public void testBoxChar() throws Exception {
+        assertEquals("a", Unbox.box('a').toString());
+        assertEquals("b", Unbox.box('b').toString());
+        assertEquals("字", Unbox.box('字').toString());
+    }
+
+    @Test
+    public void testBoxDouble() throws Exception {
+        assertEquals("3.14", Unbox.box(3.14).toString());
+        assertEquals(new Double(Double.MAX_VALUE).toString(), 
Unbox.box(Double.MAX_VALUE).toString());
+        assertEquals(new Double(Double.MIN_VALUE).toString(), 
Unbox.box(Double.MIN_VALUE).toString());
+    }
+
+    @Test
+    public void testBoxFloat() throws Exception {
+        assertEquals("3.14", Unbox.box(3.14F).toString());
+        assertEquals(new Float(Float.MAX_VALUE).toString(), 
Unbox.box(Float.MAX_VALUE).toString());
+        assertEquals(new Float(Float.MIN_VALUE).toString(), 
Unbox.box(Float.MIN_VALUE).toString());
+    }
+
+    @Test
+    public void testBoxInt() throws Exception {
+        assertEquals("0", Unbox.box(0).toString());
+        assertEquals("1", Unbox.box(1).toString());
+        assertEquals("127", Unbox.box(127).toString());
+        assertEquals("-1", Unbox.box(-1).toString());
+        assertEquals("-128", Unbox.box(-128).toString());
+        assertEquals(new Integer(Integer.MAX_VALUE).toString(), 
Unbox.box(Integer.MAX_VALUE).toString());
+        assertEquals(new Integer(Integer.MIN_VALUE).toString(), 
Unbox.box(Integer.MIN_VALUE).toString());
+    }
+
+    @Test
+    public void testBoxLong() throws Exception {
+        assertEquals("0", Unbox.box(0L).toString());
+        assertEquals("1", Unbox.box(1L).toString());
+        assertEquals("127", Unbox.box(127L).toString());
+        assertEquals("-1", Unbox.box(-1L).toString());
+        assertEquals("-128", Unbox.box(-128L).toString());
+        assertEquals(new Long(Long.MAX_VALUE).toString(), 
Unbox.box(Long.MAX_VALUE).toString());
+        assertEquals(new Long(Long.MIN_VALUE).toString(), 
Unbox.box(Long.MIN_VALUE).toString());
+    }
+
+    @Test
+    public void testBoxShort() throws Exception {
+        assertEquals("0", Unbox.box((short) 0).toString());
+        assertEquals("1", Unbox.box((short) 1).toString());
+        assertEquals("127", Unbox.box((short) 127).toString());
+        assertEquals("-1", Unbox.box((short) -1).toString());
+        assertEquals("-128", Unbox.box((short) -128).toString());
+        assertEquals(new Short(Short.MAX_VALUE).toString(), 
Unbox.box(Short.MAX_VALUE).toString());
+        assertEquals(new Short(Short.MIN_VALUE).toString(), 
Unbox.box(Short.MIN_VALUE).toString());
+    }
+
+    @Test
+    public void testBoxIsThreadLocal() throws Exception {
+        final StringBuilder[] probe = new StringBuilder[16 * 3];
+        populate(0, probe);
+        Thread t1 = new Thread() {
+            public void run() {
+                populate(16, probe);
+            }
+        };
+        t1.start();
+        t1.join();
+        Thread t2 = new Thread() {
+            public void run() {
+                populate(16, probe);
+            }
+        };
+        t2.start();
+        t2.join();
+        for (int i = 0; i < probe.length - 16; i++) {
+            for (int j = 1; j < 16; j++) {
+                assertNotSame("probe[" + i +"]=" + probe[i] + ", probe[" + (i 
+ j) +"]=" + probe[i + j],
+                        probe[i], probe[i + j]);
+            }
+        }
+    }
+
+    private void populate(final int start, final StringBuilder[] probe) {
+        for (int i = start; i <= start + 8; ) {
+            probe[i++] = Unbox.box(true);
+            probe[i++] = Unbox.box('c');
+            probe[i++] = Unbox.box(Byte.MAX_VALUE);
+            probe[i++] = Unbox.box(Double.MAX_VALUE);
+            probe[i++] = Unbox.box(Float.MAX_VALUE);
+            probe[i++] = Unbox.box(Integer.MAX_VALUE);
+            probe[i++] = Unbox.box(Long.MAX_VALUE);
+            probe[i++] = Unbox.box(Short.MAX_VALUE);
+        }
+    }
+}
\ No newline at end of file

Reply via email to