summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Riddle Hsu <riddlehsu@google.com> 2024-05-15 16:19:33 +0800
committer Riddle Hsu <riddlehsu@google.com> 2024-05-15 17:10:41 +0800
commitc0c15eb59c018a455f3a86cf9b0c23e6eb90e345 (patch)
treeaacfd09a61362727cea4000ae820bef22a034938
parentb972663f6daf3cb60874e1169c3e080e735bc8b6 (diff)
Reduce unnecessary overhead of always-true predicate lambda
Currently compiler always generates new class as $$ExternalSyntheticLambda* on every invocation place. And a new instance will be always created on invocation no matter whether the lambda is capturing arguments or not. By using an explicit shared instance, it can reduce synthetic class and method generation. Also reduce 4 instructions per invocation: new-instance, invoke-direct, check-cast, invoke-static. Bug: 163976519 Test: CtsWindowManagerDeviceActivity Change-Id: I71a89713d64f0605faf6045ba44389a8eb47f3d5
-rw-r--r--services/core/java/com/android/server/wm/ActivityRecord.java2
-rw-r--r--services/core/java/com/android/server/wm/DisplayContent.java2
-rw-r--r--services/core/java/com/android/server/wm/TaskDisplayArea.java2
-rw-r--r--services/core/java/com/android/server/wm/WindowContainer.java28
-rw-r--r--services/core/java/com/android/server/wm/utils/AlwaysTruePredicate.java33
5 files changed, 51 insertions, 16 deletions
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index e814f17bedef..bd1ba001fd31 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -11109,7 +11109,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
* Otherwise, return the creation time of the top window.
*/
long getLastWindowCreateTime() {
- final WindowState window = getWindow(win -> true);
+ final WindowState window = getWindow(alwaysTruePredicate());
return window != null && window.mAttrs.type != TYPE_BASE_APPLICATION
? window.getCreateTime()
: createTime;
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index c9a5e71ef1fd..a5853c013c7b 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -2755,7 +2755,7 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
@Nullable
Task getTopRootTask() {
- return getRootTask(t -> true);
+ return getRootTask(alwaysTruePredicate());
}
/**
diff --git a/services/core/java/com/android/server/wm/TaskDisplayArea.java b/services/core/java/com/android/server/wm/TaskDisplayArea.java
index 2c27b98250c9..eff831552320 100644
--- a/services/core/java/com/android/server/wm/TaskDisplayArea.java
+++ b/services/core/java/com/android/server/wm/TaskDisplayArea.java
@@ -223,7 +223,7 @@ final class TaskDisplayArea extends DisplayArea<WindowContainer> {
@VisibleForTesting
Task getTopRootTask() {
- return getRootTask(t -> true);
+ return getRootTask(alwaysTruePredicate());
}
@Nullable
diff --git a/services/core/java/com/android/server/wm/WindowContainer.java b/services/core/java/com/android/server/wm/WindowContainer.java
index d70ca02cc23d..edbba9244738 100644
--- a/services/core/java/com/android/server/wm/WindowContainer.java
+++ b/services/core/java/com/android/server/wm/WindowContainer.java
@@ -116,6 +116,7 @@ import com.android.internal.util.ToBooleanFunction;
import com.android.server.wm.SurfaceAnimator.Animatable;
import com.android.server.wm.SurfaceAnimator.AnimationType;
import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
+import com.android.server.wm.utils.AlwaysTruePredicate;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
@@ -2019,29 +2020,34 @@ class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<
callback, boundary, includeBoundary, traverseTopToBottom, boundaryFound);
}
+ @SuppressWarnings("unchecked")
+ static <T> Predicate<T> alwaysTruePredicate() {
+ return (Predicate<T>) AlwaysTruePredicate.INSTANCE;
+ }
+
ActivityRecord getActivityAbove(ActivityRecord r) {
- return getActivity((above) -> true, r,
+ return getActivity(alwaysTruePredicate(), r /* boundary */,
false /*includeBoundary*/, false /*traverseTopToBottom*/);
}
ActivityRecord getActivityBelow(ActivityRecord r) {
- return getActivity((below) -> true, r,
+ return getActivity(alwaysTruePredicate(), r /* boundary */,
false /*includeBoundary*/, true /*traverseTopToBottom*/);
}
ActivityRecord getBottomMostActivity() {
- return getActivity((r) -> true, false /*traverseTopToBottom*/);
+ return getActivity(alwaysTruePredicate(), false /* traverseTopToBottom */);
}
ActivityRecord getTopMostActivity() {
- return getActivity((r) -> true, true /*traverseTopToBottom*/);
+ return getActivity(alwaysTruePredicate(), true /* traverseTopToBottom */);
}
ActivityRecord getTopActivity(boolean includeFinishing, boolean includeOverlays) {
// Break down into 4 calls to avoid object creation due to capturing input params.
if (includeFinishing) {
if (includeOverlays) {
- return getActivity((r) -> true);
+ return getActivity(alwaysTruePredicate());
}
return getActivity((r) -> !r.isTaskOverlay());
} else if (includeOverlays) {
@@ -2220,21 +2226,17 @@ class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<
}
}
- Task getTaskAbove(Task t) {
- return getTask(
- (above) -> true, t, false /*includeBoundary*/, false /*traverseTopToBottom*/);
- }
-
Task getTaskBelow(Task t) {
- return getTask((below) -> true, t, false /*includeBoundary*/, true /*traverseTopToBottom*/);
+ return getTask(alwaysTruePredicate(), t /* boundary */,
+ false /* includeBoundary */, true /* traverseTopToBottom */);
}
Task getBottomMostTask() {
- return getTask((t) -> true, false /*traverseTopToBottom*/);
+ return getTask(alwaysTruePredicate(), false /* traverseTopToBottom */);
}
Task getTopMostTask() {
- return getTask((t) -> true, true /*traverseTopToBottom*/);
+ return getTask(alwaysTruePredicate(), true /* traverseTopToBottom */);
}
Task getTask(Predicate<Task> callback) {
diff --git a/services/core/java/com/android/server/wm/utils/AlwaysTruePredicate.java b/services/core/java/com/android/server/wm/utils/AlwaysTruePredicate.java
new file mode 100644
index 000000000000..49dcb6cab633
--- /dev/null
+++ b/services/core/java/com/android/server/wm/utils/AlwaysTruePredicate.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed 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 com.android.server.wm.utils;
+
+import java.util.function.Predicate;
+
+/** A simple Predicate to avoid synthetic allocation of lambda expression "o -> true". */
+public class AlwaysTruePredicate implements Predicate<Object> {
+
+ public static final AlwaysTruePredicate INSTANCE = new AlwaysTruePredicate();
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(Object o) {
+ return true;
+ }
+}