Skip to content

Commit eca76dd

Browse files
authored
Changes to initial BufferRecyclerProvider PR (#1085)
1 parent 4dfdc96 commit eca76dd

File tree

5 files changed

+86
-41
lines changed

5 files changed

+86
-41
lines changed

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
2121
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
2222
import com.fasterxml.jackson.core.util.BufferRecycler;
23-
import com.fasterxml.jackson.core.util.BufferRecyclerProvider;
23+
import com.fasterxml.jackson.core.util.BufferRecyclerPool;
2424
import com.fasterxml.jackson.core.util.BufferRecyclers;
2525
import com.fasterxml.jackson.core.util.JacksonFeature;
2626
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;
@@ -264,7 +264,7 @@ public static int collectDefaults() {
264264
/**
265265
* @since 2.16
266266
*/
267-
protected BufferRecyclerProvider _bufferRecyclerProvider;
267+
protected BufferRecyclerPool _bufferRecyclerPool;
268268

269269
/**
270270
* Object that implements conversion functionality between
@@ -370,7 +370,7 @@ public static int collectDefaults() {
370370
public JsonFactory() { this((ObjectCodec) null); }
371371

372372
public JsonFactory(ObjectCodec oc) {
373-
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
373+
_bufferRecyclerPool = BufferRecyclers.defaultRecyclerPool();
374374
_objectCodec = oc;
375375
_quoteChar = DEFAULT_QUOTE_CHAR;
376376
_streamReadConstraints = StreamReadConstraints.defaults();
@@ -389,7 +389,7 @@ public JsonFactory(ObjectCodec oc) {
389389
*/
390390
protected JsonFactory(JsonFactory src, ObjectCodec codec)
391391
{
392-
_bufferRecyclerProvider = src._bufferRecyclerProvider;
392+
_bufferRecyclerPool = src._bufferRecyclerPool;
393393
_objectCodec = codec;
394394

395395
// General
@@ -418,7 +418,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
418418
* @since 2.10
419419
*/
420420
public JsonFactory(JsonFactoryBuilder b) {
421-
_bufferRecyclerProvider = b._bufferRecyclerProvider;
421+
_bufferRecyclerPool = b._bufferRecyclerPool;
422422
_objectCodec = null;
423423

424424
// General
@@ -448,7 +448,7 @@ public JsonFactory(JsonFactoryBuilder b) {
448448
* @param bogus Argument only needed to separate constructor signature; ignored
449449
*/
450450
protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
451-
_bufferRecyclerProvider = b._bufferRecyclerProvider;
451+
_bufferRecyclerPool = b._bufferRecyclerPool;
452452
_objectCodec = null;
453453

454454
_factoryFeatures = b._factoryFeatures;
@@ -1141,8 +1141,8 @@ public String getRootValueSeparator() {
11411141
/**********************************************************
11421142
*/
11431143

1144-
public JsonFactory setBufferRecyclerProvider(BufferRecyclerProvider p) {
1145-
_bufferRecyclerProvider = Objects.requireNonNull(p);
1144+
public JsonFactory setBufferRecyclerPool(BufferRecyclerPool p) {
1145+
_bufferRecyclerPool = Objects.requireNonNull(p);
11461146
return this;
11471147
}
11481148

@@ -2138,13 +2138,23 @@ protected JsonGenerator _decorate(JsonGenerator g) {
21382138
*/
21392139
public BufferRecycler _getBufferRecycler()
21402140
{
2141+
return _getBufferRecyclerPool().acquireBufferRecycler(this);
2142+
}
2143+
2144+
/**
2145+
* Accessor for getting access to {@link BufferRecyclerPool} for getting
2146+
* {@link BufferRecycler} instance to use.
2147+
*
2148+
* @since 2.16
2149+
*/
2150+
public BufferRecyclerPool _getBufferRecyclerPool() {
21412151
// 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
21422152
// scheme, for cases where it is considered harmful (possibly
21432153
// on Android, for example)
21442154
if (!Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
2145-
return new BufferRecycler();
2155+
return BufferRecyclers.nopRecyclerPool();
21462156
}
2147-
return _bufferRecyclerProvider.acquireBufferRecycler(this);
2157+
return _bufferRecyclerPool;
21482158
}
21492159

21502160
/**

src/main/java/com/fasterxml/jackson/core/TSFBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.fasterxml.jackson.core.io.OutputDecorator;
99
import com.fasterxml.jackson.core.json.JsonReadFeature;
1010
import com.fasterxml.jackson.core.json.JsonWriteFeature;
11-
import com.fasterxml.jackson.core.util.BufferRecyclerProvider;
11+
import com.fasterxml.jackson.core.util.BufferRecyclerPool;
1212
import com.fasterxml.jackson.core.util.BufferRecyclers;
1313
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;
1414

@@ -75,7 +75,7 @@ public abstract class TSFBuilder<F extends JsonFactory,
7575
/**
7676
* @since 2.16
7777
*/
78-
protected BufferRecyclerProvider _bufferRecyclerProvider;
78+
protected BufferRecyclerPool _bufferRecyclerPool;
7979

8080
/**
8181
* Optional helper object that may decorate input sources, to do
@@ -142,7 +142,7 @@ protected TSFBuilder(JsonFactory base)
142142
protected TSFBuilder(int factoryFeatures,
143143
int parserFeatures, int generatorFeatures)
144144
{
145-
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
145+
_bufferRecyclerPool = BufferRecyclers.defaultRecyclerPool();
146146

147147
_factoryFeatures = factoryFeatures;
148148
_streamReadFeatures = parserFeatures;
@@ -170,8 +170,8 @@ protected static <T> List<T> _copy(List<T> src) {
170170
public int streamReadFeatures() { return _streamReadFeatures; }
171171
public int streamWriteFeatures() { return _streamWriteFeatures; }
172172

173-
public BufferRecyclerProvider bufferRecyclerProvider() {
174-
return _bufferRecyclerProvider;
173+
public BufferRecyclerPool bufferRecyclerPool() {
174+
return _bufferRecyclerPool;
175175
}
176176

177177
public InputDecorator inputDecorator() { return _inputDecorator; }
@@ -316,14 +316,14 @@ public B configure(JsonWriteFeature f, boolean state) {
316316
// // // Other configuration, helper objects
317317

318318
/**
319-
* @param p BufferRecyclerProvider to use for buffer allocation
319+
* @param p BufferRecyclerPool to use for buffer allocation
320320
*
321321
* @return this builder (for call chaining)
322322
*
323323
* @since 2.16
324324
*/
325-
public B bufferRecyclerProvider(BufferRecyclerProvider p) {
326-
_bufferRecyclerProvider = Objects.requireNonNull(p);
325+
public B bufferRecyclerPool(BufferRecyclerPool p) {
326+
_bufferRecyclerPool = Objects.requireNonNull(p);
327327
return _this();
328328
}
329329

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fasterxml.jackson.core.util;
2+
3+
import com.fasterxml.jackson.core.TokenStreamFactory;
4+
5+
/**
6+
* Interface for entity that controls creation and possible reuse of {@link BufferRecycler}
7+
* instances used for recycling of underlying input/output buffers.
8+
*
9+
* @since 2.16
10+
*/
11+
public interface BufferRecyclerPool
12+
extends java.io.Serializable
13+
{
14+
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory);
15+
16+
public void releaseBufferRecycler(BufferRecycler recycler);
17+
}

src/main/java/com/fasterxml/jackson/core/util/BufferRecyclerProvider.java

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

src/main/java/com/fasterxml/jackson/core/util/BufferRecyclers.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class BufferRecyclers
6363
*
6464
* @return {@link BufferRecycler} to use
6565
*
66-
* @deprecated Since 2.16 should use {@link BufferRecyclerProvider} abstraction instead
66+
* @deprecated Since 2.16 should use {@link BufferRecyclerPool} abstraction instead
6767
* of calling static methods of this class
6868
*/
6969
@Deprecated // since 2.16
@@ -188,30 +188,63 @@ public static byte[] quoteAsJsonUTF8(String rawText) {
188188

189189
/*
190190
/**********************************************************************
191-
/* Default BufferRecyclerProvider implementations
191+
/* Default BufferRecyclerPool implementations
192192
/**********************************************************************
193193
*/
194194

195-
public static BufferRecyclerProvider defaultProvider() {
196-
return ThreadLocalBufferRecyclerProvider.INSTANCE;
195+
public static BufferRecyclerPool defaultRecyclerPool() {
196+
return ThreadLocalRecyclerPool.INSTANCE;
197+
}
198+
199+
public static BufferRecyclerPool nopRecyclerPool() {
200+
return NonRecyclingRecyclerPool.INSTANCE;
197201
}
198202

199203
/**
200-
* Default {@link BufferRecyclerProvider} implementation that uses
204+
* Default {@link BufferRecyclerPool} implementation that uses
201205
* {@link ThreadLocal} for recycling instances.
202206
*
203207
* @since 2.16
204208
*/
205-
public static class ThreadLocalBufferRecyclerProvider
206-
implements BufferRecyclerProvider
209+
public static class ThreadLocalRecyclerPool
210+
implements BufferRecyclerPool
207211
{
208212
private static final long serialVersionUID = 1L;
209213

210-
public final static ThreadLocalBufferRecyclerProvider INSTANCE = new ThreadLocalBufferRecyclerProvider();
214+
public final static ThreadLocalRecyclerPool INSTANCE = new ThreadLocalRecyclerPool();
211215

212216
@Override
213217
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
214218
return getBufferRecycler();
215219
}
220+
221+
@Override
222+
public void releaseBufferRecycler(BufferRecycler recycler) {
223+
; // nothing to do, relies on ThreadLocal
224+
}
225+
}
226+
227+
/**
228+
* {@link BufferRecyclerPool} implementation that does not use
229+
* any pool but simply creates new instances when necessary.
230+
*
231+
* @since 2.16
232+
*/
233+
public static class NonRecyclingRecyclerPool
234+
implements BufferRecyclerPool
235+
{
236+
private static final long serialVersionUID = 1L;
237+
238+
public final static ThreadLocalRecyclerPool INSTANCE = new ThreadLocalRecyclerPool();
239+
240+
@Override
241+
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
242+
return new BufferRecycler();
243+
}
244+
245+
@Override
246+
public void releaseBufferRecycler(BufferRecycler recycler) {
247+
; // nothing to do, relies on ThreadLocal
248+
}
216249
}
217250
}

0 commit comments

Comments
 (0)