This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git
The following commit(s) were added to refs/heads/main by this push:
new 8ab006c3 perf(java): Refactor ThreadPoolFury to improve performance
(#2092)
8ab006c3 is described below
commit 8ab006c3ea02dc2fd9dd2e57867f6c073a81bff2
Author: monk <[email protected]>
AuthorDate: Sat Mar 15 02:12:57 2025 +0800
perf(java): Refactor ThreadPoolFury to improve performance (#2092)
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->
## What does this PR do?
<!-- Describe the purpose of this PR. -->
After testing, it was observed that ThreadPoolFury experiences
prolonged blocking during cold starts under high-concurrency scenarios.
Analysis revealed that improper usage of locks in
ClassLoaderFuryPooled was the root cause. This PR refactors the
implementation of ClassLoaderFuryPooled by significantly reducing the
granularity of locks, thereby drastically minimizing blocking time
during cold starts.
## Related issues
<!--
Is there any related issue? Please attach here.
- #xxxx0
- #xxxx1
- #xxxx2
-->
[](url)https://github.com/apache/fury/issues/2087
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
CPU:9950X
Class:org.apache.fury.benchmark.ThreadPoolFurySuite.java
old:
Percentiles, ms/op:
p(0.0000) = 0.001 ms/op
p(50.0000) = 0.001 ms/op
p(90.0000) = 1587.388 ms/op
p(95.0000) = 1587.388 ms/op
p(99.0000) = 1587.388 ms/op
p(99.9000) = 1587.388 ms/op
p(99.9900) = 1587.388 ms/op
p(99.9990) = 1587.388 ms/op
p(99.9999) = 1587.388 ms/op
p(100.0000) = 1587.388 ms/op
new:
Percentiles, ms/op:
p(0.0000) = 0.001 ms/op
p(50.0000) = 0.001 ms/op
p(90.0000) = 62.746 ms/op
p(95.0000) = 62.746 ms/op
p(99.0000) = 62.746 ms/op
p(99.9000) = 62.746 ms/op
p(99.9900) = 62.746 ms/op
p(99.9990) = 62.746 ms/op
p(99.9999) = 62.746 ms/op
p(100.0000) = 62.746 ms/op
---
.../apache/fury/benchmark/ThreadPoolFurySuite.java | 92 ++++++++++++++++++++++
.../apache/fury/pool/ClassLoaderFuryPooled.java | 88 +++++++++++----------
2 files changed, 139 insertions(+), 41 deletions(-)
diff --git
a/java/benchmark/src/main/java/org/apache/fury/benchmark/ThreadPoolFurySuite.java
b/java/benchmark/src/main/java/org/apache/fury/benchmark/ThreadPoolFurySuite.java
new file mode 100644
index 00000000..3911e0d1
--- /dev/null
+++
b/java/benchmark/src/main/java/org/apache/fury/benchmark/ThreadPoolFurySuite.java
@@ -0,0 +1,92 @@
+/*
+ * 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.fury.benchmark;
+
+import java.io.IOException;
+import org.apache.fury.Fury;
+import org.apache.fury.ThreadSafeFury;
+import org.apache.fury.config.CompatibleMode;
+import org.apache.fury.config.Language;
+import org.apache.fury.logging.Logger;
+import org.apache.fury.logging.LoggerFactory;
+import org.openjdk.jmh.Main;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.CompilerControl;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.infra.Blackhole;
+
+@BenchmarkMode(Mode.SingleShotTime)
+@CompilerControl(value = CompilerControl.Mode.INLINE)
+@State(Scope.Benchmark)
+@OutputTimeUnit(java.util.concurrent.TimeUnit.MILLISECONDS)
+public class ThreadPoolFurySuite {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(ThreadPoolFurySuite.class);
+
+ private ThreadSafeFury fury =
+ Fury.builder()
+ .withLanguage(Language.JAVA)
+ .requireClassRegistration(false)
+ .withJdkClassSerializableCheck(false)
+ .withRefTracking(false)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .withAsyncCompilation(true)
+ .withRefTracking(true)
+ .buildThreadSafeFuryPool(10, 60);
+
+ private static StructBenchmark.NumericStruct struct;
+
+ static {
+ struct = StructBenchmark.NumericStruct.build();
+ struct.f1 = 1;
+ struct.f2 = 2;
+ struct.f3 = 3;
+ struct.f4 = 4;
+ struct.f5 = 5;
+ struct.f6 = 6;
+ struct.f7 = 7;
+ struct.f8 = 8;
+ }
+
+ @Benchmark()
+ @Threads(10000)
+ public void testObjectPool(Blackhole bh) {
+ bh.consume(fury.serialize(struct));
+ }
+
+ @TearDown
+ public void tearDown() {}
+
+ public static void main(String[] args) throws IOException {
+ if (args.length == 0) {
+ String commandLine =
+ "org.apache.fury.*ObjectPoolBenchmark.* -f 1 -wi 0 -i 5 -w 2s -r 2s
-rf csv";
+ System.out.println(commandLine);
+ args = commandLine.split(" ");
+ }
+ Main.main(args);
+ }
+}
diff --git
a/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
b/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
index ececce99..40d4d9f1 100644
---
a/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
+++
b/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
@@ -20,11 +20,10 @@
package org.apache.fury.pool;
import java.util.Objects;
-import java.util.Queue;
import java.util.WeakHashMap;
-import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
@@ -43,15 +42,15 @@ public class ClassLoaderFuryPooled {
private final ClassLoader classLoader;
- /**
- * idle Fury cache change. by : 1. getLoaderBind() 2.
returnObject(LoaderBinding) 3.
- * addObjAndWarp()
- */
- private final Queue<Fury> idleCacheQueue;
+ /** idle Fury cache change. by : 1. init() 2. getFury() 3.returnFury() */
+ private final BlockingQueue<Fury> idleCacheQueue;
final WeakHashMap<Fury, Object> allFury = new WeakHashMap<>();
- /** active cache size's number change by : 1. getLoaderBind() 2.
returnObject(LoaderBinding). */
+ /**
+ * The number of active Fury objects in the cache.Make sure it does not
exceed the maximum number
+ * of object pools.
+ */
private final AtomicInteger activeCacheNumber = new AtomicInteger(0);
/**
@@ -61,7 +60,6 @@ public class ClassLoaderFuryPooled {
private final int maxPoolSize;
private final Lock lock = new ReentrantLock();
- private final Condition furyCondition = lock.newCondition();
public ClassLoaderFuryPooled(
ClassLoader classLoader,
@@ -72,58 +70,66 @@ public class ClassLoaderFuryPooled {
this.maxPoolSize = maxPoolSize;
this.furyFactory = furyFactory;
this.classLoader = classLoader;
- idleCacheQueue = new ConcurrentLinkedQueue<>();
+ idleCacheQueue = new LinkedBlockingQueue<>(maxPoolSize);
while (idleCacheQueue.size() < minPoolSize) {
- addFury();
+ addFury(true);
}
}
public Fury getFury() {
- try {
- lock.lock();
+ if (activeCacheNumber.get() < maxPoolSize) {
Fury fury = idleCacheQueue.poll();
- while (fury == null) {
- if (activeCacheNumber.get() < maxPoolSize) {
- addFury();
- } else {
- furyCondition.await();
+ if (fury != null) {
+ return fury;
+ } else {
+ // new Fury return directly, no need to add to queue, it will be added
by returnFury()
+ fury = addFury(false);
+ if (fury != null) {
+ return fury;
}
- fury = idleCacheQueue.poll();
}
- activeCacheNumber.incrementAndGet();
- return fury;
- } catch (Exception e) {
- LOG.error(e.getMessage(), e);
+ }
+ try {
+ return idleCacheQueue.take();
+ } catch (InterruptedException e) {
throw new RuntimeException(e);
- } finally {
- lock.unlock();
}
}
public void returnFury(Fury fury) {
Objects.requireNonNull(fury);
+ idleCacheQueue.offer(fury);
+ }
+
+ private Fury addFury(boolean addQueue) {
+ // only activeCacheNumber increment success, can lock and create new Fury,
otherwise return
+ // null, and block in getFury(), wait for other thread to release
idleCacheQueue.
+ int after = activeCacheNumber.incrementAndGet();
+ if (after > maxPoolSize) {
+ activeCacheNumber.decrementAndGet();
+ return null;
+ }
try {
lock.lock();
- idleCacheQueue.add(fury);
- activeCacheNumber.decrementAndGet();
- furyCondition.signalAll();
- } catch (Exception e) {
- LOG.error(e.getMessage(), e);
- throw new RuntimeException(e);
+ Fury fury = furyFactory.apply(classLoader);
+ factoryCallback.accept(fury);
+ allFury.put(fury, null);
+ if (addQueue) {
+ idleCacheQueue.add(fury);
+ }
+ return fury;
} finally {
lock.unlock();
}
}
- private void addFury() {
- Fury fury = furyFactory.apply(classLoader);
- factoryCallback.accept(fury);
- idleCacheQueue.add(fury);
- allFury.put(fury, null);
- }
-
void setFactoryCallback(Consumer<Fury> factoryCallback) {
- this.factoryCallback = this.factoryCallback.andThen(factoryCallback);
- allFury.keySet().forEach(factoryCallback);
+ try {
+ lock.lock();
+ this.factoryCallback = this.factoryCallback.andThen(factoryCallback);
+ allFury.keySet().forEach(factoryCallback);
+ } finally {
+ lock.unlock();
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]