Skip to content

Commit 33e4382

Browse files
committed
Revert "Optimize ThreadLocalCache while perusal the code."
1 parent 6013161 commit 33e4382

File tree

2 files changed

+20
-111
lines changed

2 files changed

+20
-111
lines changed

src/main/java/com/alibaba/fastjson/util/ThreadLocalCache.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
public class ThreadLocalCache {
77

8-
public final static int CHARS_CACH_INIT_SIZE = 1024; // 1k, 2^10;
9-
public final static int CHARS_CACH_INIT_SIZE_EXP = 10;
10-
public final static int CHARS_CACH_MAX_SIZE = 1024 * 128; // 128k, 2^17;
11-
public final static int CHARS_CACH_MAX_SIZE_EXP = 17;
8+
public final static int CHARS_CACH_INIT_SIZE = 1024; // 1k;
9+
public final static int CHARS_CACH_MAX_SIZE = 1024 * 128; // 1k;
1210
private final static ThreadLocal<SoftReference<char[]>> charsBufLocal = new ThreadLocal<SoftReference<char[]>>();
1311

1412
private final static ThreadLocal<CharsetDecoder> decoderLocal = new ThreadLocal<CharsetDecoder>();
@@ -47,30 +45,17 @@ public static char[] getChars(int length) {
4745
}
4846

4947
private static char[] allocate(int length) {
50-
if(length> CHARS_CACH_MAX_SIZE) {
51-
return new char[length];
52-
}
48+
int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length);
5349

54-
int allocateLength = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length);
55-
char[] chars = new char[allocateLength];
56-
charsBufLocal.set(new SoftReference<char[]>(chars));
57-
return chars;
58-
}
50+
if (allocateLength <= CHARS_CACH_MAX_SIZE) {
51+
char[] chars = new char[allocateLength];
52+
charsBufLocal.set(new SoftReference<char[]>(chars));
53+
return chars;
54+
}
5955

60-
private static int getAllocateLengthExp(int minExp, int maxExp, int length) {
61-
assert maxExp >= length;
62-
// int max = 1 << maxExp;
63-
// if(length>= max) {
64-
// return length;
65-
// }
66-
int part = length >>> minExp;
67-
if(part <= 0) {
68-
return 1<< minExp;
69-
}
70-
return 1 << 32 - Integer.numberOfLeadingZeros(length-1);
56+
return new char[length];
7157
}
72-
73-
@Deprecated
58+
7459
private static int getAllocateLength(int init, int max, int length) {
7560
int value = init;
7661
for (;;) {
@@ -89,10 +74,8 @@ private static int getAllocateLength(int init, int max, int length) {
8974
}
9075

9176
// /////////
92-
public final static int BYTES_CACH_INIT_SIZE = 1024; // 1k, 2^10;
93-
public final static int BYTES_CACH_INIT_SIZE_EXP = 10;
94-
public final static int BYTES_CACH_MAX_SIZE = 1024 * 128; // 128k, 2^17;
95-
public final static int BYTES_CACH_MAX_SIZE_EXP = 17;
77+
public final static int BYTES_CACH_INIT_SIZE = 1024; // 1k;
78+
public final static int BYTeS_CACH_MAX_SIZE = 1024 * 128; // 1k;
9679
private final static ThreadLocal<SoftReference<byte[]>> bytesBufLocal = new ThreadLocal<SoftReference<byte[]>>();
9780

9881
public static void clearBytes() {
@@ -120,14 +103,15 @@ public static byte[] getBytes(int length) {
120103
}
121104

122105
private static byte[] allocateBytes(int length) {
123-
if(length > CHARS_CACH_MAX_SIZE) {
124-
return new byte[length];
125-
}
106+
int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length);
126107

127-
int allocateLength = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length);
128-
byte[] chars = new byte[allocateLength];
129-
bytesBufLocal.set(new SoftReference<byte[]>(chars));
130-
return chars;
108+
if (allocateLength <= CHARS_CACH_MAX_SIZE) {
109+
byte[] chars = new byte[allocateLength];
110+
bytesBufLocal.set(new SoftReference<byte[]>(chars));
111+
return chars;
112+
}
113+
114+
return new byte[length];
131115
}
132116

133117
}

src/test/java/com/alibaba/json/bvt/util/TheradLocalCacheUnitTest.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)