Skip to content

Commit b84a077

Browse files
authored
Part 2 of implementing #393 (#395)
1 parent 4ced59c commit b84a077

File tree

3 files changed

+63
-34
lines changed

3 files changed

+63
-34
lines changed

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileFactory.java

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

33
import java.io.*;
4+
import java.lang.ref.SoftReference;
45
import java.net.URL;
56

67
import com.fasterxml.jackson.core.*;
@@ -72,6 +73,19 @@ public class SmileFactory extends JsonFactory
7273
protected int _smileParserFeatures;
7374
protected int _smileGeneratorFeatures;
7475

76+
/*
77+
/**********************************************************
78+
/* Smile-specific buffer recycling (moved here in 2.16)
79+
/**********************************************************
80+
81+
/**
82+
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
83+
* to a buffer recycler used to provide a low-cost
84+
* buffer recycling for Smile-specific buffers.
85+
*/
86+
final protected static ThreadLocal<SoftReference<SmileBufferRecycler>> _smileRecyclerRef
87+
= new ThreadLocal<SoftReference<SmileBufferRecycler>>();
88+
7589
/*
7690
/**********************************************************
7791
/* Factory construction, configuration
@@ -509,7 +523,8 @@ protected SmileGenerator _createGenerator(OutputStream out, IOContext ctxt) thro
509523
* But should we force writing, or throw exception, if settings are in conflict?
510524
* For now, let's error out...
511525
*/
512-
SmileGenerator gen = new SmileGenerator(ctxt, _generatorFeatures, feats, _objectCodec, out);
526+
SmileGenerator gen = new SmileGenerator(ctxt, _generatorFeatures, feats, _objectCodec, out,
527+
_smileBufferRecycler());
513528
if ((feats & SmileGenerator.Feature.WRITE_HEADER.getMask()) != 0) {
514529
gen.writeHeader();
515530
} else {
@@ -528,4 +543,16 @@ protected SmileGenerator _createGenerator(OutputStream out, IOContext ctxt) thro
528543
}
529544
return gen;
530545
}
546+
547+
protected final static SmileBufferRecycler _smileBufferRecycler()
548+
{
549+
SoftReference<SmileBufferRecycler> ref = _smileRecyclerRef.get();
550+
SmileBufferRecycler br = (ref == null) ? null : ref.get();
551+
552+
if (br == null) {
553+
br = new SmileBufferRecycler();
554+
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler>(br));
555+
}
556+
return br;
557+
}
531558
}

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.dataformat.smile;
22

33
import java.io.*;
4-
import java.lang.ref.SoftReference;
54
import java.math.BigDecimal;
65
import java.math.BigInteger;
76
import java.util.Arrays;
@@ -281,26 +280,16 @@ public SharedStringNode(String value, int index, SharedStringNode next)
281280

282281
/*
283282
/**********************************************************************
284-
/* Thread-local recycling
283+
/* Life-cycle
285284
/**********************************************************************
286285
*/
287286

288287
/**
289-
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
290-
* to a buffer recycler used to provide a low-cost
291-
* buffer recycling for Smile-specific buffers.
292-
*/
293-
final protected static ThreadLocal<SoftReference<SmileBufferRecycler>> _smileRecyclerRef
294-
= new ThreadLocal<SoftReference<SmileBufferRecycler>>();
295-
296-
/*
297-
/**********************************************************************
298-
/* Life-cycle
299-
/**********************************************************************
288+
* @since 2.16
300289
*/
301-
302290
public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
303-
ObjectCodec codec, OutputStream out)
291+
ObjectCodec codec, OutputStream out,
292+
SmileBufferRecycler sbr)
304293
{
305294
super(stdFeatures, codec, ioCtxt, /*WriteContext*/ null);
306295
DupDetector dups = JsonGenerator.Feature.STRICT_DUPLICATE_DETECTION.enabledIn(stdFeatures)
@@ -310,7 +299,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
310299
_streamWriteContext = SmileWriteContext.createRootContext(dups);
311300
_formatFeatures = smileFeatures;
312301
_streamWriteConstraints = ioCtxt.streamWriteConstraints();
313-
_smileBufferRecycler = _smileBufferRecycler();
302+
_smileBufferRecycler = sbr;
314303
_out = out;
315304
_bufferRecyclable = true;
316305
_outputBuffer = ioCtxt.allocWriteEncodingBuffer();
@@ -344,9 +333,13 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
344333
}
345334
}
346335

