Skip to content

Commit baa0380

Browse files
committed
Some refactoring pre #479
1 parent 4bb9357 commit baa0380

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/main/java/com/fasterxml/jackson/core/io/JsonStringEncoder.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.core.io;
22

33
import java.lang.ref.SoftReference;
4+
import java.util.Arrays;
45

56
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
67
import com.fasterxml.jackson.core.util.TextBuffer;
@@ -52,12 +53,6 @@ public final class JsonStringEncoder
5253
/**********************************************************************
5354
*/
5455

55-
/**
56-
* Lazily constructed text buffer used to produce JSON encoded Strings
57-
* as characters (without UTF-8 encoding)
58-
*/
59-
protected TextBuffer _text;
60-
6156
/**
6257
* Lazily-constructed builder used for UTF-8 encoding of text values
6358
* (quoted and unquoted)
@@ -109,12 +104,8 @@ public static JsonStringEncoder getInstance() {
109104
*/
110105
public char[] quoteAsString(String input)
111106
{
112-
TextBuffer textBuffer = _text;
113-
if (textBuffer == null) {
114-
// no allocator; can add if we must, shouldn't need to
115-
_text = textBuffer = new TextBuffer(null);
116-
}
117-
char[] outputBuffer = textBuffer.emptyAndGetCurrentSegment();
107+
TextBuffer textBuffer = null;
108+
char[] outputBuffer = new char[100];
118109
final int[] escCodes = CharTypes.get7BitOutputEscapes();
119110
final int escCodeCount = escCodes.length;
120111
int inPtr = 0;
@@ -130,6 +121,9 @@ public char[] quoteAsString(String input)
130121
break tight_loop;
131122
}
132123
if (outPtr >= outputBuffer.length) {
124+
if (textBuffer == null) {
125+
textBuffer = TextBuffer.fromInitial(outputBuffer);
126+
}
133127
outputBuffer = textBuffer.finishCurrentSegment();
134128
outPtr = 0;
135129
}
@@ -150,6 +144,9 @@ public char[] quoteAsString(String input)
150144
if (first > 0) {
151145
System.arraycopy(_qbuf, 0, outputBuffer, outPtr, first);
152146
}
147+
if (textBuffer == null) {
148+
textBuffer = TextBuffer.fromInitial(outputBuffer);
149+
}
153150
outputBuffer = textBuffer.finishCurrentSegment();
154151
int second = length - first;
155152
System.arraycopy(_qbuf, first, outputBuffer, 0, second);
@@ -159,6 +156,10 @@ public char[] quoteAsString(String input)
159156
outPtr += length;
160157
}
161158
}
159+
160+
if (textBuffer == null) {
161+
return Arrays.copyOfRange(outputBuffer, 0, outPtr);
162+
}
162163
textBuffer.setCurrentLength(outPtr);
163164
return textBuffer.contentsAsArray();
164165
}
@@ -175,11 +176,9 @@ public char[] quoteAsString(CharSequence input)
175176
return quoteAsString((String) input);
176177
}
177178

178-
TextBuffer textBuffer = _text;
179-
if (textBuffer == null) {
180-
_text = textBuffer = new TextBuffer(null);
181-
}
182-
char[] outputBuffer = textBuffer.emptyAndGetCurrentSegment();
179+
TextBuffer textBuffer = null;
180+
181+
char[] outputBuffer = new char[100];
183182
final int[] escCodes = CharTypes.get7BitOutputEscapes();
184183
final int escCodeCount = escCodes.length;
185184
int inPtr = 0;
@@ -195,6 +194,9 @@ public char[] quoteAsString(CharSequence input)
195194
break tight_loop;
196195
}
197196
if (outPtr >= outputBuffer.length) {
197+
if (textBuffer == null) {
198+
textBuffer = TextBuffer.fromInitial(outputBuffer);
199+
}
198200
outputBuffer = textBuffer.finishCurrentSegment();
199201
outPtr = 0;
200202
}
@@ -203,6 +205,7 @@ public char[] quoteAsString(CharSequence input)
203205
break outer;
204206
}
205207
}
208+
// something to escape; 2 or 6-char variant?
206209
char d = input.charAt(inPtr++);
207210
int escCode = escCodes[d];
208211
int length = (escCode < 0)
@@ -214,6 +217,9 @@ public char[] quoteAsString(CharSequence input)
214217
if (first > 0) {
215218
System.arraycopy(_qbuf, 0, outputBuffer, outPtr, first);
216219
}
220+
if (textBuffer == null) {
221+
textBuffer = TextBuffer.fromInitial(outputBuffer);
222+
}
217223
outputBuffer = textBuffer.finishCurrentSegment();
218224
int second = length - first;
219225
System.arraycopy(_qbuf, first, outputBuffer, 0, second);
@@ -223,6 +229,10 @@ public char[] quoteAsString(CharSequence input)
223229
outPtr += length;
224230
}
225231
}
232+
233+
if (textBuffer == null) {
234+
return Arrays.copyOfRange(outputBuffer, 0, outPtr);
235+
}
226236
textBuffer.setCurrentLength(outPtr);
227237
return textBuffer.contentsAsArray();
228238
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public final class TextBuffer
3030

3131
/**
3232
* Let's start with sizable but not huge buffer, will grow as necessary
33+
*<p>
34+
* Reduced from 1000 down to 500 in 2.10.
3335
*/
34-
final static int MIN_SEGMENT_LEN = 1000;
35-
36+
final static int MIN_SEGMENT_LEN = 500;
37+
3638
/**
3739
* Let's limit maximum segment length to something sensible.
3840
* For 2.10, let's limit to using 64kc chunks (128 kB) -- was 256kC/512kB up to 2.9
@@ -122,6 +124,26 @@ public TextBuffer(BufferRecycler allocator) {
122124
_allocator = allocator;
123125
}
124126

127+
/**
128+
* @since 2.10
129+
*/
130+
protected TextBuffer(BufferRecycler allocator, char[] initialSegment) {
131+
_allocator = allocator;
132+
_currentSegment = initialSegment;
133+
_currentSize = initialSegment.length;
134+
_inputStart = -1;
135+
}
136+
137+
/**
138+
* Factory method for constructing an instance with no allocator, and
139+
* with initial full segment.
140+
*
141+
* @since 2.10
142+
*/
143+
public static TextBuffer fromInitial(char[] initialSegment) {
144+
return new TextBuffer(null, initialSegment);
145+
}
146+
125147
/**
126148
* Method called to indicate that the underlying buffers should now
127149
* be recycled if they haven't yet been recycled. Although caller

src/test/java/com/fasterxml/jackson/core/util/TestTextBuffer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ public void testResetWithAndSetCurrentAndReturn() {
102102
public void testGetCurrentSegment() {
103103
TextBuffer textBuffer = new TextBuffer(null);
104104
textBuffer.emptyAndGetCurrentSegment();
105-
textBuffer.setCurrentAndReturn(1000);
105+
// 26-Aug-2019, tatu: Value depends on "minimum segment size":
106+
textBuffer.setCurrentAndReturn(500);
106107
textBuffer.getCurrentSegment();
107108

108-
assertEquals(1000, textBuffer.size());
109+
assertEquals(500, textBuffer.size());
109110
}
110111

111112
public void testAppendTakingTwoAndThreeInts() {

0 commit comments

Comments
 (0)