-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 49 additions & 23 deletions
72
avro/src/main/java/tools/jackson/dataformat/avro/apacheimpl/ApacheCodecRecycler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,86 @@ | ||
package tools.jackson.dataformat.avro.apacheimpl; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.apache.avro.io.*; | ||
|
||
import tools.jackson.core.util.RecyclerPool; | ||
import tools.jackson.core.util.RecyclerPool.WithPool; | ||
|
||
/** | ||
* Simple helper class that contains extracted functionality for | ||
* simple encoder/decoder recycling. | ||
*/ | ||
public final class ApacheCodecRecycler | ||
implements WithPool<ApacheCodecRecycler> | ||
{ | ||
protected final static ThreadLocal<SoftReference<ApacheCodecRecycler>> _recycler | ||
= new ThreadLocal<SoftReference<ApacheCodecRecycler>>(); | ||
|
||
private final AtomicReference<BinaryDecoder> decoderRef = new AtomicReference<>(); | ||
private final AtomicReference<BinaryEncoder> encoderRef = new AtomicReference<>(); | ||
|
||
private ApacheCodecRecycler() { } | ||
private RecyclerPool<ApacheCodecRecycler> _pool; | ||
|
||
ApacheCodecRecycler() { } | ||
|
||
/* | ||
/********************************************************************** | ||
/* Public API | ||
/********************************************************************** | ||
*/ | ||
|
||
public static BinaryDecoder acquireDecoder() { | ||
return _recycler().decoderRef.getAndSet(null); | ||
public BinaryDecoder acquireDecoder() { | ||
return decoderRef.getAndSet(null); | ||
} | ||
|
||
public static BinaryEncoder acquireEncoder() { | ||
return _recycler().encoderRef.getAndSet(null); | ||
public BinaryEncoder acquireEncoder() { | ||
return encoderRef.getAndSet(null); | ||
} | ||
|
||
public static void release(BinaryDecoder dec) { | ||
_recycler().decoderRef.set(dec); | ||
public void release(BinaryDecoder dec) { | ||
decoderRef.set(dec); | ||
} | ||
|
||
public static void release(BinaryEncoder enc) { | ||
_recycler().encoderRef.set(enc); | ||
public void release(BinaryEncoder enc) { | ||
encoderRef.set(enc); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Internal per-instance methods | ||
/********************************************************************** | ||
/********************************************************** | ||
/* WithPool implementation | ||
/********************************************************** | ||
*/ | ||
|
||
private static ApacheCodecRecycler _recycler() { | ||
SoftReference<ApacheCodecRecycler> ref = _recycler.get(); | ||
ApacheCodecRecycler r = (ref == null) ? null : ref.get(); | ||
/** | ||
* Method called by owner of this recycler instance, to provide reference to | ||
* {@link RecyclerPool} into which instance is to be released (if any) | ||
* | ||
* @since 2.16 | ||
*/ | ||
@Override | ||
public ApacheCodecRecycler withPool(RecyclerPool<ApacheCodecRecycler> pool) { | ||
if (this._pool != null) { | ||
throw new IllegalStateException("ApacheCodecRecycler already linked to pool: "+pool); | ||
} | ||
// assign to pool to which this BufferRecycler belongs in order to release it | ||
// to the same pool when the work will be completed | ||
_pool = Objects.requireNonNull(pool); | ||
return this; | ||
} | ||
|
||
if (r == null) { | ||
r = new ApacheCodecRecycler(); | ||
_recycler.set(new SoftReference<>(r)); | ||
/** | ||
* Method called when owner of this recycler no longer wishes use it; this should | ||
* return it to pool passed via {@code withPool()} (if any). | ||
* | ||
* @since 2.16 | ||
*/ | ||
@Override | ||
public void releaseToPool() { | ||
if (_pool != null) { | ||
RecyclerPool<ApacheCodecRecycler> tmpPool = _pool; | ||
// nullify the reference to the pool in order to avoid the risk of releasing | ||
// the same BufferRecycler more than once, thus compromising the pool integrity | ||
_pool = null; | ||
tmpPool.releasePooled(this); | ||
} | ||
return r; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
avro/src/main/java/tools/jackson/dataformat/avro/apacheimpl/AvroRecyclerPools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package tools.jackson.dataformat.avro.apacheimpl; | ||
|
||
import java.lang.ref.SoftReference; | ||
|
||
import tools.jackson.core.util.BufferRecycler; | ||
import tools.jackson.core.util.RecyclerPool; | ||
|
||
public final class AvroRecyclerPools | ||
{ | ||
/** | ||
* @return the default {@link RecyclerPool} implementation | ||
* which is the thread local based one: | ||
* basically alias to {@link #threadLocalPool()}). | ||
*/ | ||
public static RecyclerPool<ApacheCodecRecycler> defaultPool() { | ||
return threadLocalPool(); | ||
} | ||
|
||
/** | ||
* 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<ApacheCodecRecycler> threadLocalPool() { | ||
return ThreadLocalPool.GLOBAL; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* 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<ApacheCodecRecycler> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
protected static final ThreadLocalPool GLOBAL = new ThreadLocalPool(); | ||
|
||
protected final static ThreadLocal<SoftReference<ApacheCodecRecycler>> _recycler | ||
= new ThreadLocal<SoftReference<ApacheCodecRecycler>>(); | ||
|
||
private ThreadLocalPool() { } | ||
|
||
@Override | ||
public ApacheCodecRecycler acquirePooled() { | ||
SoftReference<ApacheCodecRecycler> ref = _recycler.get(); | ||
ApacheCodecRecycler r = (ref == null) ? null : ref.get(); | ||
|
||
if (r == null) { | ||
r = new ApacheCodecRecycler(); | ||
_recycler.set(new SoftReference<>(r)); | ||
} | ||
return r; | ||
} | ||
|
||
// // // JDK serialization support | ||
|
||
protected Object readResolve() { return GLOBAL; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters