5
5
6
6
public class ThreadLocalCache {
7
7
8
- public final static int CHARS_CACH_INIT_SIZE = 1024 ; // 1k;
9
- public final static int CHARS_CACH_MAX_SIZE = 1024 * 128 ; // 1k;
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 ;
10
12
private final static ThreadLocal <SoftReference <char []>> charsBufLocal = new ThreadLocal <SoftReference <char []>>();
11
13
12
14
private final static ThreadLocal <CharsetDecoder > decoderLocal = new ThreadLocal <CharsetDecoder >();
@@ -45,17 +47,30 @@ public static char[] getChars(int length) {
45
47
}
46
48
47
49
private static char [] allocate (int length ) {
48
- int allocateLength = getAllocateLength (CHARS_CACH_INIT_SIZE , CHARS_CACH_MAX_SIZE , length );
50
+ if (length > CHARS_CACH_MAX_SIZE ) {
51
+ return new char [length ];
52
+ }
49
53
50
- if (allocateLength <= CHARS_CACH_MAX_SIZE ) {
51
- char [] chars = new char [allocateLength ];
52
- charsBufLocal .set (new SoftReference <char []>(chars ));
53
- return chars ;
54
- }
55
-
56
- return new char [length ];
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 ;
57
58
}
58
59
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 );
71
+ }
72
+
73
+ @ Deprecated
59
74
private static int getAllocateLength (int init , int max , int length ) {
60
75
int value = init ;
61
76
for (;;) {
@@ -74,8 +89,10 @@ private static int getAllocateLength(int init, int max, int length) {
74
89
}
75
90
76
91
// /////////
77
- public final static int BYTES_CACH_INIT_SIZE = 1024 ; // 1k;
78
- public final static int BYTeS_CACH_MAX_SIZE = 1024 * 128 ; // 1k;
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 ;
79
96
private final static ThreadLocal <SoftReference <byte []>> bytesBufLocal = new ThreadLocal <SoftReference <byte []>>();
80
97
81
98
public static void clearBytes () {
@@ -103,15 +120,14 @@ public static byte[] getBytes(int length) {
103
120
}
104
121
105
122
private static byte [] allocateBytes (int length ) {
106
- int allocateLength = getAllocateLength (CHARS_CACH_INIT_SIZE , CHARS_CACH_MAX_SIZE , length );
123
+ if (length > CHARS_CACH_MAX_SIZE ) {
124
+ return new byte [length ];
125
+ }
107
126
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 ];
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 ;
115
131
}
116
132
117
133
}
0 commit comments