Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5fecb71b authored by Jared Duke's avatar Jared Duke
Browse files

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
parent bd5d5293
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -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));
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -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));
+1 −5
Original line number Diff line number Diff line
@@ -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);
                    }
                }
            }

+3 −9
Original line number Diff line number Diff line
@@ -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.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.println(" cachedEntries:" + mKeyToIndexMapMap.get(key).size());
                final ArrayMap<String, Integer> indexMap = mKeyToIndexMapMap.get(key);
                final MemoryIntArray backingStore = getBackingStoreLocked(key,