From 4efb646252c10d0193ecdf98dc6c2d22f7ed0be0 Mon Sep 17 00:00:00 2001 From: Orion Hodson Date: Tue, 20 Dec 2022 15:57:19 +0000 Subject: [PATCH] Reduce Integer allocations in ThreadLocalWorkSource The code was allocating an Integer for each call of ThreadLocalWorkSource.setUid(). Bug: 262916014 Test: atest binderLibTest BinderWorkSourceTest BinderProxyTest Test: atest BinderCallsStatsServiceTest android.os.ParcelTest Change-Id: I75ece3262bdebaf9d58aee9c355d22121ae09a40 --- core/java/android/os/ThreadLocalWorkSource.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/java/android/os/ThreadLocalWorkSource.java b/core/java/android/os/ThreadLocalWorkSource.java index 894b1cc475b0e..e9adb209ddc6d 100644 --- a/core/java/android/os/ThreadLocalWorkSource.java +++ b/core/java/android/os/ThreadLocalWorkSource.java @@ -39,8 +39,8 @@ package android.os; */ public final class ThreadLocalWorkSource { public static final int UID_NONE = Message.UID_NONE; - private static final ThreadLocal sWorkSourceUid = - ThreadLocal.withInitial(() -> UID_NONE); + private static final ThreadLocal sWorkSourceUid = + ThreadLocal.withInitial(() -> new int[] {UID_NONE}); /** * Returns the UID to blame for the code currently executed on this thread. @@ -50,7 +50,7 @@ public final class ThreadLocalWorkSource { *

It can also be set manually using {@link #setUid(int)}. */ public static int getUid() { - return sWorkSourceUid.get(); + return sWorkSourceUid.get()[0]; } /** @@ -65,7 +65,7 @@ public final class ThreadLocalWorkSource { */ public static long setUid(int uid) { final long token = getToken(); - sWorkSourceUid.set(uid); + sWorkSourceUid.get()[0] = uid; return token; } @@ -73,7 +73,7 @@ public final class ThreadLocalWorkSource { * Restores the state using the provided token. */ public static void restore(long token) { - sWorkSourceUid.set(parseUidFromToken(token)); + sWorkSourceUid.get()[0] = parseUidFromToken(token); } /** @@ -88,7 +88,7 @@ public final class ThreadLocalWorkSource { * * * @return a token that can be used to restore the state. - **/ + */ public static long clear() { return setUid(UID_NONE); } @@ -98,7 +98,7 @@ public final class ThreadLocalWorkSource { } private static long getToken() { - return sWorkSourceUid.get(); + return sWorkSourceUid.get()[0]; } private ThreadLocalWorkSource() {