Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to initial BufferRecyclerProvider PR #1085

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclerProvider;
import com.fasterxml.jackson.core.util.BufferRecyclerPool;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.core.util.JacksonFeature;
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;
Expand Down Expand Up @@ -264,7 +264,7 @@ public static int collectDefaults() {
/**
* @since 2.16
*/
protected BufferRecyclerProvider _bufferRecyclerProvider;
protected BufferRecyclerPool _bufferRecyclerPool;

/**
* Object that implements conversion functionality between
Expand Down Expand Up @@ -370,7 +370,7 @@ public static int collectDefaults() {
public JsonFactory() { this((ObjectCodec) null); }

public JsonFactory(ObjectCodec oc) {
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
_bufferRecyclerPool = BufferRecyclers.defaultRecyclerPool();
_objectCodec = oc;
_quoteChar = DEFAULT_QUOTE_CHAR;
_streamReadConstraints = StreamReadConstraints.defaults();
Expand All @@ -389,7 +389,7 @@ public JsonFactory(ObjectCodec oc) {
*/
protected JsonFactory(JsonFactory src, ObjectCodec codec)
{
_bufferRecyclerProvider = src._bufferRecyclerProvider;
_bufferRecyclerPool = src._bufferRecyclerPool;
_objectCodec = codec;

// General
Expand Down Expand Up @@ -418,7 +418,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
* @since 2.10
*/
public JsonFactory(JsonFactoryBuilder b) {
_bufferRecyclerProvider = b._bufferRecyclerProvider;
_bufferRecyclerPool = b._bufferRecyclerPool;
_objectCodec = null;

// General
Expand Down Expand Up @@ -448,7 +448,7 @@ public JsonFactory(JsonFactoryBuilder b) {
* @param bogus Argument only needed to separate constructor signature; ignored
*/
protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
_bufferRecyclerProvider = b._bufferRecyclerProvider;
_bufferRecyclerPool = b._bufferRecyclerPool;
_objectCodec = null;

_factoryFeatures = b._factoryFeatures;
Expand Down Expand Up @@ -1141,8 +1141,8 @@ public String getRootValueSeparator() {
/**********************************************************
*/

public JsonFactory setBufferRecyclerProvider(BufferRecyclerProvider p) {
_bufferRecyclerProvider = Objects.requireNonNull(p);
public JsonFactory setBufferRecyclerPool(BufferRecyclerPool p) {
_bufferRecyclerPool = Objects.requireNonNull(p);
return this;
}

Expand Down Expand Up @@ -2138,13 +2138,23 @@ protected JsonGenerator _decorate(JsonGenerator g) {
*/
public BufferRecycler _getBufferRecycler()
{
return _getBufferRecyclerPool().acquireBufferRecycler(this);
}

/**
* Accessor for getting access to {@link BufferRecyclerPool} for getting
* {@link BufferRecycler} instance to use.
*
* @since 2.16
*/
public BufferRecyclerPool _getBufferRecyclerPool() {
// 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
// scheme, for cases where it is considered harmful (possibly
// on Android, for example)
if (!Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
return new BufferRecycler();
return BufferRecyclers.nopRecyclerPool();
}
return _bufferRecyclerProvider.acquireBufferRecycler(this);
return _bufferRecyclerPool;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.fasterxml.jackson.core.io.OutputDecorator;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.util.BufferRecyclerProvider;
import com.fasterxml.jackson.core.util.BufferRecyclerPool;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.core.util.JsonGeneratorDecorator;

Expand Down Expand Up @@ -75,7 +75,7 @@ public abstract class TSFBuilder<F extends JsonFactory,
/**
* @since 2.16
*/
protected BufferRecyclerProvider _bufferRecyclerProvider;
protected BufferRecyclerPool _bufferRecyclerPool;

/**
* Optional helper object that may decorate input sources, to do
Expand Down Expand Up @@ -142,7 +142,7 @@ protected TSFBuilder(JsonFactory base)
protected TSFBuilder(int factoryFeatures,
int parserFeatures, int generatorFeatures)
{
_bufferRecyclerProvider = BufferRecyclers.defaultProvider();
_bufferRecyclerPool = BufferRecyclers.defaultRecyclerPool();

_factoryFeatures = factoryFeatures;
_streamReadFeatures = parserFeatures;
Expand Down Expand Up @@ -170,8 +170,8 @@ protected static <T> List<T> _copy(List<T> src) {
public int streamReadFeatures() { return _streamReadFeatures; }
public int streamWriteFeatures() { return _streamWriteFeatures; }

public BufferRecyclerProvider bufferRecyclerProvider() {
return _bufferRecyclerProvider;
public BufferRecyclerPool bufferRecyclerPool() {
return _bufferRecyclerPool;
}

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

/**
* @param p BufferRecyclerProvider to use for buffer allocation
* @param p BufferRecyclerPool to use for buffer allocation
*
* @return this builder (for call chaining)
*
* @since 2.16
*/
public B bufferRecyclerProvider(BufferRecyclerProvider p) {
_bufferRecyclerProvider = Objects.requireNonNull(p);
public B bufferRecyclerPool(BufferRecyclerPool p) {
_bufferRecyclerPool = Objects.requireNonNull(p);
return _this();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fasterxml.jackson.core.util;

import com.fasterxml.jackson.core.TokenStreamFactory;

/**
* Interface for entity that controls creation and possible reuse of {@link BufferRecycler}
* instances used for recycling of underlying input/output buffers.
*
* @since 2.16
*/
public interface BufferRecyclerPool
extends java.io.Serializable
{
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowtowncoder I don't understand why this method takes a TokenStreamFactory as argument, can you please clarify?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows inspecting configuration of the factory that creates parser/generator that needs recycler: mostly JsonFactory.Feature settings.
TokenStreamFactory is that abstract super-type of JsonFactory; could pass latter if that proves necessary.


public void releaseBufferRecycler(BufferRecycler recycler);
}

This file was deleted.

49 changes: 41 additions & 8 deletions src/main/java/com/fasterxml/jackson/core/util/BufferRecyclers.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class BufferRecyclers
*
* @return {@link BufferRecycler} to use
*
* @deprecated Since 2.16 should use {@link BufferRecyclerProvider} abstraction instead
* @deprecated Since 2.16 should use {@link BufferRecyclerPool} abstraction instead
* of calling static methods of this class
*/
@Deprecated // since 2.16
Expand Down Expand Up @@ -188,30 +188,63 @@ public static byte[] quoteAsJsonUTF8(String rawText) {

/*
/**********************************************************************
/* Default BufferRecyclerProvider implementations
/* Default BufferRecyclerPool implementations
/**********************************************************************
*/

public static BufferRecyclerProvider defaultProvider() {
return ThreadLocalBufferRecyclerProvider.INSTANCE;
public static BufferRecyclerPool defaultRecyclerPool() {
return ThreadLocalRecyclerPool.INSTANCE;
}

public static BufferRecyclerPool nopRecyclerPool() {
return NonRecyclingRecyclerPool.INSTANCE;
}

/**
* Default {@link BufferRecyclerProvider} implementation that uses
* Default {@link BufferRecyclerPool} implementation that uses
* {@link ThreadLocal} for recycling instances.
*
* @since 2.16
*/
public static class ThreadLocalBufferRecyclerProvider
implements BufferRecyclerProvider
public static class ThreadLocalRecyclerPool
implements BufferRecyclerPool
{
private static final long serialVersionUID = 1L;

public final static ThreadLocalBufferRecyclerProvider INSTANCE = new ThreadLocalBufferRecyclerProvider();
public final static ThreadLocalRecyclerPool INSTANCE = new ThreadLocalRecyclerPool();

@Override
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
return getBufferRecycler();
}

@Override
public void releaseBufferRecycler(BufferRecycler recycler) {
; // nothing to do, relies on ThreadLocal
}
}

/**
* {@link BufferRecyclerPool} implementation that does not use
* any pool but simply creates new instances when necessary.
*
* @since 2.16
*/
public static class NonRecyclingRecyclerPool
implements BufferRecyclerPool
{
private static final long serialVersionUID = 1L;

public final static ThreadLocalRecyclerPool INSTANCE = new ThreadLocalRecyclerPool();

@Override
public BufferRecycler acquireBufferRecycler(TokenStreamFactory forFactory) {
return new BufferRecycler();
}

@Override
public void releaseBufferRecycler(BufferRecycler recycler) {
; // nothing to do, relies on ThreadLocal
}
}
}