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

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a73c3a6c1de Reduce overhead of stream operations in SPI (#18222)
a73c3a6c1de is described below

commit a73c3a6c1decb5347e8a4908212c7595a91c0e04
Author: 吴伟杰 <[email protected]>
AuthorDate: Tue Jun 7 22:46:33 2022 +0800

    Reduce overhead of stream operations in SPI (#18222)
---
 .../shardingsphere/spi/type/optional/OptionalSPIRegistry.java      | 4 +++-
 .../shardingsphere/spi/type/required/RequiredSPIRegistry.java      | 7 ++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git 
a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/optional/OptionalSPIRegistry.java
 
b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/optional/OptionalSPIRegistry.java
index beb26492f1b..0e3a424e52f 100644
--- 
a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/optional/OptionalSPIRegistry.java
+++ 
b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/optional/OptionalSPIRegistry.java
@@ -21,6 +21,7 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 
+import java.util.Collection;
 import java.util.Optional;
 
 /**
@@ -37,6 +38,7 @@ public final class OptionalSPIRegistry {
      * @return registered service
      */
     public static <T extends OptionalSPI> Optional<T> 
findRegisteredService(final Class<T> spiClass) {
-        return 
ShardingSphereServiceLoader.getServiceInstances(spiClass).stream().findFirst();
+        Collection<T> result = 
ShardingSphereServiceLoader.getServiceInstances(spiClass);
+        return result.isEmpty() ? Optional.empty() : 
Optional.of(result.iterator().next());
     }
 }
diff --git 
a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/required/RequiredSPIRegistry.java
 
b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/required/RequiredSPIRegistry.java
index 1b3bb0ba107..3fddfc3e911 100644
--- 
a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/required/RequiredSPIRegistry.java
+++ 
b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/required/RequiredSPIRegistry.java
@@ -45,6 +45,11 @@ public final class RequiredSPIRegistry {
         if (1 == services.size()) {
             return services.iterator().next();
         }
-        return 
services.stream().filter(RequiredSPI::isDefault).findFirst().orElseGet(() -> 
services.iterator().next());
+        for (T each : services) {
+            if (each.isDefault()) {
+                return each;
+            }
+        }
+        return services.iterator().next();
     }
 }

Reply via email to