Skip to content

Commit c5f647e

Browse files
authored
Add BufferRecyclerProvider configuration (#1083)
1 parent 7adb383 commit c5f647e

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +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;
2324
import com.fasterxml.jackson.core.util.BufferRecyclers;
2425
import com.fasterxml.jackson.core.util.JacksonFeature;
2526
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;
@@ -260,6 +261,11 @@ public static int collectDefaults() {
260261
/**********************************************************
261262
*/
262263

264+
/**
265+
* @since 2.16
266+
*/
267+
protected BufferRecyclerProvider _bufferRecyclerProvider;
268+
263269
/**
264270
* Object that implements conversion functionality between
265271
* Java objects and JSON content. For base JsonFactory implementation
@@ -364,6 +370,7 @@ public static int collectDefaults() {
364370
public JsonFactory() { this((ObjectCodec) null); }
365371

366372
public JsonFactory(ObjectCodec oc) {
373+
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
367374
_objectCodec = oc;
368375
_quoteChar = DEFAULT_QUOTE_CHAR;
369376
_streamReadConstraints = StreamReadConstraints.defaults();
@@ -382,6 +389,7 @@ public JsonFactory(ObjectCodec oc) {
382389
*/
383390
protected JsonFactory(JsonFactory src, ObjectCodec codec)
384391
{
392+
_bufferRecyclerProvider = src._bufferRecyclerProvider;
385393
_objectCodec = codec;
386394

387395
// General
@@ -410,6 +418,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
410418
* @since 2.10
411419
*/
412420
public JsonFactory(JsonFactoryBuilder b) {
421+
_bufferRecyclerProvider = b._bufferRecyclerProvider;
413422
_objectCodec = null;
414423

415424
// General
@@ -439,6 +448,7 @@ public JsonFactory(JsonFactoryBuilder b) {
439448
* @param bogus Argument only needed to separate constructor signature; ignored
440449
*/
441450
protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
451+
_bufferRecyclerProvider = b._bufferRecyclerProvider;
442452
_objectCodec = null;
443453

444454
_factoryFeatures = b._factoryFeatures;
@@ -1131,6 +1141,11 @@ public String getRootValueSeparator() {
11311141
/**********************************************************
11321142
*/
11331143

1144+
public JsonFactory setBufferRecyclerProvider(BufferRecyclerProvider p) {
1145+
_bufferRecyclerProvider = Objects.requireNonNull(p);
1146+
return this;
1147+
}
1148+
11341149
/**
11351150
* Method for associating a {@link ObjectCodec} (typically
11361151
* a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
@@ -2126,10 +2141,10 @@ public BufferRecycler _getBufferRecycler()
21262141
// 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
21272142
// scheme, for cases where it is considered harmful (possibly
21282143
// on Android, for example)
2129-
if (Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
2130-
return BufferRecyclers.getBufferRecycler();
2144+
if (!Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
2145+
return new BufferRecycler();
21312146
}
2132-
return new BufferRecycler();
2147+
return _bufferRecyclerProvider.acquireBufferRecycler(this);
21332148
}
21342149

21352150
/**

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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;
12+
import com.fasterxml.jackson.core.util.BufferRecyclers;
1113
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;
1214

1315
/**
@@ -70,6 +72,11 @@ public abstract class TSFBuilder<F extends JsonFactory,
7072
/**********************************************************************
7173
*/
7274

75+
/**
76+
* @since 2.16
77+
*/
78+
protected BufferRecyclerProvider _bufferRecyclerProvider;
79+
7380
/**
7481
* Optional helper object that may decorate input sources, to do
7582
* additional processing on input during parsing.
@@ -108,7 +115,7 @@ public abstract class TSFBuilder<F extends JsonFactory,
108115
*/
109116
protected List<JsonGeneratorDecorator> _generatorDecorators;
110117

111-
118+
/*
112119
/**********************************************************************
113120
/* Construction
114121
/**********************************************************************
@@ -135,6 +142,8 @@ protected TSFBuilder(JsonFactory base)
135142
protected TSFBuilder(int factoryFeatures,
136143
int parserFeatures, int generatorFeatures)
137144
{
145+
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
146+
138147
_factoryFeatures = factoryFeatures;
139148
_streamReadFeatures = parserFeatures;
140149
_streamWriteFeatures = generatorFeatures;
@@ -161,6 +170,10 @@ protected static <T> List<T> _copy(List<T> src) {
161170
public int streamReadFeatures() { return _streamReadFeatures; }
162171
public int streamWriteFeatures() { return _streamWriteFeatures; }
163172

173+
public BufferRecyclerProvider bufferRecyclerProvider() {
174+
return _bufferRecyclerProvider;
175+
}
176+
164177
public InputDecorator inputDecorator() { return _inputDecorator; }
165178
public OutputDecorator outputDecorator() { return _outputDecorator; }
166179

@@ -300,6 +313,20 @@ public B configure(JsonWriteFeature f, boolean state) {
300313
return _failNonJSON(f);
301314
}
302315

316+
// // // Other configuration, helper objects
317+
318+
/**
319+
* @param p BufferRecyclerProvider to use for buffer allocation
320+
*
321+
* @return this builder (for call chaining)
322+
*
323+
* @since 2.16
324+
*/
325+
public B bufferRecyclerProvider(BufferRecyclerProvider p) {
326+
_bufferRecyclerProvider = Objects.requireNonNull(p);
327+
return _this();
328+
}
329+
303330
// // // Other configuration, decorators
304331

305332
public B inputDecorator(InputDecorator dec) {

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Arrays;
77

88
import com.fasterxml.jackson.core.*;
9-
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
109
import com.fasterxml.jackson.core.io.IOContext;
1110
import com.fasterxml.jackson.core.io.ContentReference;
1211
import com.fasterxml.jackson.core.io.NumberInput;

src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.core.json;
22

33
import java.io.*;
4-
import java.util.Arrays;
54

65
import com.fasterxml.jackson.core.*;
76
import com.fasterxml.jackson.core.base.ParserBase;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.fasterxml.jackson.core.util;
2+
3+
import com.fasterxml.jackson.core.TokenStreamFactory;
4+
5+
/**
6+
* Provider for {@link BufferRecycler}s to allow pluggable and optional
7+
* recycling of underlying input/output buffers.
8+
*
9+
* @since 2.16
10+
*/
11+
public interface BufferRecyclerProvider
12+
extends java.io.Serializable
13+
{
14+
public abstract BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory);
15+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.ref.SoftReference;
44

5+
import com.fasterxml.jackson.core.TokenStreamFactory;
56
import com.fasterxml.jackson.core.io.JsonStringEncoder;
67

78
/**
@@ -61,7 +62,11 @@ public class BufferRecyclers
6162
* Main accessor to call for accessing possibly recycled {@link BufferRecycler} instance.
6263
*
6364
* @return {@link BufferRecycler} to use
65+
*
66+
* @deprecated Since 2.16 should use {@link BufferRecyclerProvider} abstraction instead
67+
* of calling static methods of this class
6468
*/
69+
@Deprecated // since 2.16
6570
public static BufferRecycler getBufferRecycler()
6671
{
6772
SoftReference<BufferRecycler> ref = _recyclerRef.get();
@@ -180,4 +185,33 @@ public static void quoteAsJsonText(CharSequence input, StringBuilder output) {
180185
public static byte[] quoteAsJsonUTF8(String rawText) {
181186
return JsonStringEncoder.getInstance().quoteAsUTF8(rawText);
182187
}
188+
189+
/*
190+
/**********************************************************************
191+
/* Default BufferRecyclerProvider implementations
192+
/**********************************************************************
193+
*/
194+
195+
public static BufferRecyclerProvider defaultProvider() {
196+
return ThreadLocalBufferRecyclerProvider.INSTANCE;
197+
}
198+
199+
/**
200+
* Default {@link BufferRecyclerProvider} implementation that uses
201+
* {@link ThreadLocal} for recycling instances.
202+
*
203+
* @since 2.16
204+
*/
205+
public static class ThreadLocalBufferRecyclerProvider
206+
implements BufferRecyclerProvider
207+
{
208+
private static final long serialVersionUID = 1L;
209+
210+
public final static ThreadLocalBufferRecyclerProvider INSTANCE = new ThreadLocalBufferRecyclerProvider();
211+
212+
@Override
213+
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
214+
return getBufferRecycler();
215+
}
216+
}
183217
}

0 commit comments

Comments
 (0)