|
1 | 1 | package io.avaje.jsonb.stream; |
2 | 2 |
|
3 | | -import java.io.IOException; |
4 | 3 | import java.io.InputStream; |
5 | 4 | import java.io.OutputStream; |
6 | 5 |
|
7 | 6 | final class Recycle { |
| 7 | + private Recycle() {} |
| 8 | + |
| 9 | + private static boolean jvmRecycle; |
| 10 | + private static ThreadLocal<JParser> read; |
| 11 | + private static ThreadLocal<JGenerator> managed; |
| 12 | + |
| 13 | + static { |
| 14 | + if (Float.parseFloat(System.getProperty("java.specification.version")) >= 19 |
| 15 | + && !Boolean.getBoolean("jsonb.UseTLBuffers")) { |
| 16 | + jvmRecycle = true; |
| 17 | + } else { |
| 18 | + managed = ThreadLocal.withInitial(Recycle::getGenerator); |
| 19 | + read = ThreadLocal.withInitial(Recycle::getParser); |
| 20 | + } |
| 21 | + } |
8 | 22 |
|
9 | | - static ThreadLocal<JGenerator> managed = ThreadLocal.withInitial(() -> new JGenerator(4096)); |
10 | | - |
11 | | - static ThreadLocal<JParser> read = ThreadLocal.withInitial(() -> { |
12 | | - char[] ch = new char[4096]; |
13 | | - byte[] by = new byte[4096]; |
14 | | - return new JParser(ch, by, 0, JParser.ErrorInfo.MINIMAL, JParser.DoublePrecision.DEFAULT, JParser.UnknownNumberParsing.BIGDECIMAL, 100, 50_000); |
15 | | - }); |
16 | | - |
17 | | - /** |
18 | | - * Return a recycled generator with the given target OutputStream. |
19 | | - */ |
| 23 | + /** Return a recycled generator with the given target OutputStream. */ |
20 | 24 | static JsonGenerator generator(OutputStream target) { |
21 | | - return managed.get().prepare(target); |
| 25 | + return (jvmRecycle ? getGenerator() : managed.get()).prepare(target); |
22 | 26 | } |
23 | 27 |
|
24 | | - /** |
25 | | - * Return a recycled generator with expected "to String" result. |
26 | | - */ |
| 28 | + /** Return a recycled generator with expected "to String" result. */ |
27 | 29 | static JsonGenerator generator() { |
28 | | - return managed.get().prepare(null); |
| 30 | + return (jvmRecycle ? getGenerator() : managed.get()).prepare(null); |
29 | 31 | } |
30 | 32 |
|
31 | 33 | static JsonParser parser(byte[] bytes) { |
32 | | - return read.get().process(bytes, bytes.length); |
| 34 | + return (jvmRecycle ? getParser() : read.get()).process(bytes, bytes.length); |
| 35 | + } |
| 36 | + |
| 37 | + static JsonParser parser(InputStream in) { |
| 38 | + return (jvmRecycle ? getParser() : read.get()).process(in); |
| 39 | + } |
| 40 | + |
| 41 | + static JGenerator getGenerator() { |
| 42 | + return new JGenerator(4096); |
33 | 43 | } |
34 | 44 |
|
35 | | - static JsonParser parser(InputStream in) throws IOException { |
36 | | - return read.get().process(in); |
| 45 | + static JParser getParser() { |
| 46 | + final char[] ch = new char[4096]; |
| 47 | + final byte[] by = new byte[4096]; |
| 48 | + return new JParser( |
| 49 | + ch, |
| 50 | + by, |
| 51 | + 0, |
| 52 | + JParser.ErrorInfo.MINIMAL, |
| 53 | + JParser.DoublePrecision.DEFAULT, |
| 54 | + JParser.UnknownNumberParsing.BIGDECIMAL, |
| 55 | + 100, |
| 56 | + 50_000); |
37 | 57 | } |
38 | 58 | } |
0 commit comments