You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/02/28 17:09:06 UTC

[camel] 02/02: CAMEL-19058: cache the producer and endpoint locally to avoid hitting the type check scalability issue

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

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

commit c8d36cb1ddb93331cbecb0efa10586737f1d72c6
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Sun Feb 12 09:28:55 2023 +0100

    CAMEL-19058: cache the producer and endpoint locally to avoid hitting the type check scalability issue
---
 .../org/apache/camel/support/cache/DefaultProducerCache.java | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java b/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
index 17612316e62..c8ad9287213 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
@@ -58,6 +58,9 @@ public class DefaultProducerCache extends ServiceSupport implements ProducerCach
     private boolean extendedStatistics;
     private int maxCacheSize;
 
+    private Endpoint lastUsedEndpoint;
+    private AsyncProducer lastUsedProducer;
+
     public DefaultProducerCache(Object source, CamelContext camelContext, int cacheSize) {
         this.source = source;
         this.camelContext = camelContext;
@@ -119,11 +122,20 @@ public class DefaultProducerCache extends ServiceSupport implements ProducerCach
 
     @Override
     public AsyncProducer acquireProducer(Endpoint endpoint) {
+        // Try to favor thread locality as some data in the producer's cache may be shared among threads,
+        // triggering cases of false sharing
+        if (endpoint == lastUsedEndpoint) {
+            return lastUsedProducer;
+        }
+
         try {
             AsyncProducer producer = producers.acquire(endpoint);
             if (statistics != null) {
                 statistics.onHit(endpoint.getEndpointUri());
             }
+            lastUsedEndpoint = endpoint;
+            lastUsedProducer = producer;
+
             return producer;
         } catch (Throwable e) {
             throw new FailedToCreateProducerException(endpoint, e);