From f4aa35d2872e913e453ff4021d55e537e944bce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Tue, 30 Sep 2014 05:16:56 +0800 Subject: [PATCH] Revert "Optimize ThreadLocalCache while perusal the code." --- .../fastjson/util/ThreadLocalCache.java | 56 +++++--------- .../bvt/util/TheradLocalCacheUnitTest.java | 75 ------------------- 2 files changed, 20 insertions(+), 111 deletions(-) delete mode 100644 src/test/java/com/alibaba/json/bvt/util/TheradLocalCacheUnitTest.java diff --git a/src/main/java/com/alibaba/fastjson/util/ThreadLocalCache.java b/src/main/java/com/alibaba/fastjson/util/ThreadLocalCache.java index 19ae61362..8d9a3f447 100755 --- a/src/main/java/com/alibaba/fastjson/util/ThreadLocalCache.java +++ b/src/main/java/com/alibaba/fastjson/util/ThreadLocalCache.java @@ -5,10 +5,8 @@ import java.nio.charset.CharsetDecoder; public class ThreadLocalCache { - public final static int CHARS_CACH_INIT_SIZE = 1024; // 1k, 2^10; - public final static int CHARS_CACH_INIT_SIZE_EXP = 10; - public final static int CHARS_CACH_MAX_SIZE = 1024 * 128; // 128k, 2^17; - public final static int CHARS_CACH_MAX_SIZE_EXP = 17; + public final static int CHARS_CACH_INIT_SIZE = 1024; // 1k; + public final static int CHARS_CACH_MAX_SIZE = 1024 * 128; // 1k; private final static ThreadLocal> charsBufLocal = new ThreadLocal>(); private final static ThreadLocal decoderLocal = new ThreadLocal(); @@ -47,30 +45,17 @@ public class ThreadLocalCache { } private static char[] allocate(int length) { - if(length> CHARS_CACH_MAX_SIZE) { - return new char[length]; - } + int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length); - int allocateLength = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length); - char[] chars = new char[allocateLength]; - charsBufLocal.set(new SoftReference(chars)); - return chars; + if (allocateLength <= CHARS_CACH_MAX_SIZE) { + char[] chars = new char[allocateLength]; + charsBufLocal.set(new SoftReference(chars)); + return chars; + } + + return new char[length]; } - private static int getAllocateLengthExp(int minExp, int maxExp, int length) { - assert maxExp >= length; -// int max = 1 << maxExp; -// if(length>= max) { -// return length; -// } - int part = length >>> minExp; - if(part <= 0) { - return 1<< minExp; - } - return 1 << 32 - Integer.numberOfLeadingZeros(length-1); - } - -@Deprecated private static int getAllocateLength(int init, int max, int length) { int value = init; for (;;) { @@ -89,10 +74,8 @@ public class ThreadLocalCache { } // ///////// - public final static int BYTES_CACH_INIT_SIZE = 1024; // 1k, 2^10; - public final static int BYTES_CACH_INIT_SIZE_EXP = 10; - public final static int BYTES_CACH_MAX_SIZE = 1024 * 128; // 128k, 2^17; - public final static int BYTES_CACH_MAX_SIZE_EXP = 17; + public final static int BYTES_CACH_INIT_SIZE = 1024; // 1k; + public final static int BYTeS_CACH_MAX_SIZE = 1024 * 128; // 1k; private final static ThreadLocal> bytesBufLocal = new ThreadLocal>(); public static void clearBytes() { @@ -120,14 +103,15 @@ public class ThreadLocalCache { } private static byte[] allocateBytes(int length) { - if(length > CHARS_CACH_MAX_SIZE) { - return new byte[length]; - } + int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length); - int allocateLength = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length); - byte[] chars = new byte[allocateLength]; - bytesBufLocal.set(new SoftReference(chars)); - return chars; + if (allocateLength <= CHARS_CACH_MAX_SIZE) { + byte[] chars = new byte[allocateLength]; + bytesBufLocal.set(new SoftReference(chars)); + return chars; + } + + return new byte[length]; } } diff --git a/src/test/java/com/alibaba/json/bvt/util/TheradLocalCacheUnitTest.java b/src/test/java/com/alibaba/json/bvt/util/TheradLocalCacheUnitTest.java deleted file mode 100644 index 1a20438f1..000000000 --- a/src/test/java/com/alibaba/json/bvt/util/TheradLocalCacheUnitTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.alibaba.json.bvt.util; - -import java.lang.reflect.Method; -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.alibaba.fastjson.util.ThreadLocalCache; - -import static com.alibaba.fastjson.util.ThreadLocalCache.CHARS_CACH_INIT_SIZE; -import static com.alibaba.fastjson.util.ThreadLocalCache.CHARS_CACH_INIT_SIZE_EXP; -import static com.alibaba.fastjson.util.ThreadLocalCache.CHARS_CACH_MAX_SIZE; -import static com.alibaba.fastjson.util.ThreadLocalCache.CHARS_CACH_MAX_SIZE_EXP; - -/** - * Test new {@link #getAllocateLengthExp(int, int, int)} comparing with old - * {@link #getAllocateLength(int, int, int)} method in {@link ThreadLocalCache}. - */ -public class TheradLocalCacheUnitTest { - - static Method getAllocateLengthMethod; - static Method getAllocateLengthExpMethod; - - @BeforeClass - public static void init() throws Exception { - getAllocateLengthMethod = ThreadLocalCache.class.getDeclaredMethod("getAllocateLength", - int.class, int.class, int.class); - getAllocateLengthExpMethod = ThreadLocalCache.class.getDeclaredMethod( - "getAllocateLengthExp", int.class, int.class, int.class); - - getAllocateLengthMethod.setAccessible(true); - getAllocateLengthExpMethod.setAccessible(true); - } - - @Test - public void getAllocateLengthNewImplementationRandomTest() throws Exception { - Random random = ThreadLocalRandom.current(); - for (int i = 0; i < 100000000; i++) { - int length = Math.abs(random.nextInt()); - testSample(length); - } - } - - @Test - public void getAllocateLengthNewImplementationCertainTest() throws Exception { - testSample(0); - testSample(1); - testSample(1024); - testSample(CHARS_CACH_INIT_SIZE); - testSample(CHARS_CACH_MAX_SIZE); - testSample(114649); - } - - private void testSample(int length) throws Exception { - if (length > CHARS_CACH_MAX_SIZE) { - return; - } - int oldWay = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length); - int newWay = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length); - // since new method always need premise length <= max value. we need to - // skip this case. - Assert.assertEquals("error when test length:" + length, oldWay, newWay); - } - - private static int getAllocateLength(int init, int max, int length) throws Exception { - return (Integer) getAllocateLengthMethod.invoke(null, init, max, length); - } - - private static int getAllocateLengthExp(int minExp, int maxExp, int length) throws Exception { - return (Integer) getAllocateLengthExpMethod.invoke(null, minExp, maxExp, length); - } -}