I had been doing that intentionnally (more in the spirit "if it overflows
it is wrong anyway") but it is also the case for "safe" apps of this impl,
have a single allocation then be gc friendly.

Romain Manni-Bucau
@rmannibucau <https://x.com/rmannibucau> | .NET Blog
<https://dotnetbirdie.github.io/> | Blog <https://rmannibucau.github.io/> | Old
Blog <http://rmannibucau.wordpress.com> | Github
<https://github.com/rmannibucau> | LinkedIn
<https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/en-us/product/java-ee-8-high-performance-9781788473064>
Javaccino <https://javaccino.dev/> founder (Java/.NET service - contact via
linkedin)


Le dim. 15 nov. 2026 à 16:57, Markus Jung via dev <[email protected]>
a écrit :

> Thinking about this some more I’ll probably revert it (and a316738, which
> is the reason i touched the singleton strategy to be a “maybe” singleton
> instead of a real singleton)
>
> The whole idea of a316738 was basically to prevent OOM errors when a user
> configures an absurdly large number length limit. Nobody should be doing
> that anyways IMO
>
>
> > On 15. Nov 2026, at 15:57, Romain Manni-Bucau <[email protected]>
> wrote:
> >
> > FYI the intent of singleton was literally a singleton, this does break
> that.
> > It has limitations and use cases - ack that - so think this commit is
> wrong
> > in that regard and is another implementation (I dont see an use case for
> it
> > but maybe there is), not a fix.
> >
> > Romain Manni-Bucau
> > @rmannibucau <https://x.com/rmannibucau> | .NET Blog
> > <https://dotnetbirdie.github.io/> | Blog <https://rmannibucau.github.io/>
> | Old
> > Blog <http://rmannibucau.wordpress.com> | Github
> > <https://github.com/rmannibucau> | LinkedIn
> > <https://www.linkedin.com/in/rmannibucau> | Book
> > <
> https://www.packtpub.com/en-us/product/java-ee-8-high-performance-9781788473064
> >
> > Javaccino <https://javaccino.dev/> founder (Java/.NET service - contact
> via
> > linkedin)
> >
> >
> > ---------- Forwarded message ---------
> > De : jungm.apache.org via commits <[email protected]>
> > Date: dim. 15 nov. 2026 à 14:25
> > Subject: (johnzon) 08/09: Hand out SINGLETON buffer to one user at a time
> > To: [email protected] <[email protected]>
> > Cc: <[email protected]>
> >
> >
> > This is an automated email from the ASF dual-hosted git repository.
> >
> > jungm pushed a commit to branch johnzon-1.3.x
> > in repository https://gitbox.apache.org/repos/asf/johnzon.git
> >
> > commit 9027190285f6d1fb1c4aad2e585b6be253aab2c4
> > Author: Markus Jung <[email protected]>
> > AuthorDate: Sun Nov 15 14:09:59 2026 +0100
> >
> >    Hand out SINGLETON buffer to one user at a time
> >
> >    (cherry picked from commit 2caea48d1d57cf7d7f50c4578168ce065dfede4e)
> > ---
> > .../apache/johnzon/core/BufferStrategyFactory.java | 25 +++++++++-----
> > .../johnzon/core/BufferStrategyFactoryTest.java    | 38
> > ++++++++++++++++++++++
> > 2 files changed, 54 insertions(+), 9 deletions(-)
> >
> > diff --git
> >
> a/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
> >
> b/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
> > index dd092a53..902ec4fe 100644
> > ---
> >
> a/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
> > +++
> >
> b/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
> > @@ -20,6 +20,7 @@ import java.util.HashMap;
> > import java.util.Locale;
> > import java.util.Map;
> > import java.util.concurrent.ConcurrentLinkedQueue;
> > +import java.util.concurrent.atomic.AtomicReference;
> >
> > import org.apache.johnzon.core.util.ClassUtil;
> >
> > @@ -85,30 +86,36 @@ public class BufferStrategyFactory {
> >         protected char[] newInstance(int size) {
> >             return new char[size];
> >         }
> > -
> > -        @Override
> > -        public void release(final char[] value) {
> > -            // no-op
> > -        }
> >     }
> >
> > +    // the buffer is handed out to one user at a time, other users get a
> > new one.
> > +    // Sharing it would let users holding buffers at the same time
> > overwrite each other's content, e.g. two
> > +    // parsers open at once, or one parser whose value spans several
> value
> > buffers.
> >     private static abstract class SingletonProvider<T> implements
> > BufferStrategy.BufferProvider<T> {
> > -        protected final T buffer;
> > +        private final int size;
> > +        private final T buffer;
> > +        // holds the buffer while it is not in use
> > +        private final AtomicReference<T> available;
> >
> >         public SingletonProvider(final int size) {
> > -            buffer = newInstance(size);
> > +            this.size = size;
> > +            this.buffer = newInstance(size);
> > +            this.available = new AtomicReference<>(buffer);
> >         }
> >
> >         protected abstract T newInstance(int size);
> >
> >         @Override
> >         public T newBuffer() {
> > -            return buffer;
> > +            final T value = available.getAndSet(null);
> > +            return value != null ? value : newInstance(size);
> >         }
> >
> >         @Override
> >         public void release(final T value) {
> > -            // no-op
> > +            if (value == buffer) {
> > +                available.set(buffer);
> > +            }
> >         }
> >     }
> >
> > diff --git
> >
> a/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
> >
> b/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
> > index f1226972..4b523728 100644
> > ---
> >
> a/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
> > +++
> >
> b/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
> > @@ -16,17 +16,22 @@
> >  */
> > package org.apache.johnzon.core;
> >
> > +import java.io.StringReader;
> > import java.io.StringWriter;
> > import java.util.HashMap;
> > import java.util.concurrent.atomic.AtomicInteger;
> >
> > import javax.json.spi.JsonProvider;
> > import javax.json.stream.JsonGenerator;
> > +import javax.json.stream.JsonParser;
> > +import javax.json.stream.JsonParserFactory;
> >
> > import org.junit.Test;
> >
> > import static org.junit.Assert.assertEquals;
> > import static org.junit.Assert.assertNotNull;
> > +import static org.junit.Assert.assertNotSame;
> > +import static org.junit.Assert.assertSame;
> > import static org.junit.Assert.assertTrue;
> >
> > public class BufferStrategyFactoryTest {
> > @@ -71,6 +76,39 @@ public class BufferStrategyFactoryTest {
> >         assertEquals("{\"age\":27,\"name\":\"karl\"}", sw.toString());
> >     }
> >
> > +    @Test
> > +    public void singletonHandsOutBufferOnce() {
> > +        final BufferStrategy.BufferProvider<char[]> provider =
> > BufferStrategyFactory.valueOf("SINGLETON").newCharProvider(4);
> > +        final char[] first = provider.newBuffer();
> > +        final char[] second = provider.newBuffer();
> > +        assertNotSame(first, second);
> > +
> > +        provider.release(second);
> > +        assertNotSame(first, provider.newBuffer());
> > +
> > +        provider.release(first);
> > +        assertSame(first, provider.newBuffer());
> > +    }
> > +
> > +    @Test
> > +    public void singletonParsersOpenAtSameTime() {
> > +        final JsonParserFactory factory =
> > JsonProvider.provider().createParserFactory(new HashMap<String,
> Object>() {{
> > +            put(AbstractJsonFactory.BUFFER_STRATEGY, "SINGLETON");
> > +            put(JsonParserFactoryImpl.BUFFER_LENGTH, 2);
> > +        }});
> > +        try (JsonParser first = factory.createParser(new
> > StringReader("[\"abcdef\",123456]"));
> > +             JsonParser second = factory.createParser(new
> > StringReader("[\"uvwxyz\",987654]"))) {
> > +            for (int i = 0; i < 2; i++) {
> > +                assertEquals(first.next(), second.next());
> > +            }
> > +            assertEquals("abcdef", first.getString());
> > +            assertEquals("uvwxyz", second.getString());
> > +            assertEquals(first.next(), second.next());
> > +            assertEquals(123456, first.getInt());
> > +            assertEquals(987654, second.getInt());
> > +        }
> > +    }
> > +
> >     private void verify(Object bufferStrategy) {
> >         assertNotNull(bufferStrategy);
> >         assertTrue(bufferStrategy instanceof BufferStrategy);
>
>

Reply via email to