This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 036ec60458949addb12bd222f16a7d76e75b3f3f
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Tue May 14 10:11:28 2024 +0200

    Remove wrong optimization
    
    The benchmark on JDK 22 gives me the following:
    
    Benchmark                            Mode  Cnt          Score         Error 
 Units
    ArrayCopyBenchmark.arraysFill       thrpt   25  272368248.021 ± 1420262.516 
 ops/s
    ArrayCopyBenchmark.systemArrayCopy  thrpt   25  169188743.936 ± 1923345.140 
 ops/s
    
    See 
http://psy-lob-saw.blogspot.com/2015/04/on-arraysfill-intrinsics-superword-and.html
 for background information.
---
 core/camel-base-engine/pom.xml                     | 13 ++++++
 .../camel/impl/engine/CamelInternalProcessor.java  |  8 +---
 .../camel/impl/engine/ArrayCopyBenchmark.java      | 50 ++++++++++++++++++++++
 3 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/core/camel-base-engine/pom.xml b/core/camel-base-engine/pom.xml
index e75720e2a50..7e562efbeb4 100644
--- a/core/camel-base-engine/pom.xml
+++ b/core/camel-base-engine/pom.xml
@@ -50,6 +50,19 @@
             <artifactId>slf4j-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.openjdk.jmh</groupId>
+            <artifactId>jmh-core</artifactId>
+            <version>1.37</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.openjdk.jmh</groupId>
+            <artifactId>jmh-generator-annprocess</artifactId>
+            <version>1.37</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
 </project>
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java
index f2b0faef125..56a061be2e8 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelInternalProcessor.java
@@ -17,6 +17,7 @@
 package org.apache.camel.impl.engine;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
@@ -116,7 +117,6 @@ public class CamelInternalProcessor extends 
DelegateAsyncProcessor implements In
     private final ShutdownStrategy shutdownStrategy;
     private final List<CamelInternalProcessorAdvice<?>> advices = new 
ArrayList<>();
     private byte statefulAdvices;
-    private Object[] emptyStatefulStates;
     private PooledObjectFactory<CamelInternalTask> taskFactory;
 
     public CamelInternalProcessor(CamelContext camelContext) {
@@ -142,9 +142,6 @@ public class CamelInternalProcessor extends 
DelegateAsyncProcessor implements In
             int capacity = 
camelContext.getCamelContextExtension().getExchangeFactory().getCapacity();
             taskFactory.setCapacity(capacity);
             LOG.trace("Using TaskFactory: {}", taskFactory);
-
-            // create empty array we can use for reset
-            emptyStatefulStates = new Object[statefulAdvices];
         }
 
         ServiceHelper.buildService(taskFactory, processor);
@@ -236,8 +233,7 @@ public class CamelInternalProcessor extends 
DelegateAsyncProcessor implements In
 
         @Override
         public void reset() {
-            // reset array by copying over from empty which is a very fast JVM 
optimized operation
-            System.arraycopy(emptyStatefulStates, 0, states, 0, 
statefulAdvices);
+            Arrays.fill(this.states, null);
             this.exchange = null;
             this.originalCallback = null;
         }
diff --git 
a/core/camel-base-engine/src/test/java/org/apache/camel/impl/engine/ArrayCopyBenchmark.java
 
b/core/camel-base-engine/src/test/java/org/apache/camel/impl/engine/ArrayCopyBenchmark.java
new file mode 100644
index 00000000000..a727980cad0
--- /dev/null
+++ 
b/core/camel-base-engine/src/test/java/org/apache/camel/impl/engine/ArrayCopyBenchmark.java
@@ -0,0 +1,50 @@
+/*
+ * 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.camel.impl.engine;
+
+import java.util.Arrays;
+
+import org.openjdk.jmh.annotations.*;
+
+@State(Scope.Benchmark)
+public class ArrayCopyBenchmark {
+
+    private static final int ARRAY_SIZE = 10; // Adjust as needed
+
+    private Object[] sourceArray;
+    private Object[] destArray;
+
+    @Setup
+    public void setup() {
+        sourceArray = new Object[ARRAY_SIZE];
+        destArray = new Object[ARRAY_SIZE];
+    }
+
+    @Benchmark
+    public void systemArrayCopy() {
+        System.arraycopy(sourceArray, 0, destArray, 0, ARRAY_SIZE);
+    }
+
+    @Benchmark
+    public void arraysFill() {
+        Arrays.fill(destArray, null);
+    }
+
+    public static void main(String[] args) throws Exception {
+        org.openjdk.jmh.Main.main(args);
+    }
+}

Reply via email to