Skip to content

Commit b231680

Browse files
fix: Fixed high CPU usage due to pending JS commands (#22024) (#22035) (#22048)
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 Co-authored-by: Tomi Virtanen <tltv@vaadin.com> (cherry picked from commit dfe8763) Co-authored-by: daniel-pss-prosoft <54983789+daniel-pss-prosoft@users.noreply.github.com>
1 parent cedecb0 commit b231680

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
@@ -16,6 +16,7 @@
1616
import java.util.HashMap;
1717
import java.util.HashSet;
1818
import java.util.IdentityHashMap;
19+
import java.util.LinkedHashSet;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -162,7 +163,7 @@ public List<Object> getParameters() {
162163
*/
163164
private long lastHeartbeatTimestamp = System.currentTimeMillis();
164165

165-
private List<PendingJavaScriptInvocation> pendingJsInvocations = new ArrayList<>();
166+
private Set<PendingJavaScriptInvocation> pendingJsInvocations = new LinkedHashSet<>();
166167

167168
private final HashMap<StateNode, PendingJavaScriptInvocationDetachListener> pendingJsInvocationDetachListeners = new HashMap<>();
168169

@@ -587,7 +588,7 @@ public List<PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations() {
587588

588589
pendingJsInvocations = getPendingJavaScriptInvocations()
589590
.filter(invocation -> !invocation.getOwner().isVisible())
590-
.collect(Collectors.toCollection(ArrayList::new));
591+
.collect(Collectors.toCollection(LinkedHashSet::new));
591592
pendingJsInvocations
592593
.forEach(this::registerDetachListenerForPendingInvocation);
593594
return readyToSend;
@@ -630,8 +631,7 @@ public void execute() {
630631

631632
private void removePendingInvocation(
632633
PendingJavaScriptInvocation invocation) {
633-
UIInternals.this.pendingJsInvocations.removeIf(
634-
pendingInvocation -> pendingInvocation.equals(invocation));
634+
UIInternals.this.pendingJsInvocations.remove(invocation);
635635
if (invocationList.isEmpty() && registration != null) {
636636
registration.remove();
637637
registration = null;

0 commit comments

Comments
 (0)