This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 0f92d31596 improve Result.clone() performance, fixes #9332 (#9334)
0f92d31596 is described below
commit 0f92d315960ebe644c945a12deb25cd0526536e0
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 6 20:35:31 2026 +0100
improve Result.clone() performance, fixes #9332 (#9334)
---
core/src/main/java/org/apache/hop/core/Result.java | 34 +++++-
.../test/java/org/apache/hop/core/ResultTest.java | 136 ++++++++++++++++++++-
2 files changed, 164 insertions(+), 6 deletions(-)
diff --git a/core/src/main/java/org/apache/hop/core/Result.java
b/core/src/main/java/org/apache/hop/core/Result.java
index 6f245b9927..2171db3262 100644
--- a/core/src/main/java/org/apache/hop/core/Result.java
+++ b/core/src/main/java/org/apache/hop/core/Result.java
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -29,6 +30,7 @@ import lombok.Setter;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.exception.HopFileException;
import org.apache.hop.core.exception.HopRuntimeException;
+import org.apache.hop.core.exception.HopValueException;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.RowMeta;
import org.apache.hop.core.xml.XmlHandler;
@@ -209,11 +211,7 @@ public class Result implements Cloneable {
// Clone result rows and files as well...
if (rows != null) {
- List<RowMetaAndData> clonedRows = new ArrayList<>();
- for (RowMetaAndData row : rows) {
- clonedRows.add(row.clone());
- }
- result.setRows(clonedRows);
+ result.setRows(cloneRows(rows));
}
if (resultFiles != null) {
@@ -231,6 +229,32 @@ public class Result implements Cloneable {
}
}
+ /**
+ * Deep copy of the result rows. Each distinct row metadata is cloned once,
and its clone is
+ * shared by every row that shared the original: cloning row metadata
rebuilds its number and date
+ * formats, and the engine clones the result before every workflow action.
The metadata is matched
+ * by identity, so rows with their own metadata keep their own clone. Every
row still gets a new
+ * copy of its values.
+ */
+ private static List<RowMetaAndData> cloneRows(List<RowMetaAndData> rows) {
+ List<RowMetaAndData> clonedRows = new ArrayList<>(rows.size());
+ Map<IRowMeta, IRowMeta> clonedRowMetas = new IdentityHashMap<>();
+ for (RowMetaAndData row : rows) {
+ IRowMeta rowMeta = row.getRowMeta();
+ if (rowMeta == null) {
+ clonedRows.add(row.clone());
+ continue;
+ }
+ IRowMeta clonedRowMeta = clonedRowMetas.computeIfAbsent(rowMeta,
IRowMeta::clone);
+ try {
+ clonedRows.add(new RowMetaAndData(clonedRowMeta,
rowMeta.cloneRow(row.getData())));
+ } catch (HopValueException e) {
+ throw new HopRuntimeException("Problem with clone row detected in
RowMetaAndData", e);
+ }
+ }
+ return clonedRows;
+ }
+
/**
* Returns a string representation of the Result object
*
diff --git a/core/src/test/java/org/apache/hop/core/ResultTest.java
b/core/src/test/java/org/apache/hop/core/ResultTest.java
index 6f430b3186..9a9f8481a8 100644
--- a/core/src/test/java/org/apache/hop/core/ResultTest.java
+++ b/core/src/test/java/org/apache/hop/core/ResultTest.java
@@ -6,7 +6,7 @@
* (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
+ * 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,
@@ -17,8 +17,20 @@
package org.apache.hop.core;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaBinary;
+import org.apache.hop.core.row.value.ValueMetaInteger;
+import org.apache.hop.core.row.value.ValueMetaString;
import org.apache.hop.core.xml.XmlHandler;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Node;
@@ -40,4 +52,126 @@ class ResultTest {
assertEquals(0, copy.getRows().size());
assertEquals(0, copy.getResultFiles().size());
}
+
+ @Test
+ void cloneCopiesValuesIntoNewArraysAndNewMetadata() {
+ IRowMeta rowMeta = idNamePayloadMeta();
+ byte[] payload = {1, 2, 3};
+ Result result = resultWith(row(rowMeta, 1L, "a", payload), row(rowMeta,
2L, "b", null));
+
+ List<RowMetaAndData> clonedRows = result.clone().getRows();
+
+ assertEquals(2, clonedRows.size());
+ for (int i = 0; i < 2; i++) {
+ RowMetaAndData original = result.getRows().get(i);
+ RowMetaAndData clone = clonedRows.get(i);
+ assertNotSame(original, clone);
+ assertNotSame(original.getData(), clone.getData());
+ assertNotSame(original.getRowMeta(), clone.getRowMeta());
+ assertEquals(
+ List.of(original.getRowMeta().getFieldNames()),
+ List.of(clone.getRowMeta().getFieldNames()));
+ assertEquals(original.getData()[0], clone.getData()[0]);
+ assertEquals(original.getData()[1], clone.getData()[1]);
+ }
+ // cloneRow() copies binary values, as before.
+ assertNotSame(payload, clonedRows.get(0).getData()[2]);
+ assertArrayEquals(payload, (byte[]) clonedRows.get(0).getData()[2]);
+ assertNull(clonedRows.get(1).getData()[2]);
+ }
+
+ @Test
+ void changingTheOriginalDoesNotChangeTheClone() {
+ IRowMeta rowMeta = idNamePayloadMeta();
+ byte[] payload = {1, 2, 3};
+ Result result = resultWith(row(rowMeta, 1L, "a", payload), row(rowMeta,
2L, "b", null));
+
+ Result clone = result.clone();
+ result.getRows().get(0).getData()[1] = "changed";
+ payload[0] = 9;
+ rowMeta.getValueMeta(1).setName("renamed");
+ rowMeta.addValueMeta(new ValueMetaString("extra"));
+
+ for (RowMetaAndData clonedRow : clone.getRows()) {
+ assertEquals(
+ List.of("id", "name", "payload"),
List.of(clonedRow.getRowMeta().getFieldNames()));
+ }
+ assertEquals("a", clone.getRows().get(0).getData()[1]);
+ assertArrayEquals(new byte[] {1, 2, 3}, (byte[])
clone.getRows().get(0).getData()[2]);
+ }
+
+ @Test
+ void rowsSharingMetadataShareOneClone() {
+ IRowMeta shared = idNamePayloadMeta();
+ Result result =
+ resultWith(
+ row(shared, 1L, "a", null), row(shared, 2L, "b", null),
row(shared, 3L, "c", null));
+
+ List<RowMetaAndData> clonedRows = result.clone().getRows();
+
+ IRowMeta clonedMeta = clonedRows.get(0).getRowMeta();
+ assertNotSame(shared, clonedMeta);
+ for (RowMetaAndData row : clonedRows) {
+ assertSame(clonedMeta, row.getRowMeta());
+ }
+ }
+
+ @Test
+ void rowsWithTheirOwnMetadataKeepTheirOwnClone() {
+ // Two equal but distinct metadata objects: matched by identity, not by
equality.
+ IRowMeta first = idNamePayloadMeta();
+ IRowMeta second = idNamePayloadMeta();
+ Result result =
+ resultWith(
+ row(first, 1L, "a", null), row(second, 2L, "b", null), row(first,
3L, "c", null));
+
+ List<RowMetaAndData> clonedRows = result.clone().getRows();
+
+ assertNotSame(clonedRows.get(0).getRowMeta(),
clonedRows.get(1).getRowMeta());
+ assertSame(clonedRows.get(0).getRowMeta(), clonedRows.get(2).getRowMeta());
+ }
+
+ @Test
+ void mixedLayoutsKeepEachRowsOwnLayout() {
+ IRowMeta wide = idNamePayloadMeta();
+ IRowMeta narrow = new RowMeta();
+ narrow.addValueMeta(new ValueMetaString("code"));
+ Result result =
+ resultWith(
+ row(wide, 1L, "a", null), new RowMetaAndData(narrow, "x"),
row(wide, 2L, "b", null));
+
+ List<RowMetaAndData> clonedRows = result.clone().getRows();
+
+ assertEquals(
+ List.of("id", "name", "payload"),
List.of(clonedRows.get(0).getRowMeta().getFieldNames()));
+ assertEquals(List.of("code"),
List.of(clonedRows.get(1).getRowMeta().getFieldNames()));
+ assertEquals("x", clonedRows.get(1).getData()[0]);
+ assertSame(clonedRows.get(0).getRowMeta(), clonedRows.get(2).getRowMeta());
+ }
+
+ @Test
+ void cloneOfEmptyRows() {
+ Result empty = new Result();
+ Result clone = empty.clone();
+ assertTrue(clone.getRows().isEmpty());
+ assertNotSame(empty.getRows(), clone.getRows());
+ }
+
+ private static IRowMeta idNamePayloadMeta() {
+ IRowMeta rowMeta = new RowMeta();
+ rowMeta.addValueMeta(new ValueMetaInteger("id"));
+ rowMeta.addValueMeta(new ValueMetaString("name"));
+ rowMeta.addValueMeta(new ValueMetaBinary("payload"));
+ return rowMeta;
+ }
+
+ private static RowMetaAndData row(IRowMeta rowMeta, Long id, String name,
byte[] payload) {
+ return new RowMetaAndData(rowMeta, id, name, payload);
+ }
+
+ private static Result resultWith(RowMetaAndData... rows) {
+ Result result = new Result();
+ result.setRows(new ArrayList<>(List.of(rows)));
+ return result;
+ }
}