From 2ad85f032327c99c38d00bf97631d7af66201058 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Thu, 16 Dec 2021 18:12:34 +0800 Subject: [PATCH] Use hand written hasCode for ChangeIdStateQuery Because Objects.hash(Object... values) will always create an array to wrap the "..." arguments, with additional auto-boxing for all primitive type arguments. This can reduce the execution time of ChangeIdStateQuery#hashCode() by 8 times, which is usually the main cost of CompatChanges#isChangeEnabled. Bug: 208449209 Test: atest android.app.compat.CompatChangesTest Change-Id: I863aa1d35e7448b5a965368272198c4529253ae0 --- core/java/android/app/compat/ChangeIdStateQuery.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/compat/ChangeIdStateQuery.java b/core/java/android/app/compat/ChangeIdStateQuery.java index 91765f78a2b01..7598d6c90d3d8 100644 --- a/core/java/android/app/compat/ChangeIdStateQuery.java +++ b/core/java/android/app/compat/ChangeIdStateQuery.java @@ -85,6 +85,14 @@ final class ChangeIdStateQuery { @Override public int hashCode() { - return Objects.hash(type, changeId, packageName, uid, userId); + int result = 1; + result = 31 * result + type; + result = 31 * result + (int) (changeId ^ (changeId >>> 32)); + if (packageName != null) { + result = 31 * result + packageName.hashCode(); + } + result = 31 * result + uid; + result = 31 * result + userId; + return result; } }