Skip to content

Commit

Permalink
Part 1 of doing #393: combine parser/generator side recycler (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Sep 15, 2023
1 parent 8c78d84 commit 4ced59c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,66 @@

import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.dataformat.smile.SmileGenerator.SharedStringNode;

/**
* Simple helper class used for implementing simple reuse system for Smile-specific
* buffers that are used.
*
* @param <T> Type of name entries stored in arrays to recycle
*/
public class SmileBufferRecycler<T>
public class SmileBufferRecycler
{
public final static int DEFAULT_NAME_BUFFER_LENGTH = 64;

public final static int DEFAULT_STRING_VALUE_BUFFER_LENGTH = 64;

protected final AtomicReference<T[]> _seenNamesBuffer = new AtomicReference<>();
// // // Input side

protected final AtomicReference<String[]> _seenNamesReadBuffer = new AtomicReference<>();

protected final AtomicReference<String[]> _seenStringValuesReadBuffer = new AtomicReference<>();

// // // Output side

protected final AtomicReference<SharedStringNode[]> _seenNamesWriteBuffer = new AtomicReference<>();

protected final AtomicReference<SharedStringNode[]> _seenStringValuesWriteBuffer = new AtomicReference<>();

protected final AtomicReference<T[]> _seenStringValuesBuffer = new AtomicReference<>();

public SmileBufferRecycler() { }

public T[] allocSeenNamesBuffer()
{
return _seenNamesBuffer.getAndSet(null);
// // // Input side

public String[] allocSeenNamesReadBuffer() {
return _seenNamesReadBuffer.getAndSet(null);
}

public String[] allocSeenStringValuesReadBuffer() {
return _seenStringValuesReadBuffer.getAndSet(null);
}

public void releaseSeenNamesReadBuffer(String[] buffer) {
_seenNamesReadBuffer.set(buffer);
}

public void releaseSeenStringValuesReadBuffer(String[] buffer) {
_seenStringValuesReadBuffer.set(buffer);
}

// // // Output side

public SharedStringNode[] allocSeenNamesWriteBuffer() {
return _seenNamesWriteBuffer.getAndSet(null);
}

public T[] allocSeenStringValuesBuffer()
{
return _seenStringValuesBuffer.getAndSet(null);
public SharedStringNode[] allocSeenStringValuesWriteBuffer() {
return _seenStringValuesWriteBuffer.getAndSet(null);
}

public void releaseSeenNamesBuffer(T[] buffer) {
_seenNamesBuffer.set(buffer);
public void releaseSeenNamesWriteBuffer(SharedStringNode[] buffer) {
_seenNamesWriteBuffer.set(buffer);
}

public void releaseSeenStringValuesBuffer(T[] buffer) {
_seenStringValuesBuffer.set(buffer);
public void releaseSeenStringValuesWriteBuffer(SharedStringNode[] buffer) {
_seenStringValuesWriteBuffer.set(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private Feature(boolean defaultState) {
* Helper class used for keeping track of possibly shareable String
* references (for field names and/or short String values)
*/
protected final static class SharedStringNode
public final static class SharedStringNode
{
public final String value;
public final int index;
Expand Down Expand Up @@ -201,7 +201,7 @@ public SharedStringNode(String value, int index, SharedStringNode next)
* Helper object used for low-level recycling of Smile-generator
* specific buffers.
*/
protected final SmileBufferRecycler<SharedStringNode> _smileBufferRecycler;
protected final SmileBufferRecycler _smileBufferRecycler;

/*
/**********************************************************************
Expand Down Expand Up @@ -290,8 +290,8 @@ public SharedStringNode(String value, int index, SharedStringNode next)
* to a buffer recycler used to provide a low-cost
* buffer recycling for Smile-specific buffers.
*/
final protected static ThreadLocal<SoftReference<SmileBufferRecycler<SharedStringNode>>> _smileRecyclerRef
= new ThreadLocal<SoftReference<SmileBufferRecycler<SharedStringNode>>>();
final protected static ThreadLocal<SoftReference<SmileBufferRecycler>> _smileRecyclerRef
= new ThreadLocal<SoftReference<SmileBufferRecycler>>();

/*
/**********************************************************************
Expand Down Expand Up @@ -325,7 +325,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
_seenNames = null;
_seenNameCount = -1;
} else {
_seenNames = _smileBufferRecycler.allocSeenNamesBuffer();
_seenNames = _smileBufferRecycler.allocSeenNamesWriteBuffer();
if (_seenNames == null) {
_seenNames = new SharedStringNode[SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH];
}
Expand All @@ -336,7 +336,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
_seenStringValues = null;
_seenStringValueCount = -1;
} else {
_seenStringValues = _smileBufferRecycler.allocSeenStringValuesBuffer();
_seenStringValues = _smileBufferRecycler.allocSeenStringValuesWriteBuffer();
if (_seenStringValues == null) {
_seenStringValues = new SharedStringNode[SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH];
}
Expand Down Expand Up @@ -372,7 +372,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
_seenNames = null;
_seenNameCount = -1;
} else {
_seenNames = _smileBufferRecycler.allocSeenNamesBuffer();
_seenNames = _smileBufferRecycler.allocSeenNamesWriteBuffer();
if (_seenNames == null) {
_seenNames = new SharedStringNode[SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH];
}
Expand All @@ -383,7 +383,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
_seenStringValues = null;
_seenStringValueCount = -1;
} else {
_seenStringValues = _smileBufferRecycler.allocSeenStringValuesBuffer();
_seenStringValues = _smileBufferRecycler.allocSeenStringValuesWriteBuffer();
if (_seenStringValues == null) {
_seenStringValues = new SharedStringNode[SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH];
}
Expand Down Expand Up @@ -413,14 +413,14 @@ public void writeHeader() throws IOException
_writeBytes(HEADER_BYTE_1, HEADER_BYTE_2, HEADER_BYTE_3, (byte) last);
}

protected final static SmileBufferRecycler<SharedStringNode> _smileBufferRecycler()
protected final static SmileBufferRecycler _smileBufferRecycler()
{
SoftReference<SmileBufferRecycler<SharedStringNode>> ref = _smileRecyclerRef.get();
SmileBufferRecycler<SharedStringNode> br = (ref == null) ? null : ref.get();
SoftReference<SmileBufferRecycler> ref = _smileRecyclerRef.get();
SmileBufferRecycler br = (ref == null) ? null : ref.get();

if (br == null) {
br = new SmileBufferRecycler<SharedStringNode>();
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler<SharedStringNode>>(br));
br = new SmileBufferRecycler();
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler>(br));
}
return br;
}
Expand Down Expand Up @@ -2638,7 +2638,7 @@ protected void _releaseBuffers()
if (_seenNameCount > 0) {
Arrays.fill(nameBuf, null);
}
_smileBufferRecycler.releaseSeenNamesBuffer(nameBuf);
_smileBufferRecycler.releaseSeenNamesWriteBuffer(nameBuf);
}
}
{
Expand All @@ -2651,7 +2651,7 @@ protected void _releaseBuffers()
if (_seenStringValueCount > 0) {
Arrays.fill(valueBuf, null);
}
_smileBufferRecycler.releaseSeenStringValuesBuffer(valueBuf);
_smileBufferRecycler.releaseSeenStringValuesWriteBuffer(valueBuf);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private final void _expandSeenStringValues(String newText)
int len = oldShared.length;
String[] newShared;
if (len == 0) {
newShared = _smileBufferRecycler.allocSeenStringValuesBuffer();
newShared = _smileBufferRecycler.allocSeenStringValuesReadBuffer();
if (newShared == null) {
newShared = new String[SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH];
}
Expand Down Expand Up @@ -1526,7 +1526,7 @@ private final String[] _expandSeenNames(String[] oldShared)
int len = oldShared.length;
String[] newShared;
if (len == 0) {
newShared = _smileBufferRecycler.allocSeenNamesBuffer();
newShared = _smileBufferRecycler.allocSeenNamesReadBuffer();
if (newShared == null) {
newShared = new String[SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,14 @@ public abstract class SmileParserBase extends ParserMinimalBase
* to a buffer recycler used to provide a low-cost
* buffer recycling for Smile-specific buffers.
*/
protected final static ThreadLocal<SoftReference<SmileBufferRecycler<String>>> _smileRecyclerRef
= new ThreadLocal<SoftReference<SmileBufferRecycler<String>>>();
protected final static ThreadLocal<SoftReference<SmileBufferRecycler>> _smileRecyclerRef
= new ThreadLocal<SoftReference<SmileBufferRecycler>>();

/**
* Helper object used for low-level recycling of Smile-generator
* specific buffers.
*/
protected final SmileBufferRecycler<String> _smileBufferRecycler;
protected final SmileBufferRecycler _smileBufferRecycler;

/*
/**********************************************************************
Expand Down Expand Up @@ -284,14 +284,14 @@ public StreamReadConstraints streamReadConstraints() {
return _ioContext.streamReadConstraints();
}

protected final static SmileBufferRecycler<String> _smileBufferRecycler()
protected final static SmileBufferRecycler _smileBufferRecycler()
{
SoftReference<SmileBufferRecycler<String>> ref = _smileRecyclerRef.get();
SmileBufferRecycler<String> br = (ref == null) ? null : ref.get();
SoftReference<SmileBufferRecycler> ref = _smileRecyclerRef.get();
SmileBufferRecycler br = (ref == null) ? null : ref.get();

if (br == null) {
br = new SmileBufferRecycler<String>();
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler<String>>(br));
br = new SmileBufferRecycler();
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler>(br));
}
return br;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ protected final void _releaseBuffers() throws IOException {
if (_seenNameCount > 0) {
Arrays.fill(nameBuf, 0, _seenNameCount, null);
}
_smileBufferRecycler.releaseSeenNamesBuffer(nameBuf);
_smileBufferRecycler.releaseSeenNamesReadBuffer(nameBuf);
}
String[] valueBuf = _seenStringValues;
if (valueBuf != null && valueBuf.length > 0) {
Expand All @@ -456,7 +456,7 @@ protected final void _releaseBuffers() throws IOException {
if (_seenStringValueCount > 0) {
Arrays.fill(valueBuf, 0, _seenStringValueCount, null);
}
_smileBufferRecycler.releaseSeenStringValuesBuffer(valueBuf);
_smileBufferRecycler.releaseSeenStringValuesReadBuffer(valueBuf);
}
_releaseBuffers2();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ protected final String[] _expandSeenNames(String[] oldShared)
int len = oldShared.length;
String[] newShared;
if (len == 0) {
newShared = _smileBufferRecycler.allocSeenNamesBuffer();
newShared = _smileBufferRecycler.allocSeenNamesReadBuffer();
if (newShared == null) {
newShared = new String[SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH];
}
Expand Down Expand Up @@ -630,7 +630,7 @@ private final void _expandSeenStringValues(String v)
int len = oldShared.length;
String[] newShared;
if (len == 0) {
newShared = _smileBufferRecycler.allocSeenStringValuesBuffer();
newShared = _smileBufferRecycler.allocSeenStringValuesReadBuffer();
if (newShared == null) {
newShared = new String[SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH];
}
Expand Down

0 comments on commit 4ced59c

Please sign in to comment.