/
JsonRecyclerPools.java
275 lines (227 loc) · 8.89 KB
/
JsonRecyclerPools.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.fasterxml.jackson.core.util;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedDeque;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.util.RecyclerPool.BoundedPoolBase;
import com.fasterxml.jackson.core.util.RecyclerPool.ConcurrentDequePoolBase;
import com.fasterxml.jackson.core.util.RecyclerPool.LockFreePoolBase;
/**
* Set of {@link RecyclerPool} implementations to be used by the default
* JSON-backed {@link JsonFactory} for recycling {@link BufferRecycler}
* containers.
*
* @since 2.16
*/
public final class JsonRecyclerPools
{
/**
* Method to call to get the default recycler pool instance:
* as of Jackson 2.18.x and later (except for 2.17.0) this is same as calling
* {@link #newConcurrentDequePool()}: before this it was calling
* {@link #threadLocalPool()} (except for 2.17.1 that temporarily
* called {@link #newLockFreePool()}, changed back due to reported issues).
*
* @return the default {@link RecyclerPool} implementation to use
* if no specific implementation desired.
*/
public static RecyclerPool<BufferRecycler> defaultPool() {
// since 2.18.0:
return newConcurrentDequePool();
}
/**
* Accessor for getting the shared/global {@link ThreadLocalPool} instance
* (due to design only one instance ever needed)
*
* @return Globally shared instance of {@link ThreadLocalPool}
*/
public static RecyclerPool<BufferRecycler> threadLocalPool() {
return ThreadLocalPool.GLOBAL;
}
/**
* Accessor for getting the shared/global {@link NonRecyclingPool} instance
* (due to design only one instance ever needed)
*
* @return Globally shared instance of {@link NonRecyclingPool}.
*/
public static RecyclerPool<BufferRecycler> nonRecyclingPool() {
return NonRecyclingPool.GLOBAL;
}
/**
* Accessor for getting the shared/global {@link ConcurrentDequePool} instance.
*
* @return Globally shared instance of {@link NonRecyclingPool}.
*/
public static RecyclerPool<BufferRecycler> sharedConcurrentDequePool() {
return ConcurrentDequePool.GLOBAL;
}
/**
* Accessor for constructing a new, non-shared {@link ConcurrentDequePool} instance.
*
* @return Globally shared instance of {@link NonRecyclingPool}.
*/
public static RecyclerPool<BufferRecycler> newConcurrentDequePool() {
return ConcurrentDequePool.construct();
}
/**
* Accessor for getting the shared/global {@link LockFreePool} instance.
*
* @return Globally shared instance of {@link LockFreePool}.
*/
public static RecyclerPool<BufferRecycler> sharedLockFreePool() {
return LockFreePool.GLOBAL;
}
/**
* Accessor for constructing a new, non-shared {@link LockFreePool} instance.
*
* @return Globally shared instance of {@link LockFreePool}.
*/
public static RecyclerPool<BufferRecycler> newLockFreePool() {
return LockFreePool.construct();
}
/**
* Accessor for getting the shared/global {@link BoundedPool} instance.
*
* @return Globally shared instance of {@link BoundedPool}.
*/
public static RecyclerPool<BufferRecycler> sharedBoundedPool() {
return BoundedPool.GLOBAL;
}
/**
* Accessor for constructing a new, non-shared {@link BoundedPool} instance.
*
* @param size Maximum number of values to pool
*
* @return Globally shared instance of {@link BoundedPool}.
*/
public static RecyclerPool<BufferRecycler> newBoundedPool(int size) {
return BoundedPool.construct(size);
}
/*
/**********************************************************************
/* Concrete RecyclerPool implementations for recycling BufferRecyclers
/**********************************************************************
*/
/**
* {@link ThreadLocal}-based {@link RecyclerPool} implementation used for
* recycling {@link BufferRecycler} instances:
* see {@link RecyclerPool.ThreadLocalPoolBase} for full explanation
* of functioning.
*/
public static class ThreadLocalPool
extends RecyclerPool.ThreadLocalPoolBase<BufferRecycler>
{
private static final long serialVersionUID = 1L;
protected static final ThreadLocalPool GLOBAL = new ThreadLocalPool();
private ThreadLocalPool() { }
@SuppressWarnings("deprecation")
@Override
public BufferRecycler acquirePooled() {
return BufferRecyclers.getBufferRecycler();
}
// // // JDK serialization support
protected Object readResolve() { return GLOBAL; }
}
/**
* Dummy {@link RecyclerPool} implementation that does not recycle
* anything but simply creates new instances when asked to acquire items.
*/
public static class NonRecyclingPool
extends RecyclerPool.NonRecyclingPoolBase<BufferRecycler>
{
private static final long serialVersionUID = 1L;
protected static final NonRecyclingPool GLOBAL = new NonRecyclingPool();
protected NonRecyclingPool() { }
@Override
public BufferRecycler acquirePooled() {
return new BufferRecycler();
}
// // // JDK serialization support
protected Object readResolve() { return GLOBAL; }
}
/**
* {@link RecyclerPool} implementation that uses
* {@link ConcurrentLinkedDeque} for recycling instances.
*<p>
* Pool is unbounded: see {@link RecyclerPool} what this means.
*/
public static class ConcurrentDequePool extends ConcurrentDequePoolBase<BufferRecycler>
{
private static final long serialVersionUID = 1L;
protected static final ConcurrentDequePool GLOBAL = new ConcurrentDequePool(SERIALIZATION_SHARED);
// // // Life-cycle (constructors, factory methods)
protected ConcurrentDequePool(int serialization) {
super(serialization);
}
public static ConcurrentDequePool construct() {
return new ConcurrentDequePool(SERIALIZATION_NON_SHARED);
}
@Override
public BufferRecycler createPooled() {
return new BufferRecycler();
}
// // // JDK serialization support
// Make sure to re-link to global/shared or non-shared.
protected Object readResolve() {
return _resolveToShared(GLOBAL).orElseGet(() -> construct());
}
}
/**
* {@link RecyclerPool} implementation that uses
* a lock free linked list for recycling instances.
*<p>
* Pool is unbounded: see {@link RecyclerPool} for
* details on what this means.
*/
public static class LockFreePool extends LockFreePoolBase<BufferRecycler>
{
private static final long serialVersionUID = 1L;
protected static final LockFreePool GLOBAL = new LockFreePool(SERIALIZATION_SHARED);
// // // Life-cycle (constructors, factory methods)
protected LockFreePool(int serialization) {
super(serialization);
}
public static LockFreePool construct() {
return new LockFreePool(SERIALIZATION_NON_SHARED);
}
@Override
public BufferRecycler createPooled() {
return new BufferRecycler();
}
// // // JDK serialization support
// Make sure to re-link to global/shared or non-shared.
protected Object readResolve() {
return _resolveToShared(GLOBAL).orElseGet(() -> construct());
}
}
/**
* {@link RecyclerPool} implementation that uses
* a bounded queue ({@link ArrayBlockingQueue} for recycling instances.
* This is "bounded" pool since it will never hold on to more
* {@link BufferRecycler} instances than its size configuration:
* the default size is {@link BoundedPoolBase#DEFAULT_CAPACITY}.
*/
public static class BoundedPool extends BoundedPoolBase<BufferRecycler>
{
private static final long serialVersionUID = 1L;
protected static final BoundedPool GLOBAL = new BoundedPool(SERIALIZATION_SHARED);
// // // Life-cycle (constructors, factory methods)
protected BoundedPool(int capacityAsId) {
super(capacityAsId);
}
public static BoundedPool construct(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("capacity must be > 0, was: "+capacity);
}
return new BoundedPool(capacity);
}
@Override
public BufferRecycler createPooled() {
return new BufferRecycler();
}
// // // JDK serialization support
// Make sure to re-link to global/shared or non-shared.
protected Object readResolve() {
return _resolveToShared(GLOBAL).orElseGet(() -> construct(_serialization));
}
}
}