From 0e1f3f771740cccbd439c220ae6978a168698d1e Mon Sep 17 00:00:00 2001 From: gibkim <86421130+gibkim@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:55:31 +0900 Subject: [PATCH] Avoid empty annotation array allocation when cloning Ion values Signed-off-by: gibkim <86421130+gibkim@users.noreply.github.com> --- .../amazon/ion/IonValueCloneBenchmark.java | 79 +++++++++++++++++ .../amazon/ion/impl/lite/IonValueLite.java | 36 ++++---- .../ion/impl/lite/IonValueLiteCloneTest.java | 87 +++++++++++++++++++ 3 files changed, 185 insertions(+), 17 deletions(-) create mode 100644 src/jmh/java/com/amazon/ion/IonValueCloneBenchmark.java create mode 100644 src/test/java/com/amazon/ion/impl/lite/IonValueLiteCloneTest.java diff --git a/src/jmh/java/com/amazon/ion/IonValueCloneBenchmark.java b/src/jmh/java/com/amazon/ion/IonValueCloneBenchmark.java new file mode 100644 index 0000000000..98f8bc70d0 --- /dev/null +++ b/src/jmh/java/com/amazon/ion/IonValueCloneBenchmark.java @@ -0,0 +1,79 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.amazon.ion; + +import com.amazon.ion.system.IonSystemBuilder; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +/** + * Clones values after different annotation histories, including retained capacity after removals. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Fork(2) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@State(Scope.Thread) +public class IonValueCloneBenchmark { + + @Param({"UNINITIALIZED", "EMPTY_ARRAY", "CLEARED", "REMOVED", "ANNOTATED", "ANNOTATED_SINGLE", "SPARSE"}) + public String annotations; + + private IonValue value; + + @Setup + public void setup() { + value = IonSystemBuilder.standard().build().newInt(1); + switch (annotations) { + case "EMPTY_ARRAY": + value.setTypeAnnotationSymbols(); + break; + case "CLEARED": + value.addTypeAnnotation("first"); + value.addTypeAnnotation("second"); + value.addTypeAnnotation("third"); + value.clearTypeAnnotations(); + break; + case "REMOVED": + value.addTypeAnnotation("first"); + value.removeTypeAnnotation("first"); + break; + case "ANNOTATED": + value.addTypeAnnotation("first"); + value.addTypeAnnotation("second"); + value.addTypeAnnotation("third"); + break; + case "ANNOTATED_SINGLE": + value.addTypeAnnotation("first"); + break; + case "SPARSE": + for (int i = 0; i < 1024; i++) { + value.addTypeAnnotation("annotation" + i); + } + for (int i = 0; i < 1023; i++) { + value.removeTypeAnnotation("annotation" + i); + } + break; + case "UNINITIALIZED": + break; + default: + throw new IllegalArgumentException(annotations); + } + } + + @Benchmark + public IonValue cloneValue() { + return value.clone(); + } +} diff --git a/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java b/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java index d17de5cf9a..a2b42515d3 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java @@ -302,24 +302,26 @@ public SymbolTable getSymbolTable() this._annotations = null; } else { int size = existing._annotations.length; - this._annotations = new SymbolToken[size]; - for (int i = 0; i < size; i++) { - SymbolToken existingToken = existing._annotations[i]; - if (existingToken == null) { - // The annotation array grows by doubling; null indicates the actual end. - break; - } - String text = existingToken.getText(); - int sid = existingToken.getSid(); - if (text == null || sid == UNKNOWN_SYMBOL_ID) { - // TODO - amazon-ion/ion-java/issues/223 needs consistent handling, should attempt to resolve and if it cant; fail - _annotations[i] = existingToken; - if (sid > UNKNOWN_SYMBOL_ID) { - _flags |= IS_SYMBOL_ID_PRESENT; + // Cleared arrays can retain capacity without containing any annotations. + SymbolToken existingToken = size == 0 ? null : existing._annotations[0]; + if (existingToken == null) { + this._annotations = SymbolToken.EMPTY_ARRAY; + } else { + this._annotations = new SymbolToken[size]; + int i = 0; + do { + String text = existingToken.getText(); + int sid = existingToken.getSid(); + if (text == null || sid == UNKNOWN_SYMBOL_ID) { + // TODO - amazon-ion/ion-java/issues/223 needs consistent handling, should attempt to resolve and if it cant; fail + _annotations[i] = existingToken; + if (sid > UNKNOWN_SYMBOL_ID) { + _flags |= IS_SYMBOL_ID_PRESENT; + } + } else { + this._annotations[i] = _Private_Utils.newSymbolToken(text, UNKNOWN_SYMBOL_ID); } - } else { - this._annotations[i] = _Private_Utils.newSymbolToken(text, UNKNOWN_SYMBOL_ID); - } + } while (++i < size && (existingToken = existing._annotations[i]) != null); } } // Note: the field name is copied in IonStruct.clone. diff --git a/src/test/java/com/amazon/ion/impl/lite/IonValueLiteCloneTest.java b/src/test/java/com/amazon/ion/impl/lite/IonValueLiteCloneTest.java new file mode 100644 index 0000000000..6fb9875866 --- /dev/null +++ b/src/test/java/com/amazon/ion/impl/lite/IonValueLiteCloneTest.java @@ -0,0 +1,87 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.amazon.ion.impl.lite; + +import com.amazon.ion.IonTestCase; +import com.amazon.ion.SymbolToken; +import com.amazon.ion.impl._Private_Utils; +import org.junit.Test; + +public class IonValueLiteCloneTest extends IonTestCase { + + @Test + public void cloneWithEmptyAnnotationArray() { + IonValueLite value = (IonValueLite) system().newInt(1); + value.setTypeAnnotationSymbols(); + assertSame(SymbolToken.EMPTY_ARRAY, value._annotations); + assertEmptyClone(value); + } + + @Test + public void cloneAfterClearingAnnotations() { + IonValueLite value = (IonValueLite) system().newInt(1); + value.addTypeAnnotation("first"); + value.addTypeAnnotation("second"); + value.addTypeAnnotation("third"); + value.clearTypeAnnotations(); + assertTrue(value._annotations.length > 0); + assertEmptyClone(value); + } + + @Test + public void cloneAfterRemovingLastAnnotation() { + IonValueLite value = (IonValueLite) system().newInt(1); + value.addTypeAnnotation("first"); + value.removeTypeAnnotation("first"); + assertTrue(value._annotations.length > 0); + assertEmptyClone(value); + } + + @Test + public void cloneWithoutAnnotationArray() { + IonValueLite value = (IonValueLite) system().newInt(1); + IonValueLite clone = (IonValueLite) value.clone(); + assertNull(value._annotations); + assertNull(clone._annotations); + clone.addTypeAnnotation("clone"); + assertFalse(value.hasTypeAnnotation("clone")); + } + + @Test + public void cloneWithAnnotationsDoesNotShareArray() { + IonValueLite value = (IonValueLite) system().newInt(1); + value.addTypeAnnotation("first"); + value.addTypeAnnotation("second"); + value.addTypeAnnotation("third"); + IonValueLite clone = (IonValueLite) value.clone(); + assertNotSame(value._annotations, clone._annotations); + assertArrayEquals(value.getTypeAnnotations(), clone.getTypeAnnotations()); + clone.removeTypeAnnotation("second"); + value.addTypeAnnotation("original"); + assertTrue(value.hasTypeAnnotation("second")); + assertFalse(clone.hasTypeAnnotation("original")); + } + + @Test + public void cloneStopsAtFirstNullAnnotation() { + IonValueLite value = (IonValueLite) system().newInt(1); + value.setTypeAnnotationSymbols( + _Private_Utils.newSymbolToken("first", -1), + null, + _Private_Utils.newSymbolToken("ignored", -1)); + IonValueLite clone = (IonValueLite) value.clone(); + clone.addTypeAnnotation("second"); + assertArrayEquals(new String[] {"first", "second"}, clone.getTypeAnnotations()); + assertArrayEquals(new String[] {"first"}, value.getTypeAnnotations()); + } + + private void assertEmptyClone(IonValueLite value) { + IonValueLite clone = (IonValueLite) value.clone(); + assertSame(SymbolToken.EMPTY_ARRAY, clone._annotations); + assertEquals(0, clone.getTypeAnnotations().length); + clone.addTypeAnnotation("clone"); + value.addTypeAnnotation("original"); + assertFalse(value.hasTypeAnnotation("clone")); + assertFalse(clone.hasTypeAnnotation("original")); + } +}