336+
/**
337+
* @since 2.16
338+
*/
347339
public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
348-
ObjectCodec codec, OutputStream out, byte[] outputBuffer, int offset,
349-
boolean bufferRecyclable)
340+
ObjectCodec codec, OutputStream out,
341+
SmileBufferRecycler sbr,
342+
byte[] outputBuffer, int offset, boolean bufferRecyclable)
350343
{
351344
super(stdFeatures, codec, ioCtxt, null);
352345
DupDetector dups = JsonGenerator.Feature.STRICT_DUPLICATE_DETECTION.enabledIn(stdFeatures)
@@ -356,7 +349,7 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
356349
_streamWriteContext = SmileWriteContext.createRootContext(dups);
357350
_formatFeatures = smileFeatures;
358351
_streamWriteConstraints = ioCtxt.streamWriteConstraints();
359-
_smileBufferRecycler = _smileBufferRecycler();
352+
_smileBufferRecycler = sbr;
360353
_out = out;
361354
_bufferRecyclable = bufferRecyclable;
362355
_outputTail = offset;
@@ -391,6 +384,29 @@ public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
391384
}
392385
}
393386

387+
/**
388+
* @deprecated Since 2.16
389+
*/
390+
@Deprecated // @since 2.16
391+
public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
392+
ObjectCodec codec, OutputStream out)
393+
{
394+
this(ioCtxt, stdFeatures, smileFeatures, codec, out, new SmileBufferRecycler());
395+
}
396+
397+
/**
398+
* @deprecated Since 2.16
399+
*/
400+
@Deprecated // @since 2.16
401+
public SmileGenerator(IOContext ioCtxt, int stdFeatures, int smileFeatures,
402+
ObjectCodec codec, OutputStream out,
403+
byte[] outputBuffer, int offset, boolean bufferRecyclable)
404+
{
405+
this(ioCtxt, stdFeatures, smileFeatures,
406+
codec, out, new SmileBufferRecycler(),
407+
outputBuffer, offset, bufferRecyclable);
408+
}
409+
394410
/**
395411
* Method that can be called to explicitly write Smile document header.
396412
* Note that usually you do not need to call this for first document to output,
@@ -413,18 +429,6 @@ public void writeHeader() throws IOException
413429
_writeBytes(HEADER_BYTE_1, HEADER_BYTE_2, HEADER_BYTE_3, (byte) last);
414430
}
415431

416-
protected final static SmileBufferRecycler _smileBufferRecycler()
417-
{
418-
SoftReference<SmileBufferRecycler> ref = _smileRecyclerRef.get();
419-
SmileBufferRecycler br = (ref == null) ? null : ref.get();
420-
421-
if (br == null) {
422-
br = new SmileBufferRecycler();
423-
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler>(br));
424-
}
425-
return br;
426-
}
427-
428432
/*
429433
/**********************************************************
430434
/* Versioned

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/SmileFactoryPropertiesTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.io.*;
44

55
import com.fasterxml.jackson.core.*;
6-
import com.fasterxml.jackson.core.io.IOContext;
7-
import com.fasterxml.jackson.core.io.ContentReference;
86
import com.fasterxml.jackson.dataformat.smile.async.NonBlockingByteArrayParser;
97

108
import static org.junit.Assert.assertArrayEquals;

0 commit comments

Comments
 (0)