diff options
| author | 2024-02-10 00:25:36 +0000 | |
|---|---|---|
| committer | 2024-02-14 19:27:30 +0000 | |
| commit | 5fecb71b79571a2168508f1a10123918b5ccc306 (patch) | |
| tree | 42f7ae805c1cf4e9092becb390e638a6f65503ff | |
| parent | bd5d529388bcc9959a7d17704fde0c60c1b9ead4 (diff) | |
Cache MemoryIntArray size
The underlying ashmem buffer size should never change, so cache it upon
MemoryIntArray creation. We still validate that the ashmem region hasn't
been closed before any associated operations on the member size.
This reduces MemoryIntArray-related overhead for some common operations
by ~50%.
Bug: 323623465
Test: m + presubmit
Change-Id: I41060c3da3dad830b35acf042c5bc5b0af3b6091
4 files changed, 16 insertions, 24 deletions
diff --git a/core/java/android/util/MemoryIntArray.java b/core/java/android/util/MemoryIntArray.java index 5cbbbef2cf88..2226881c82cf 100644 --- a/core/java/android/util/MemoryIntArray.java +++ b/core/java/android/util/MemoryIntArray.java @@ -58,6 +58,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { private final boolean mIsOwner; private final long mMemoryAddr; + private final int mSize; private int mFd = -1; /** @@ -75,6 +76,9 @@ public final class MemoryIntArray implements Parcelable, Closeable { final String name = UUID.randomUUID().toString(); mFd = nativeCreate(name, size); mMemoryAddr = nativeOpen(mFd, mIsOwner); + // Note that we use the effective size after allocation, rather than the provided size, + // preserving compat with the original behavior. In practice these should be equivalent. + mSize = nativeSize(mFd); mCloseGuard.open("MemoryIntArray.close"); } @@ -86,6 +90,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { } mFd = pfd.detachFd(); mMemoryAddr = nativeOpen(mFd, mIsOwner); + mSize = nativeSize(mFd); mCloseGuard.open("MemoryIntArray.close"); } @@ -127,13 +132,11 @@ public final class MemoryIntArray implements Parcelable, Closeable { } /** - * Gets the array size. - * - * @throws IOException If an error occurs while accessing the shared memory. + * @return Gets the array size. */ - public int size() throws IOException { + public int size() { enforceNotClosed(); - return nativeSize(mFd); + return mSize; } /** @@ -210,11 +213,10 @@ public final class MemoryIntArray implements Parcelable, Closeable { } } - private void enforceValidIndex(int index) throws IOException { - final int size = size(); - if (index < 0 || index > size - 1) { + private void enforceValidIndex(int index) { + if (index < 0 || index > mSize - 1) { throw new IndexOutOfBoundsException( - index + " not between 0 and " + (size - 1)); + index + " not between 0 and " + (mSize - 1)); } } diff --git a/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java b/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java index 51013e4b4f00..8093af9bb004 100644 --- a/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java +++ b/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java @@ -123,7 +123,7 @@ public class MemoryIntArrayTest { parcel.recycle(); assertNotNull("Should marshall file descriptor", secondArray); - + assertEquals("Marshalled size must be three", 3, secondArray.size()); assertEquals("First element should be 1", 1, secondArray.get(0)); assertEquals("First element should be 2", 2, secondArray.get(1)); assertEquals("First element should be 3", 3, secondArray.get(2)); diff --git a/core/tests/utiltests/src/android/util/RemoteMemoryIntArrayService.java b/core/tests/utiltests/src/android/util/RemoteMemoryIntArrayService.java index 9264c6c86c3f..32dda6be1e2b 100644 --- a/core/tests/utiltests/src/android/util/RemoteMemoryIntArrayService.java +++ b/core/tests/utiltests/src/android/util/RemoteMemoryIntArrayService.java @@ -84,11 +84,7 @@ public class RemoteMemoryIntArrayService extends Service { @Override public int size() { synchronized (mLock) { - try { - return mArray.size(); - } catch (IOException e) { - throw new IllegalStateException(e); - } + return mArray.size(); } } diff --git a/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java b/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java index cd35f67a1369..be480b941586 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java @@ -308,11 +308,8 @@ final class GenerationRegistry { final long token = proto.start(GenerationRegistryProto.BACKING_STORES); final int key = mKeyToBackingStoreMap.keyAt(i); proto.write(BackingStoreProto.KEY, key); - try { - proto.write(BackingStoreProto.BACKING_STORE_SIZE, - mKeyToBackingStoreMap.valueAt(i).size()); - } catch (IOException ignore) { - } + proto.write(BackingStoreProto.BACKING_STORE_SIZE, + mKeyToBackingStoreMap.valueAt(i).size()); proto.write(BackingStoreProto.NUM_CACHED_ENTRIES, mKeyToIndexMapMap.get(key).size()); final ArrayMap<String, Integer> indexMap = mKeyToIndexMapMap.get(key); @@ -357,10 +354,7 @@ final class GenerationRegistry { pw.print("_Backing store for type:"); pw.print(SettingsState.settingTypeToString( SettingsState.getTypeFromKey(key))); pw.print(" user:"); pw.print(SettingsState.getUserIdFromKey(key)); - try { - pw.print(" size:" + mKeyToBackingStoreMap.valueAt(i).size()); - } catch (IOException ignore) { - } + pw.print(" size:" + mKeyToBackingStoreMap.valueAt(i).size()); pw.println(" cachedEntries:" + mKeyToIndexMapMap.get(key).size()); final ArrayMap<String, Integer> indexMap = mKeyToIndexMapMap.get(key); final MemoryIntArray backingStore = getBackingStoreLocked(key, |