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

Commit 84aa61e2 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi Committed by Android (Google) Code Review
Browse files

Optimize remote control stack traversal order

Iterate of remote control stack entries from the top of the stack
 for cases where the condition being checked is more likely at
 the top of the stack, as opposed to the bottom, which is first
 accessed when using an iterator.

Change-Id: I625bee59021c2d652e9d6355b48dc0c11e36093f
parent 383f36cf
Loading
Loading
Loading
Loading
+75 −64
Original line number Original line Diff line number Diff line
@@ -5579,9 +5579,11 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        if(DEBUG_RC) Log.d(TAG, "onNewPlaybackInfoForRcc(id=" + rccId +
        if(DEBUG_RC) Log.d(TAG, "onNewPlaybackInfoForRcc(id=" + rccId +
                ", what=" + key + ",val=" + value + ")");
                ", what=" + key + ",val=" + value + ")");
        synchronized(mRCStack) {
        synchronized(mRCStack) {
            Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
            // iterating from top of stack as playback information changes are more likely
            while(stackIterator.hasNext()) {
            //   on entries at the top of the remote control stack
                RemoteControlStackEntry rcse = stackIterator.next();
            try {
                for (int index = mRCStack.size()-1; index >= 0; index--) {
                    final RemoteControlStackEntry rcse = mRCStack.elementAt(index);
                    if (rcse.mRccId == rccId) {
                    if (rcse.mRccId == rccId) {
                        switch (key) {
                        switch (key) {
                            case RemoteControlClient.PLAYBACKINFO_PLAYBACK_TYPE:
                            case RemoteControlClient.PLAYBACKINFO_PLAYBACK_TYPE:
@@ -5633,6 +5635,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        }
                        }
                        return;
                        return;
                    }
                    }
                }//for
            } catch (ArrayIndexOutOfBoundsException e) {
                // not expected to happen, indicates improper concurrent modification
                Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e);
            }
            }
        }
        }
    }
    }
@@ -5669,9 +5675,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
     */
     */
    private boolean checkUpdateRemoteStateIfActive(int streamType) {
    private boolean checkUpdateRemoteStateIfActive(int streamType) {
        synchronized(mRCStack) {
        synchronized(mRCStack) {
            Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
            // iterating from top of stack as active playback is more likely on entries at the top
            while(stackIterator.hasNext()) {
            try {
                RemoteControlStackEntry rcse = stackIterator.next();
                for (int index = mRCStack.size()-1; index >= 0; index--) {
                    final RemoteControlStackEntry rcse = mRCStack.elementAt(index);
                    if ((rcse.mPlaybackType == RemoteControlClient.PLAYBACK_TYPE_REMOTE)
                    if ((rcse.mPlaybackType == RemoteControlClient.PLAYBACK_TYPE_REMOTE)
                            && isPlaystateActive(rcse.mPlaybackState)
                            && isPlaystateActive(rcse.mPlaybackState)
                            && (rcse.mPlaybackStream == streamType)) {
                            && (rcse.mPlaybackStream == streamType)) {
@@ -5687,6 +5694,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        return true;
                        return true;
                    }
                    }
                }
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                // not expected to happen, indicates improper concurrent modification
                Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e);
            }
        }
        }
        synchronized (mMainRemote) {
        synchronized (mMainRemote) {
            mMainRemoteIsActive = false;
            mMainRemoteIsActive = false;