Skip to content

Commit 9a5095d

Browse files
authored
fix: Fixed high CPU usage due to pending JS commands (#22024) (#22035) (#22049)
Field "pendingJsInvocations" should be a Set instead of a List in order to avoid O(n^2) operations. This greatly improves performance in situations where many fields are updated constantly and the JS command queue becomes very large. Fixes #22024
1 parent e1b5dc8 commit 9a5095d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.HashMap;
1515
import java.util.HashSet;
1616
import java.util.IdentityHashMap;
17+
import java.util.LinkedHashSet;
1718
import java.util.List;
1819
import java.util.Map;
1920
import java.util.Optional;
@@ -165,7 +166,7 @@ public List<Object> getParameters() {
165166
*/
166167
private long lastHeartbeatTimestamp = System.currentTimeMillis();
167168

168-
private List<PendingJavaScriptInvocation> pendingJsInvocations = new ArrayList<>();
169+
private Set<PendingJavaScriptInvocation> pendingJsInvocations = new LinkedHashSet<>();
169170

170171
private final HashMap<StateNode, PendingJavaScriptInvocationDetachListener> pendingJsInvocationDetachListeners = new HashMap<>();
171172

@@ -572,7 +573,7 @@ public List<PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations() {
572573

573574
pendingJsInvocations = getPendingJavaScriptInvocations()
574575
.filter(invocation -> !invocation.getOwner().isVisible())
575-
.collect(Collectors.toCollection(ArrayList::new));
576+
.collect(Collectors.toCollection(LinkedHashSet::new));
576577
pendingJsInvocations
577578
.forEach(this::registerDetachListenerForPendingInvocation);
578579
return readyToSend;
@@ -615,8 +616,7 @@ public void execute() {
615616

616617
private void removePendingInvocation(
617618
PendingJavaScriptInvocation invocation) {
618-
UIInternals.this.pendingJsInvocations.removeIf(
619-
pendingInvocation -> pendingInvocation.equals(invocation));
619+
UIInternals.this.pendingJsInvocations.remove(invocation);
620620
if (invocationList.isEmpty() && registration != null) {
621621
registration.remove();
622622
registration = null;

0 commit comments

Comments
 (0)