From b00a97700c70abf0d64470e27c28872a84a59fe8 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Mon, 24 Nov 2025 20:10:34 +0300 Subject: [PATCH 01/16] IGNITE-26860 Add configOnly flag to dump --- .../jmh/binary/JmhMapSerdesBenchmark.java | 193 ++++++++++++++++++ .../ignite/internal/binary/BinaryUtils.java | 15 +- .../internal/binary/BinaryWriterEx.java | 8 +- .../internal/binary/BinaryWriterExImpl.java | 29 ++- .../internal/binary/GridBinaryMarshaller.java | 4 +- .../builder/BinaryObjectBuilderImpl.java | 10 +- .../binary/BinaryMarshallerSelfTest.java | 3 +- .../binary/RawBinaryObjectExtractorTest.java | 4 +- 8 files changed, 231 insertions(+), 35 deletions(-) create mode 100644 modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java new file mode 100644 index 0000000000000..79304a5d224eb --- /dev/null +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.benchmarks.jmh.binary; + +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.Ignition; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.binary.BinaryContext; +import org.apache.ignite.internal.binary.BinaryUtils; +import org.apache.ignite.internal.binary.BinaryWriterEx; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryStreams; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.openjdk.jmh.annotations.Mode.AverageTime; +import static org.openjdk.jmh.annotations.Scope.Thread; + +/** */ +@State(Thread) +@OutputTimeUnit(NANOSECONDS) +@BenchmarkMode(AverageTime) +@Warmup(iterations = 5) +@Measurement(iterations = 5, time = 30, timeUnit = SECONDS) +public class JmhMapSerdesBenchmark { + /** */ + @Param({/*"Integer", "Date", "String",*/ "Object"}) + private String key; + + /** */ + @Param({/*"Integer", "Date", "String",*/ "Object"}) + private String value; + + /** */ + @Param({"10", /*"100", "1000", "10000"*/}) + private String size; + + /** */ + @Param({"HashMap", "ConcurrentHashMap", "LinkedHashMap"}) + private String mapType; + + /** */ + private BinaryContext bctx; + + /** */ + private BinaryOutputStream out; + + /** */ + private Map data; + + /** */ + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(new String[]{JmhMapSerdesBenchmark.class.getName()}); + } + + /** */ + @Setup + public void setup() throws Exception { + IgniteEx node = (IgniteEx)Ignition.start(new IgniteConfiguration()); + + bctx = node.context().cacheObjects().binaryContext(); + out = BinaryStreams.outputStream((int)(100 * U.MB)); + + Ignition.stopAll(false); + + if ("HashMap".equals(mapType)) + data = new HashMap<>(); + else if ("ConcurrentHashMap".equals(mapType)) + data = new ConcurrentHashMap<>(); + else if ("LinkedHashMap".equals(mapType)) + data = new LinkedHashMap<>(); + else + throw new IllegalArgumentException("Unknown map type: " + mapType); + + int sz = Integer.parseInt(size); + for (int i = 0; i < sz; i++) + data.put(produce(i, key), produce(i, value)); + } + + /** */ + public Object produce(int i, String type) { + if ("Integer".equals(type)) + return i; + else if ("Date".equals(type)) + return new Date(System.currentTimeMillis() + i); + else if ("String".equals(type)) + return "" + i; + else if ("Object".equals(type)) { + return new Employee("Name" + i, 1000L * i, new Date()); + } + + throw new IllegalArgumentException("Unknown type: " + type); + } + + /** */ + @Benchmark + public void mapSerialization(Blackhole bh) { + BinaryWriterEx writer = BinaryUtils.writer(bctx, out); + + writer.writeMap(data); + + out.position(0); + } + + /** */ + public static class Employee { + /** */ + private String fio; + + /** */ + private long salary; + + /** */ + private Date created; + + /** */ + public Employee(String fio, long salary, Date created) { + this.fio = fio; + this.salary = salary; + this.created = created; + } + + /** */ + public Employee() { + } + + /** */ + public Employee(String fio, long salary) { + this.fio = fio; + this.salary = salary; + } + + /** */ + public String getFio() { + return fio; + } + + /** */ + public void setFio(String fio) { + this.fio = fio; + } + + /** */ + public long getSalary() { + return salary; + } + + /** */ + public void setSalary(long salary) { + this.salary = salary; + } + + /** */ + public Date getCreated() { + return created; + } + + /** */ + public void setCreated(Date created) { + this.created = created; + } + } +} diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index 690e6c91f303d..d629eeae7ff9b 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -88,6 +88,7 @@ import static org.apache.ignite.IgniteCommonsSystemProperties.DFLT_IGNITE_USE_BINARY_ARRAYS; import static org.apache.ignite.IgniteCommonsSystemProperties.IGNITE_BINARY_MARSHALLER_USE_STRING_SERIALIZATION_VER_2; import static org.apache.ignite.IgniteCommonsSystemProperties.IGNITE_USE_BINARY_ARRAYS; +import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; import static org.apache.ignite.internal.util.GridUnsafe.align; /** @@ -2923,10 +2924,16 @@ static BinaryReaderEx reader(BinaryContext ctx, * @param ctx Context. * @return Writer instance. */ - public static BinaryWriterEx writer(BinaryContext ctx) { + public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) { BinaryThreadLocalContext locCtx = BinaryThreadLocalContext.get(); - return new BinaryWriterExImpl(ctx, BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()), locCtx.schemaHolder(), null); + return new BinaryWriterExImpl( + ctx, + BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()), + locCtx.schemaHolder(), + null, + failIfUnregistered + ); } /** @@ -2935,7 +2942,7 @@ public static BinaryWriterEx writer(BinaryContext ctx) { * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { - return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null); + return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, DFLT_FAIL_IF_UNREGISTERED); } /** @@ -2944,7 +2951,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema) { - return new BinaryWriterExImpl(ctx, out, schema, null); + return new BinaryWriterExImpl(ctx, out, schema, null, DFLT_FAIL_IF_UNREGISTERED); } /** @return Instance of caching handler. */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java index fdd8882b306f3..d980422ca72a0 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java @@ -29,6 +29,9 @@ * Extended writer interface. */ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOutput { + /** Default value for {@link #failIfUnregistered()}. */ + public static final boolean DFLT_FAIL_IF_UNREGISTERED = false; + /** * @param obj Object to write. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. @@ -66,11 +69,6 @@ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOut */ public boolean failIfUnregistered(); - /** - * @param failIfUnregistered Fail if unregistered. - */ - public void failIfUnregistered(boolean failIfUnregistered); - /** * @param obj Object. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index 4ab8eadac6091..ed1b092e9ad64 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -83,18 +83,26 @@ class BinaryWriterExImpl implements BinaryWriterEx { private BinaryInternalMapper mapper; /** */ - private boolean failIfUnregistered; + private final boolean failIfUnregistered; /** * @param ctx Context. * @param out Output stream. * @param handles Handles. + * @param failIfUnregistered Flag to fail while writing object of unregistered type. */ - public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema, BinaryWriterHandles handles) { + public BinaryWriterExImpl( + BinaryContext ctx, + BinaryOutputStream out, + BinaryWriterSchemaHolder schema, + BinaryWriterHandles handles, + boolean failIfUnregistered + ) { this.ctx = ctx; this.out = out; this.schema = schema; this.handles = handles; + this.failIfUnregistered = failIfUnregistered; start = out.position(); } @@ -104,11 +112,6 @@ public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWrite return failIfUnregistered; } - /** {@inheritDoc} */ - @Override public void failIfUnregistered(boolean failIfUnregistered) { - this.failIfUnregistered = failIfUnregistered; - } - /** {@inheritDoc} */ @Override public void typeId(int typeId) { this.typeId = typeId; @@ -844,9 +847,7 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { - BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles()); - - writer.failIfUnregistered(failIfUnregistered); + BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered); writer.marshal(obj); } @@ -857,9 +858,7 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { - BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null); - - writer.failIfUnregistered(failIfUnregistered); + BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null, failIfUnregistered); writer.marshal(obj); } @@ -1538,9 +1537,7 @@ boolean tryWriteAsHandle(Object obj) { /** {@inheritDoc} */ @Override public BinaryWriterEx newWriter(int typeId) { - BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles()); - - res.failIfUnregistered(failIfUnregistered); + BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered); res.typeId(typeId); diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java index 311c629b5535e..eed925e070db6 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java @@ -250,9 +250,7 @@ public byte[] marshal(@Nullable Object obj, boolean failIfUnregistered) throws B if (obj == null) return new byte[] { NULL }; - try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) { - writer.failIfUnregistered(failIfUnregistered); - + try (BinaryWriterEx writer = BinaryUtils.writer(ctx, failIfUnregistered)) { writer.marshal(obj); return writer.array(); diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java index 1993d1b01c151..3eca70ca1f6cd 100644 --- a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java @@ -172,12 +172,12 @@ public BinaryObjectBuilderImpl(BinaryObjectEx obj) { /** {@inheritDoc} */ @Override public BinaryObject build() { - try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) { - Thread curThread = Thread.currentThread(); - - if (curThread instanceof IgniteThread) - writer.failIfUnregistered(((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata()); + Thread curThread = Thread.currentThread(); + try (BinaryWriterEx writer = BinaryUtils.writer( + ctx, + curThread instanceof IgniteThread && ((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata()) + ) { writer.typeId(typeId); BinaryBuilderSerializer serializationCtx = new BinaryBuilderSerializer(); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java index 1423f520b5bc7..cfe47a5e33801 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java @@ -110,6 +110,7 @@ import org.junit.Test; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotEquals; @@ -3019,7 +3020,7 @@ public void testThreadLocalArrayReleased() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); - try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh))) { + try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), DFLT_FAIL_IF_UNREGISTERED)) { assertEquals(true, BinaryStreamsTestUtils.threadLocalIsAcquired()); writer.writeString("Thread local test"); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java index 1f4fe14c8fac9..92af46aed4554 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java @@ -38,6 +38,8 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.junit.Test; +import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; + /** */ public class RawBinaryObjectExtractorTest extends GridCommonAbstractTest { /** */ @@ -49,7 +51,7 @@ public void test() throws Exception { byte[] serializedTestObjectsBytes; - try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) { + try (BinaryWriterEx writer = BinaryUtils.writer(ctx, DFLT_FAIL_IF_UNREGISTERED)) { testObjects.forEach(writer::writeObject); serializedTestObjectsBytes = writer.array(); From 69b4428216ac32bc9c68a688b39912a47f6faef7 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Mon, 24 Nov 2025 20:12:33 +0300 Subject: [PATCH 02/16] IGNITE-27155 BinaryWriterExImpl#failIfUnregistered can be final --- .../jmh/binary/JmhMapSerdesBenchmark.java | 193 ------------------ 1 file changed, 193 deletions(-) delete mode 100644 modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java deleted file mode 100644 index 79304a5d224eb..0000000000000 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.benchmarks.jmh.binary; - -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import org.apache.ignite.Ignition; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.internal.IgniteEx; -import org.apache.ignite.internal.binary.BinaryContext; -import org.apache.ignite.internal.binary.BinaryUtils; -import org.apache.ignite.internal.binary.BinaryWriterEx; -import org.apache.ignite.internal.binary.streams.BinaryOutputStream; -import org.apache.ignite.internal.binary.streams.BinaryStreams; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Param; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; - -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.openjdk.jmh.annotations.Mode.AverageTime; -import static org.openjdk.jmh.annotations.Scope.Thread; - -/** */ -@State(Thread) -@OutputTimeUnit(NANOSECONDS) -@BenchmarkMode(AverageTime) -@Warmup(iterations = 5) -@Measurement(iterations = 5, time = 30, timeUnit = SECONDS) -public class JmhMapSerdesBenchmark { - /** */ - @Param({/*"Integer", "Date", "String",*/ "Object"}) - private String key; - - /** */ - @Param({/*"Integer", "Date", "String",*/ "Object"}) - private String value; - - /** */ - @Param({"10", /*"100", "1000", "10000"*/}) - private String size; - - /** */ - @Param({"HashMap", "ConcurrentHashMap", "LinkedHashMap"}) - private String mapType; - - /** */ - private BinaryContext bctx; - - /** */ - private BinaryOutputStream out; - - /** */ - private Map data; - - /** */ - public static void main(String[] args) throws Exception { - org.openjdk.jmh.Main.main(new String[]{JmhMapSerdesBenchmark.class.getName()}); - } - - /** */ - @Setup - public void setup() throws Exception { - IgniteEx node = (IgniteEx)Ignition.start(new IgniteConfiguration()); - - bctx = node.context().cacheObjects().binaryContext(); - out = BinaryStreams.outputStream((int)(100 * U.MB)); - - Ignition.stopAll(false); - - if ("HashMap".equals(mapType)) - data = new HashMap<>(); - else if ("ConcurrentHashMap".equals(mapType)) - data = new ConcurrentHashMap<>(); - else if ("LinkedHashMap".equals(mapType)) - data = new LinkedHashMap<>(); - else - throw new IllegalArgumentException("Unknown map type: " + mapType); - - int sz = Integer.parseInt(size); - for (int i = 0; i < sz; i++) - data.put(produce(i, key), produce(i, value)); - } - - /** */ - public Object produce(int i, String type) { - if ("Integer".equals(type)) - return i; - else if ("Date".equals(type)) - return new Date(System.currentTimeMillis() + i); - else if ("String".equals(type)) - return "" + i; - else if ("Object".equals(type)) { - return new Employee("Name" + i, 1000L * i, new Date()); - } - - throw new IllegalArgumentException("Unknown type: " + type); - } - - /** */ - @Benchmark - public void mapSerialization(Blackhole bh) { - BinaryWriterEx writer = BinaryUtils.writer(bctx, out); - - writer.writeMap(data); - - out.position(0); - } - - /** */ - public static class Employee { - /** */ - private String fio; - - /** */ - private long salary; - - /** */ - private Date created; - - /** */ - public Employee(String fio, long salary, Date created) { - this.fio = fio; - this.salary = salary; - this.created = created; - } - - /** */ - public Employee() { - } - - /** */ - public Employee(String fio, long salary) { - this.fio = fio; - this.salary = salary; - } - - /** */ - public String getFio() { - return fio; - } - - /** */ - public void setFio(String fio) { - this.fio = fio; - } - - /** */ - public long getSalary() { - return salary; - } - - /** */ - public void setSalary(long salary) { - this.salary = salary; - } - - /** */ - public Date getCreated() { - return created; - } - - /** */ - public void setCreated(Date created) { - this.created = created; - } - } -} From c4f5a0af7390b9489558dfb87be5a995295a784f Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Mon, 24 Nov 2025 21:24:27 +0300 Subject: [PATCH 03/16] IGNITE-27155 BinaryWriterExImpl#failIfUnregistered can be final --- .../java/org/apache/ignite/internal/binary/BinaryUtils.java | 6 +++--- .../org/apache/ignite/internal/binary/BinaryWriterEx.java | 3 --- .../ignite/internal/binary/BinaryMarshallerSelfTest.java | 3 +-- .../internal/binary/RawBinaryObjectExtractorTest.java | 4 +--- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index d629eeae7ff9b..4ce858fca0e34 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -88,7 +88,6 @@ import static org.apache.ignite.IgniteCommonsSystemProperties.DFLT_IGNITE_USE_BINARY_ARRAYS; import static org.apache.ignite.IgniteCommonsSystemProperties.IGNITE_BINARY_MARSHALLER_USE_STRING_SERIALIZATION_VER_2; import static org.apache.ignite.IgniteCommonsSystemProperties.IGNITE_USE_BINARY_ARRAYS; -import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; import static org.apache.ignite.internal.util.GridUnsafe.align; /** @@ -2922,6 +2921,7 @@ static BinaryReaderEx reader(BinaryContext ctx, /** * @param ctx Context. + * @param failIfUnregistered Flag to fail while writing object of unregistered type. * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) { @@ -2942,7 +2942,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistere * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { - return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, DFLT_FAIL_IF_UNREGISTERED); + return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, false); } /** @@ -2951,7 +2951,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema) { - return new BinaryWriterExImpl(ctx, out, schema, null, DFLT_FAIL_IF_UNREGISTERED); + return new BinaryWriterExImpl(ctx, out, schema, null, false); } /** @return Instance of caching handler. */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java index d980422ca72a0..5c73676c6bf28 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java @@ -29,9 +29,6 @@ * Extended writer interface. */ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOutput { - /** Default value for {@link #failIfUnregistered()}. */ - public static final boolean DFLT_FAIL_IF_UNREGISTERED = false; - /** * @param obj Object to write. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java index cfe47a5e33801..955826b8f3fa4 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java @@ -110,7 +110,6 @@ import org.junit.Test; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotEquals; @@ -3020,7 +3019,7 @@ public void testThreadLocalArrayReleased() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); - try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), DFLT_FAIL_IF_UNREGISTERED)) { + try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), false)) { assertEquals(true, BinaryStreamsTestUtils.threadLocalIsAcquired()); writer.writeString("Thread local test"); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java index 92af46aed4554..3cf2d7b570f3b 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java @@ -38,8 +38,6 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.junit.Test; -import static org.apache.ignite.internal.binary.BinaryWriterEx.DFLT_FAIL_IF_UNREGISTERED; - /** */ public class RawBinaryObjectExtractorTest extends GridCommonAbstractTest { /** */ @@ -51,7 +49,7 @@ public void test() throws Exception { byte[] serializedTestObjectsBytes; - try (BinaryWriterEx writer = BinaryUtils.writer(ctx, DFLT_FAIL_IF_UNREGISTERED)) { + try (BinaryWriterEx writer = BinaryUtils.writer(ctx, false)) { testObjects.forEach(writer::writeObject); serializedTestObjectsBytes = writer.array(); From 0740c1f42ecb982a8a1842b771d483e7d4ad7f8e Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 25 Nov 2025 14:28:00 +0300 Subject: [PATCH 04/16] IGNITE-27086 Omit creation of extra BinaryWriterExImpl instances --- .../internal/binary/BinaryWriterExImpl.java | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index ed1b092e9ad64..8f9a9f00f7d13 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -61,11 +61,14 @@ class BinaryWriterExImpl implements BinaryWriterEx { /** Schema. */ private final BinaryWriterSchemaHolder schema; + /** */ + private final boolean failIfUnregistered; + /** */ private int typeId; /** */ - private final int start; + private int start; /** Raw offset position. */ private int rawOffPos; @@ -82,9 +85,6 @@ class BinaryWriterExImpl implements BinaryWriterEx { /** */ private BinaryInternalMapper mapper; - /** */ - private final boolean failIfUnregistered; - /** * @param ctx Context. * @param out Output stream. @@ -132,7 +132,7 @@ public BinaryWriterExImpl( * @param enableReplace Object replacing enabled flag. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ - void marshal(Object obj, boolean enableReplace) throws BinaryObjectException { + private void marshal(Object obj, boolean enableReplace) throws BinaryObjectException { String newName = ctx.igniteInstanceName(); String oldName = CommonUtils.setCurrentIgniteName(newName); @@ -847,9 +847,29 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { - BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered); - - writer.marshal(obj); + int typeId0 = this.typeId; + int start0 = this.start; + int rawOffPos0 = this.rawOffPos; + int schemaId0 = this.schemaId; + int fieldCnt0 = this.fieldCnt; + BinaryInternalMapper mapper0 = this.mapper; + + this.typeId = 0; + this.start = out.position(); + this.rawOffPos = 0; + this.schemaId = BinaryUtils.schemaInitialId(); + this.fieldCnt = 0; + this.mapper = null; + // Handles not cleared, because, in this mode it shared down to hierarchy. + + marshal(obj); + + this.typeId = typeId0; + this.start = start0; + this.rawOffPos = rawOffPos0; + this.schemaId = schemaId0; + this.fieldCnt = fieldCnt0; + this.mapper = mapper0; } } @@ -858,9 +878,32 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { - BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null, failIfUnregistered); - - writer.marshal(obj); + int typeId0 = this.typeId; + int start0 = this.start; + int rawOffPos0 = this.rawOffPos; + int schemaId0 = this.schemaId; + int fieldCnt0 = this.fieldCnt; + BinaryInternalMapper mapper0 = this.mapper; + BinaryWriterHandles handles0 = this.handles; + + this.typeId = 0; + this.start = out.position(); + this.rawOffPos = 0; + this.schemaId = BinaryUtils.schemaInitialId(); + this.fieldCnt = 0; + this.mapper = null; + // Handles cleared, because, in this mode it NOT shared down to hierarchy. + this.handles = null; + + marshal(obj); + + this.typeId = typeId0; + this.start = start0; + this.rawOffPos = rawOffPos0; + this.schemaId = schemaId0; + this.fieldCnt = fieldCnt0; + this.mapper = mapper0; + this.handles = handles0; } } From 66cae9dffdd16d9010b4f93fbfc563f0ea560ef6 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 25 Nov 2025 14:42:28 +0300 Subject: [PATCH 05/16] IGNITE-27086 Omit creation of extra BinaryWriterExImpl instances --- .../jmh/binary/JmhMapSerdesBenchmark.java | 199 ++++++++++++++++++ .../internal/binary/BinaryWriterExImpl.java | 6 +- 2 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java new file mode 100644 index 0000000000000..a3215fcf30eb6 --- /dev/null +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.benchmarks.jmh.binary; + +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.Ignition; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.binary.BinaryContext; +import org.apache.ignite.internal.binary.BinaryUtils; +import org.apache.ignite.internal.binary.BinaryWriterEx; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryStreams; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.openjdk.jmh.annotations.Mode.AverageTime; +import static org.openjdk.jmh.annotations.Scope.Thread; + +/** */ +@State(Thread) +@OutputTimeUnit(NANOSECONDS) +@BenchmarkMode(AverageTime) +@Warmup(iterations = 3, time = 30, timeUnit = SECONDS) +@Measurement(iterations = 3, time = 60, timeUnit = SECONDS) +public class JmhMapSerdesBenchmark { + /** */ + @Param({/*"Integer", "Date", "String",*/ "Object"}) + private String key; + + /** */ + @Param({/*"Integer", "Date", "String",*/ "Object"}) + private String value; + + /** */ + @Param({/* "10", "100", */ "1000"/*, "10000"*/}) + private String size; + + /** */ + @Param({"HashMap"/*, "ConcurrentHashMap", "LinkedHashMap"*/}) + private String mapType; + + /** */ + private BinaryContext bctx; + + /** */ + private BinaryOutputStream out; + + /** */ + private BinaryWriterEx writer; + + /** */ + private Map data; + + /** */ + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(new String[]{JmhMapSerdesBenchmark.class.getName()}); + } + + /** */ + @Setup + public void setup() throws Exception { + IgniteEx node = (IgniteEx)Ignition.start(new IgniteConfiguration()); + + bctx = node.context().cacheObjects().binaryContext(); + out = BinaryStreams.outputStream((int)(100 * U.MB)); + writer = BinaryUtils.writer(bctx, out); + + Ignition.stopAll(false); + + if ("HashMap".equals(mapType)) + data = new HashMap<>(); + else if ("ConcurrentHashMap".equals(mapType)) + data = new ConcurrentHashMap<>(); + else if ("LinkedHashMap".equals(mapType)) + data = new LinkedHashMap<>(); + else + throw new IllegalArgumentException("Unknown map type: " + mapType); + + int sz = Integer.parseInt(size); + for (int i = 0; i < sz; i++) + data.put(produce(i, key), produce(i, value)); + } + + /** */ + public Object produce(int i, String type) { + if ("Integer".equals(type)) + return i; + else if ("Date".equals(type)) + return new Date(System.currentTimeMillis() + i); + else if ("String".equals(type)) + return "" + i; + else if ("Object".equals(type)) { + return new Employee("Name" + i, 1000L * i, new Date()); + } + + throw new IllegalArgumentException("Unknown type: " + type); + } + + /** */ + @Benchmark + public void mapSerialization(Blackhole bh) { + BinaryWriterEx writer = BinaryUtils.writer(bctx, out); + + writer.writeMap(data); + + out.position(0); + + bh.consume(writer); + } + + /** */ + public static class Employee { + /** */ + private String fio; + + /** */ + private long salary; + + /** */ + private Date created; + + /** */ + public Employee(String fio, long salary, Date created) { + this.fio = fio; + this.salary = salary; + this.created = created; + } + + /** */ + public Employee() { + } + + /** */ + public Employee(String fio, long salary) { + this.fio = fio; + this.salary = salary; + } + + /** */ + public String getFio() { + return fio; + } + + /** */ + public void setFio(String fio) { + this.fio = fio; + } + + /** */ + public long getSalary() { + return salary; + } + + /** */ + public void setSalary(long salary) { + this.salary = salary; + } + + /** */ + public Date getCreated() { + return created; + } + + /** */ + public void setCreated(Date created) { + this.created = created; + } + } +} diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index 8f9a9f00f7d13..212e0fbff928c 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -73,15 +73,15 @@ class BinaryWriterExImpl implements BinaryWriterEx { /** Raw offset position. */ private int rawOffPos; - /** Handles. */ - private BinaryWriterHandles handles; - /** Schema ID. */ private int schemaId = BinaryUtils.schemaInitialId(); /** Amount of written fields. */ private int fieldCnt; + /** Handles. */ + private BinaryWriterHandles handles; + /** */ private BinaryInternalMapper mapper; From 8912799cde95c0d38d483fed5912021f54e23335 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 25 Nov 2025 23:28:03 +0300 Subject: [PATCH 06/16] IGNITE-27086 Omit creation of extra BinaryWriterExImpl instances --- .../jmh/binary/JmhMapSerdesBenchmark.java | 6 +++--- .../ignite/internal/binary/BinaryUtils.java | 17 +++++++++++++---- .../internal/binary/BinaryWriterExImpl.java | 11 +++++------ .../internal/binary/GridBinaryMarshaller.java | 2 +- .../binary/builder/BinaryObjectBuilderImpl.java | 5 ++--- .../binary/BinaryMarshallerSelfTest.java | 5 +++-- .../binary/RawBinaryObjectExtractorTest.java | 4 +++- 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java index a3215fcf30eb6..82b2800cc2ff7 100644 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -51,14 +51,14 @@ @OutputTimeUnit(NANOSECONDS) @BenchmarkMode(AverageTime) @Warmup(iterations = 3, time = 30, timeUnit = SECONDS) -@Measurement(iterations = 3, time = 60, timeUnit = SECONDS) +@Measurement(iterations = 3, time = 30, timeUnit = SECONDS) public class JmhMapSerdesBenchmark { /** */ - @Param({/*"Integer", "Date", "String",*/ "Object"}) + @Param({/*"Integer", "Date",*/ "String"/*, "Object"*/}) private String key; /** */ - @Param({/*"Integer", "Date", "String",*/ "Object"}) + @Param({/*"Integer", "Date",*/ "String"/*, "Object"*/}) private String value; /** */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index 4ce858fca0e34..6b0a71f81a095 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -2922,9 +2922,10 @@ static BinaryReaderEx reader(BinaryContext ctx, /** * @param ctx Context. * @param failIfUnregistered Flag to fail while writing object of unregistered type. + * @param typeId Type id. * @return Writer instance. */ - public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) { + public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered, int typeId) { BinaryThreadLocalContext locCtx = BinaryThreadLocalContext.get(); return new BinaryWriterExImpl( @@ -2932,7 +2933,8 @@ public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistere BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()), locCtx.schemaHolder(), null, - failIfUnregistered + failIfUnregistered, + typeId ); } @@ -2942,7 +2944,14 @@ public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistere * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { - return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, false); + return new BinaryWriterExImpl( + ctx, + out, + BinaryThreadLocalContext.get().schemaHolder(), + null, + false, + GridBinaryMarshaller.UNREGISTERED_TYPE_ID + ); } /** @@ -2951,7 +2960,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) { * @return Writer instance. */ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema) { - return new BinaryWriterExImpl(ctx, out, schema, null, false); + return new BinaryWriterExImpl(ctx, out, schema, null, false, GridBinaryMarshaller.UNREGISTERED_TYPE_ID); } /** @return Instance of caching handler. */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index 212e0fbff928c..ecaa16b37a656 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -90,19 +90,22 @@ class BinaryWriterExImpl implements BinaryWriterEx { * @param out Output stream. * @param handles Handles. * @param failIfUnregistered Flag to fail while writing object of unregistered type. + * @param typeId Type id. */ public BinaryWriterExImpl( BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema, BinaryWriterHandles handles, - boolean failIfUnregistered + boolean failIfUnregistered, + int typeId ) { this.ctx = ctx; this.out = out; this.schema = schema; this.handles = handles; this.failIfUnregistered = failIfUnregistered; + this.typeId = typeId; start = out.position(); } @@ -1580,11 +1583,7 @@ boolean tryWriteAsHandle(Object obj) { /** {@inheritDoc} */ @Override public BinaryWriterEx newWriter(int typeId) { - BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered); - - res.typeId(typeId); - - return res; + return new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered, typeId); } /** {@inheritDoc} */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java index eed925e070db6..b416597d7631a 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java @@ -250,7 +250,7 @@ public byte[] marshal(@Nullable Object obj, boolean failIfUnregistered) throws B if (obj == null) return new byte[] { NULL }; - try (BinaryWriterEx writer = BinaryUtils.writer(ctx, failIfUnregistered)) { + try (BinaryWriterEx writer = BinaryUtils.writer(ctx, failIfUnregistered, UNREGISTERED_TYPE_ID)) { writer.marshal(obj); return writer.array(); diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java index 3eca70ca1f6cd..fd43af92bd379 100644 --- a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java @@ -176,10 +176,9 @@ public BinaryObjectBuilderImpl(BinaryObjectEx obj) { try (BinaryWriterEx writer = BinaryUtils.writer( ctx, - curThread instanceof IgniteThread && ((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata()) + curThread instanceof IgniteThread && ((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata(), + typeId) ) { - writer.typeId(typeId); - BinaryBuilderSerializer serializationCtx = new BinaryBuilderSerializer(); serializationCtx.registerObjectWriting(this, 0); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java index 955826b8f3fa4..fcf19c0e11a44 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java @@ -110,6 +110,7 @@ import org.junit.Test; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.ignite.internal.binary.GridBinaryMarshaller.UNREGISTERED_TYPE_ID; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotEquals; @@ -3019,7 +3020,7 @@ public void testThreadLocalArrayReleased() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); - try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), false)) { + try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), false, UNREGISTERED_TYPE_ID)) { assertEquals(true, BinaryStreamsTestUtils.threadLocalIsAcquired()); writer.writeString("Thread local test"); @@ -3309,7 +3310,7 @@ public void testPredefinedTypeIds() throws Exception { for (Map.Entry entry : map.entrySet()) { int id = entry.getValue(); - if (id == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) + if (id == UNREGISTERED_TYPE_ID) continue; BinaryClassDescriptor desc = bCtx.descriptorForTypeId(false, entry.getValue(), null, true); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java index 3cf2d7b570f3b..364dbdc43dd30 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java @@ -38,6 +38,8 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.junit.Test; +import static org.apache.ignite.internal.binary.GridBinaryMarshaller.UNREGISTERED_TYPE_ID; + /** */ public class RawBinaryObjectExtractorTest extends GridCommonAbstractTest { /** */ @@ -49,7 +51,7 @@ public void test() throws Exception { byte[] serializedTestObjectsBytes; - try (BinaryWriterEx writer = BinaryUtils.writer(ctx, false)) { + try (BinaryWriterEx writer = BinaryUtils.writer(ctx, false, UNREGISTERED_TYPE_ID)) { testObjects.forEach(writer::writeObject); serializedTestObjectsBytes = writer.array(); From bc1fd2130c471ed231fc5b225fac386d4fe64176 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Wed, 26 Nov 2025 09:18:24 +0300 Subject: [PATCH 07/16] IGNITE-27086 Omit creation of extra BinaryWriterExImpl instances --- .../binary/BinaryClassDescriptor.java | 2 -- .../ignite/internal/binary/BinaryUtils.java | 34 +++++++++---------- .../internal/binary/BinaryWriterEx.java | 5 --- .../internal/binary/BinaryWriterExImpl.java | 7 ++-- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java index d8f2c428231ba..a9ca32721a57d 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java @@ -608,8 +608,6 @@ void write(Object obj, BinaryWriterExImpl writer) throws BinaryObjectException { assert writer != null; assert mode != BinaryWriteMode.OPTIMIZED : "OptimizedMarshaller should not be used here: " + cls.getName(); - writer.typeId(typeId); - switch (mode) { case P_BYTE: case BYTE: diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index 6b0a71f81a095..60e6985f20725 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -2859,10 +2859,10 @@ public static BinaryReaderEx reader(BinaryContext ctx, BinaryInputStream in, Cla * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ public static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - BinaryReaderEx reader, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + BinaryReaderEx reader, + boolean forUnmarshal) { return reader(ctx, in, ldr, reader.handles(), forUnmarshal); } @@ -2876,10 +2876,10 @@ public static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - @Nullable BinaryReaderHandles hnds, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + @Nullable BinaryReaderHandles hnds, + boolean forUnmarshal) { return new BinaryReaderExImpl(ctx, in, ldr, hnds, forUnmarshal); } @@ -2893,10 +2893,10 @@ static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ public static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - boolean skipHdrCheck, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + boolean skipHdrCheck, + boolean forUnmarshal) { return reader(ctx, in, ldr, null, skipHdrCheck, forUnmarshal); } @@ -2911,11 +2911,11 @@ public static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - @Nullable BinaryReaderHandles hnds, - boolean skipHdrCheck, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + @Nullable BinaryReaderHandles hnds, + boolean skipHdrCheck, + boolean forUnmarshal) { return new BinaryReaderExImpl(ctx, in, ldr, hnds, skipHdrCheck, forUnmarshal); } diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java index 5c73676c6bf28..3327753c4b4bb 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java @@ -72,11 +72,6 @@ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOut */ void marshal(Object obj) throws BinaryObjectException; - /** - * @param typeId Type ID. - */ - public void typeId(int typeId); - /** * @return Array. */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index ecaa16b37a656..5baf2b3bd12b4 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -115,11 +115,6 @@ public BinaryWriterExImpl( return failIfUnregistered; } - /** {@inheritDoc} */ - @Override public void typeId(int typeId) { - this.typeId = typeId; - } - /** {@inheritDoc} */ @Override public void close() { out.close(); @@ -207,6 +202,8 @@ private void marshal0(Object obj, boolean enableReplace) throws BinaryObjectExce return; } + this.typeId = desc.typeId(); + desc.write(obj, this); } From ffa74151c225f09d7bf8ac1a01f10063c5131d03 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Mon, 1 Dec 2025 15:52:22 +0300 Subject: [PATCH 08/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../ignite/internal/binary/BinaryUtils.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index 60e6985f20725..6b0a71f81a095 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -2859,10 +2859,10 @@ public static BinaryReaderEx reader(BinaryContext ctx, BinaryInputStream in, Cla * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ public static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - BinaryReaderEx reader, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + BinaryReaderEx reader, + boolean forUnmarshal) { return reader(ctx, in, ldr, reader.handles(), forUnmarshal); } @@ -2876,10 +2876,10 @@ public static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - @Nullable BinaryReaderHandles hnds, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + @Nullable BinaryReaderHandles hnds, + boolean forUnmarshal) { return new BinaryReaderExImpl(ctx, in, ldr, hnds, forUnmarshal); } @@ -2893,10 +2893,10 @@ static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ public static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - boolean skipHdrCheck, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + boolean skipHdrCheck, + boolean forUnmarshal) { return reader(ctx, in, ldr, null, skipHdrCheck, forUnmarshal); } @@ -2911,11 +2911,11 @@ public static BinaryReaderEx reader(BinaryContext ctx, * @param forUnmarshal {@code True} if reader is need to unmarshal object. */ static BinaryReaderEx reader(BinaryContext ctx, - BinaryInputStream in, - ClassLoader ldr, - @Nullable BinaryReaderHandles hnds, - boolean skipHdrCheck, - boolean forUnmarshal) { + BinaryInputStream in, + ClassLoader ldr, + @Nullable BinaryReaderHandles hnds, + boolean skipHdrCheck, + boolean forUnmarshal) { return new BinaryReaderExImpl(ctx, in, ldr, hnds, skipHdrCheck, forUnmarshal); } From a53ca5eecd2b08d908e5b863764c3eb03a8bc58d Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Mon, 1 Dec 2025 15:53:03 +0300 Subject: [PATCH 09/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../apache/ignite/internal/binary/BinaryMarshallerSelfTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java index 7e20d6c7b8bb3..dcc49c013e433 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java @@ -3309,7 +3309,7 @@ public void testPredefinedTypeIds() throws Exception { for (Map.Entry entry : map.entrySet()) { int id = entry.getValue(); - if (id == UNREGISTERED_TYPE_ID) + if (id == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) continue; BinaryClassDescriptor desc = bCtx.descriptorForTypeId(false, entry.getValue(), null, true); From 9cef7905c97e1110225880d91c572b3fb2315355 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 00:54:14 +0300 Subject: [PATCH 10/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../jmh/binary/JmhMapSerdesBenchmark.java | 6 ++-- .../internal/binary/BinaryWriterExImpl.java | 28 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java index 82b2800cc2ff7..cf0f4c0d4b768 100644 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -54,15 +54,15 @@ @Measurement(iterations = 3, time = 30, timeUnit = SECONDS) public class JmhMapSerdesBenchmark { /** */ - @Param({/*"Integer", "Date",*/ "String"/*, "Object"*/}) + @Param({"Integer"/*, "Date", "String", "Object"*/}) private String key; /** */ - @Param({/*"Integer", "Date",*/ "String"/*, "Object"*/}) + @Param({"Integer"/*, "Date", "String", "Object"*/}) private String value; /** */ - @Param({/* "10", "100", */ "1000"/*, "10000"*/}) + @Param({/* "10", "100", "1000" ,*/ "10000"}) private String size; /** */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index b578bd6ad6a5c..08af729d44069 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -845,13 +845,8 @@ void writeBooleanField(@Nullable Boolean val) { int fieldCnt0 = this.fieldCnt; BinaryInternalMapper mapper0 = this.mapper; - this.typeId = 0; - this.start = out.position(); - this.rawOffPos = 0; - this.schemaId = BinaryUtils.schemaInitialId(); - this.fieldCnt = 0; - this.mapper = null; - // Handles not cleared, because, in this mode it shared down to hierarchy. + clearState(); + // Handles not cleared, because, in this mode they shared down to hierarchy. marshal(obj); @@ -877,13 +872,8 @@ void writeBooleanField(@Nullable Boolean val) { BinaryInternalMapper mapper0 = this.mapper; BinaryWriterHandles handles0 = this.handles; - this.typeId = 0; - this.start = out.position(); - this.rawOffPos = 0; - this.schemaId = BinaryUtils.schemaInitialId(); - this.fieldCnt = 0; - this.mapper = null; - // Handles cleared, because, in this mode it NOT shared down to hierarchy. + clearState(); + // Handles cleared, because, in this mode they are NOT shared down to hierarchy. this.handles = null; marshal(obj); @@ -1578,4 +1568,14 @@ boolean tryWriteAsHandle(Object obj) { @Override public BinaryContext context() { return ctx; } + + /** Clears writer state. */ + private void clearState() { + this.typeId = 0; + this.start = out.position(); + this.rawOffPos = 0; + this.schemaId = BinaryUtils.schemaInitialId(); + this.fieldCnt = 0; + this.mapper = null; + } } From 20e1b437f4c07378ddf20ab22de0dd683c993a46 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 01:30:57 +0300 Subject: [PATCH 11/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java index cf0f4c0d4b768..4a4d3b541feaa 100644 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -62,7 +62,7 @@ public class JmhMapSerdesBenchmark { private String value; /** */ - @Param({/* "10", "100", "1000" ,*/ "10000"}) + @Param({/* "10", "100", "1000" ,*/ "100000"}) private String size; /** */ From 09e1ac916a9a2b28f14de17a724ef776e4d3f9b5 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 02:38:47 +0300 Subject: [PATCH 12/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../jmh/binary/JmhMapSerdesBenchmark.java | 93 +------------------ 1 file changed, 3 insertions(+), 90 deletions(-) diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java index 4a4d3b541feaa..5f7275883d344 100644 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -17,7 +17,6 @@ package org.apache.ignite.internal.benchmarks.jmh.binary; -import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -54,19 +53,11 @@ @Measurement(iterations = 3, time = 30, timeUnit = SECONDS) public class JmhMapSerdesBenchmark { /** */ - @Param({"Integer"/*, "Date", "String", "Object"*/}) - private String key; - - /** */ - @Param({"Integer"/*, "Date", "String", "Object"*/}) - private String value; - - /** */ - @Param({/* "10", "100", "1000" ,*/ "100000"}) + @Param({"100000", "1000000"}) private String size; /** */ - @Param({"HashMap"/*, "ConcurrentHashMap", "LinkedHashMap"*/}) + @Param({"HashMap", "ConcurrentHashMap", "LinkedHashMap"}) private String mapType; /** */ @@ -75,9 +66,6 @@ public class JmhMapSerdesBenchmark { /** */ private BinaryOutputStream out; - /** */ - private BinaryWriterEx writer; - /** */ private Map data; @@ -93,7 +81,6 @@ public void setup() throws Exception { bctx = node.context().cacheObjects().binaryContext(); out = BinaryStreams.outputStream((int)(100 * U.MB)); - writer = BinaryUtils.writer(bctx, out); Ignition.stopAll(false); @@ -108,22 +95,7 @@ else if ("LinkedHashMap".equals(mapType)) int sz = Integer.parseInt(size); for (int i = 0; i < sz; i++) - data.put(produce(i, key), produce(i, value)); - } - - /** */ - public Object produce(int i, String type) { - if ("Integer".equals(type)) - return i; - else if ("Date".equals(type)) - return new Date(System.currentTimeMillis() + i); - else if ("String".equals(type)) - return "" + i; - else if ("Object".equals(type)) { - return new Employee("Name" + i, 1000L * i, new Date()); - } - - throw new IllegalArgumentException("Unknown type: " + type); + data.put(i, i); } /** */ @@ -137,63 +109,4 @@ public void mapSerialization(Blackhole bh) { bh.consume(writer); } - - /** */ - public static class Employee { - /** */ - private String fio; - - /** */ - private long salary; - - /** */ - private Date created; - - /** */ - public Employee(String fio, long salary, Date created) { - this.fio = fio; - this.salary = salary; - this.created = created; - } - - /** */ - public Employee() { - } - - /** */ - public Employee(String fio, long salary) { - this.fio = fio; - this.salary = salary; - } - - /** */ - public String getFio() { - return fio; - } - - /** */ - public void setFio(String fio) { - this.fio = fio; - } - - /** */ - public long getSalary() { - return salary; - } - - /** */ - public void setSalary(long salary) { - this.salary = salary; - } - - /** */ - public Date getCreated() { - return created; - } - - /** */ - public void setCreated(Date created) { - this.created = created; - } - } } From 3fcbfe9319dc0939f79d6d379ce6ec3b58afd1a6 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 02:46:55 +0300 Subject: [PATCH 13/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java index 5f7275883d344..4744f7e125b59 100644 --- a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java +++ b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/binary/JmhMapSerdesBenchmark.java @@ -53,7 +53,7 @@ @Measurement(iterations = 3, time = 30, timeUnit = SECONDS) public class JmhMapSerdesBenchmark { /** */ - @Param({"100000", "1000000"}) + @Param({"5", "100000", "1000000"}) private String size; /** */ From a885b8e02cbf4aa19ae0b7575ae03c7f0d256906 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 13:05:54 +0300 Subject: [PATCH 14/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../ignite/internal/binary/BinaryWriterExImpl.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index 08af729d44069..a7ce36b2647d3 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -61,9 +61,6 @@ class BinaryWriterExImpl implements BinaryWriterEx { /** Schema. */ private final BinaryWriterSchemaHolder schema; - /** */ - private final boolean failIfUnregistered; - /** */ private int typeId; @@ -73,18 +70,21 @@ class BinaryWriterExImpl implements BinaryWriterEx { /** Raw offset position. */ private int rawOffPos; + /** Handles. */ + private BinaryWriterHandles handles; + /** Schema ID. */ private int schemaId = BinaryUtils.schemaInitialId(); /** Amount of written fields. */ private int fieldCnt; - /** Handles. */ - private BinaryWriterHandles handles; - /** */ private BinaryInternalMapper mapper; + /** */ + private final boolean failIfUnregistered; + /** * @param ctx Context. * @param out Output stream. From d541e89e8f833b3931cb0cd4fa8eda8aacd4fb44 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 16:36:17 +0300 Subject: [PATCH 15/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../internal/binary/BinaryWriterExImpl.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index a7ce36b2647d3..a7950cb591969 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -40,6 +40,7 @@ import org.jetbrains.annotations.Nullable; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.ignite.internal.binary.GridBinaryMarshaller.UNREGISTERED_TYPE_ID; import static org.apache.ignite.internal.util.CommonUtils.MAX_ARRAY_SIZE; /** @@ -288,7 +289,7 @@ else if (offsetByteCnt == BinaryUtils.OFFSET_2) out.unsafeWriteByte(GridBinaryMarshaller.OBJ); out.unsafeWriteByte(GridBinaryMarshaller.PROTO_VER); out.unsafeWriteShort(flags); - out.unsafeWriteInt(registered ? typeId : GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(registered ? typeId : UNREGISTERED_TYPE_ID); out.unsafePosition(start + GridBinaryMarshaller.TOTAL_LEN_POS); out.unsafeWriteInt(retPos - start); out.unsafeWriteInt(schemaId); @@ -357,7 +358,7 @@ void writeBinaryArray(BinaryArray val) throws BinaryObjectException { ); out.unsafeWriteInt(val.componentTypeId()); - if (val.componentTypeId() == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) + if (val.componentTypeId() == UNREGISTERED_TYPE_ID) writeString(val.componentClassName()); out.writeInt(val.array().length); @@ -375,7 +376,7 @@ void writeBinaryEnum(BinaryEnumObjectImpl val) { int typeId = val.typeId(); - if (typeId != GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { + if (typeId != UNREGISTERED_TYPE_ID) { out.unsafeEnsure(1 + 4 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); @@ -412,7 +413,7 @@ public void writeProxy(Proxy proxy, Class[] intfs) { if (desc.registered()) out.writeInt(desc.typeId()); else { - out.writeInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.writeInt(UNREGISTERED_TYPE_ID); writeString(intf.getName()); } @@ -460,7 +461,7 @@ void writeClass(@Nullable Class val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(UNREGISTERED_TYPE_ID); writeString(val.getName()); } @@ -838,6 +839,7 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { + // Store state. int typeId0 = this.typeId; int start0 = this.start; int rawOffPos0 = this.rawOffPos; @@ -845,11 +847,13 @@ void writeBooleanField(@Nullable Boolean val) { int fieldCnt0 = this.fieldCnt; BinaryInternalMapper mapper0 = this.mapper; - clearState(); // Handles not cleared, because, in this mode they shared down to hierarchy. + clearState(false); + // Maybe recursive `writeObject` invocation which will change state. marshal(obj); + // Restore state. this.typeId = typeId0; this.start = start0; this.rawOffPos = rawOffPos0; @@ -864,6 +868,7 @@ void writeBooleanField(@Nullable Boolean val) { if (obj == null) out.writeByte(GridBinaryMarshaller.NULL); else { + // Store state. int typeId0 = this.typeId; int start0 = this.start; int rawOffPos0 = this.rawOffPos; @@ -872,12 +877,13 @@ void writeBooleanField(@Nullable Boolean val) { BinaryInternalMapper mapper0 = this.mapper; BinaryWriterHandles handles0 = this.handles; - clearState(); // Handles cleared, because, in this mode they are NOT shared down to hierarchy. - this.handles = null; + clearState(true); + // Maybe recursive `writeObject` invocation which will change state. marshal(obj); + // Restore state. this.typeId = typeId0; this.start = start0; this.rawOffPos = rawOffPos0; @@ -1189,7 +1195,7 @@ void writeBooleanField(@Nullable Boolean val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(UNREGISTERED_TYPE_ID); writeString(val.getClass().getComponentType().getName()); } @@ -1273,7 +1279,7 @@ void writeBooleanField(@Nullable Boolean val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(UNREGISTERED_TYPE_ID); writeString(val.getDeclaringClass().getName()); } @@ -1313,7 +1319,7 @@ void doWriteEnumArray(@Nullable Object[] val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(UNREGISTERED_TYPE_ID); writeString(val.getClass().getComponentType().getName()); } @@ -1570,12 +1576,15 @@ boolean tryWriteAsHandle(Object obj) { } /** Clears writer state. */ - private void clearState() { - this.typeId = 0; + private void clearState(boolean clearHandler) { + this.typeId = UNREGISTERED_TYPE_ID; this.start = out.position(); this.rawOffPos = 0; this.schemaId = BinaryUtils.schemaInitialId(); this.fieldCnt = 0; this.mapper = null; + + if (clearHandler) + this.handles = null; } } From 60b9d255beca2220dc68ebde585d459fe835ba69 Mon Sep 17 00:00:00 2001 From: Nikolay Izhikov Date: Tue, 2 Dec 2025 16:57:49 +0300 Subject: [PATCH 16/16] IGNITE-27177 Remove BinaryWriter#typeId seter --- .../internal/binary/BinaryWriterExImpl.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index a7950cb591969..2bf503b043551 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -40,7 +40,6 @@ import org.jetbrains.annotations.Nullable; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.ignite.internal.binary.GridBinaryMarshaller.UNREGISTERED_TYPE_ID; import static org.apache.ignite.internal.util.CommonUtils.MAX_ARRAY_SIZE; /** @@ -289,7 +288,7 @@ else if (offsetByteCnt == BinaryUtils.OFFSET_2) out.unsafeWriteByte(GridBinaryMarshaller.OBJ); out.unsafeWriteByte(GridBinaryMarshaller.PROTO_VER); out.unsafeWriteShort(flags); - out.unsafeWriteInt(registered ? typeId : UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(registered ? typeId : GridBinaryMarshaller.UNREGISTERED_TYPE_ID); out.unsafePosition(start + GridBinaryMarshaller.TOTAL_LEN_POS); out.unsafeWriteInt(retPos - start); out.unsafeWriteInt(schemaId); @@ -358,7 +357,7 @@ void writeBinaryArray(BinaryArray val) throws BinaryObjectException { ); out.unsafeWriteInt(val.componentTypeId()); - if (val.componentTypeId() == UNREGISTERED_TYPE_ID) + if (val.componentTypeId() == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) writeString(val.componentClassName()); out.writeInt(val.array().length); @@ -376,7 +375,7 @@ void writeBinaryEnum(BinaryEnumObjectImpl val) { int typeId = val.typeId(); - if (typeId != UNREGISTERED_TYPE_ID) { + if (typeId != GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { out.unsafeEnsure(1 + 4 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); @@ -413,7 +412,7 @@ public void writeProxy(Proxy proxy, Class[] intfs) { if (desc.registered()) out.writeInt(desc.typeId()); else { - out.writeInt(UNREGISTERED_TYPE_ID); + out.writeInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); writeString(intf.getName()); } @@ -461,7 +460,7 @@ void writeClass(@Nullable Class val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); writeString(val.getName()); } @@ -1195,7 +1194,7 @@ void writeBooleanField(@Nullable Boolean val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); writeString(val.getClass().getComponentType().getName()); } @@ -1279,7 +1278,7 @@ void writeBooleanField(@Nullable Boolean val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); writeString(val.getDeclaringClass().getName()); } @@ -1319,7 +1318,7 @@ void doWriteEnumArray(@Nullable Object[] val) { if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); writeString(val.getClass().getComponentType().getName()); } @@ -1577,7 +1576,7 @@ boolean tryWriteAsHandle(Object obj) { /** Clears writer state. */ private void clearState(boolean clearHandler) { - this.typeId = UNREGISTERED_TYPE_ID; + this.typeId = GridBinaryMarshaller.UNREGISTERED_TYPE_ID; this.start = out.position(); this.rawOffPos = 0; this.schemaId = BinaryUtils.schemaInitialId();