From 1ef9d5938e671ce7a9d5c29d95c1364d6d6249ea Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 8 Apr 2014 16:42:24 +0900 Subject: [PATCH] [CB26] Remove useless variables. This is nice for code simplicity, but it has a small adverse impact on performance. Bug: 13406701 Change-Id: I9316b3fe8d79b940fbd83cf1187c8c0d10b26c71 --- .../inputmethod/latin/LastComposedWord.java | 6 +- .../inputmethod/latin/WordComposer.java | 60 ++++--------------- 2 files changed, 13 insertions(+), 53 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LastComposedWord.java b/java/src/com/android/inputmethod/latin/LastComposedWord.java index 2a16ab5ab..232bf7407 100644 --- a/java/src/com/android/inputmethod/latin/LastComposedWord.java +++ b/java/src/com/android/inputmethod/latin/LastComposedWord.java @@ -44,7 +44,6 @@ public final class LastComposedWord { public static final String NOT_A_SEPARATOR = ""; - public final int[] mPrimaryKeyCodes; public final ArrayList mEvents; public final String mTypedWord; public final CharSequence mCommittedWord; @@ -57,16 +56,15 @@ public final class LastComposedWord { private boolean mActive; public static final LastComposedWord NOT_A_COMPOSED_WORD = - new LastComposedWord(null, new ArrayList(), null, "", "", + new LastComposedWord(new ArrayList(), null, "", "", NOT_A_SEPARATOR, null, WordComposer.CAPS_MODE_OFF); // Warning: this is using the passed objects as is and fully expects them to be // immutable. Do not fiddle with their contents after you passed them to this constructor. - public LastComposedWord(final int[] primaryKeyCodes, final ArrayList events, + public LastComposedWord(final ArrayList events, final InputPointers inputPointers, final String typedWord, final CharSequence committedWord, final String separatorString, final String prevWord, final int capitalizedMode) { - mPrimaryKeyCodes = primaryKeyCodes; if (inputPointers != null) { mInputPointers.copy(inputPointers); } diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index 02f18cdd3..1268e5aac 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -42,11 +42,6 @@ public final class WordComposer { private CombinerChain mCombinerChain; - // An array of code points representing the characters typed so far. - // The array is limited to MAX_WORD_LENGTH code points, but mTypedWord extends past that - // and mCodePointSize can go past that. If mCodePointSize is greater than MAX_WORD_LENGTH, - // this just does not contain the associated code points past MAX_WORD_LENGTH. - private int[] mPrimaryKeyCodes; // The list of events that served to compose this string. private final ArrayList mEvents; private final InputPointers mInputPointers = new InputPointers(MAX_WORD_LENGTH); @@ -71,7 +66,6 @@ public final class WordComposer { private int mCapsCount; private int mDigitsCount; private int mCapitalizedMode; - private int mTrailingSingleQuotesCount; // This is the number of code points entered so far. This is not limited to MAX_WORD_LENGTH. // In general, this contains the size of mPrimaryKeyCodes, except when this is greater than // MAX_WORD_LENGTH in which case mPrimaryKeyCodes only contain the first MAX_WORD_LENGTH @@ -86,10 +80,8 @@ public final class WordComposer { public WordComposer() { mCombinerChain = new CombinerChain(); - mPrimaryKeyCodes = new int[MAX_WORD_LENGTH]; mEvents = CollectionUtils.newArrayList(); mAutoCorrection = null; - mTrailingSingleQuotesCount = 0; mIsResumed = false; mIsBatchMode = false; mCursorPositionWithinWord = 0; @@ -108,7 +100,6 @@ public final class WordComposer { mCapsCount = 0; mDigitsCount = 0; mIsFirstCharCapitalized = false; - mTrailingSingleQuotesCount = 0; mIsResumed = false; mIsBatchMode = false; mCursorPositionWithinWord = 0; @@ -143,10 +134,7 @@ public final class WordComposer { */ public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( final int[] destination, final int maxSize) { - int i = mTypedWordCache.length() - 1; - while (i >= 0 && mTypedWordCache.charAt(i) == Constants.CODE_SINGLE_QUOTE) { - --i; - } + final int i = mTypedWordCache.length() - 1 - trailingSingleQuotesCount(); if (i < 0) { // The string is empty or contains only single quotes. return 0; @@ -198,28 +186,8 @@ public final class WordComposer { if (0 == mCodePointSize) { mIsFirstCharCapitalized = false; } - if (Constants.CODE_DELETE == event.mKeyCode) { - if (mTrailingSingleQuotesCount > 0) { - --mTrailingSingleQuotesCount; - } else { - // Delete, but we didn't end in a quote: must recompute mTrailingSingleQuotesCount - // We're only searching for single quotes, so no need to account for code points - for (int i = mTypedWordCache.length() - 1; i > 0; --i) { - if (Constants.CODE_SINGLE_QUOTE != mTypedWordCache.charAt(i)) { - break; - } - ++mTrailingSingleQuotesCount; - } - } - } else { - if (Constants.CODE_SINGLE_QUOTE == primaryCode) { - ++mTrailingSingleQuotesCount; - } else { - mTrailingSingleQuotesCount = 0; - } + if (Constants.CODE_DELETE != event.mKeyCode) { if (newIndex < MAX_WORD_LENGTH) { - mPrimaryKeyCodes[newIndex] = primaryCode >= Constants.CODE_SPACE - ? Character.toLowerCase(primaryCode) : primaryCode; // In the batch input mode, the {@code mInputPointers} holds batch input points and // shouldn't be overridden by the "typed key" coordinates // (See {@link #setBatchInputWord}). @@ -263,15 +231,8 @@ public final class WordComposer { mCombinerChain.reset(); int actualMoveAmountWithinWord = 0; int cursorPos = mCursorPositionWithinWord; - final int[] codePoints; - if (mCodePointSize >= MAX_WORD_LENGTH) { - // If we have more than MAX_WORD_LENGTH characters, we don't have everything inside - // mPrimaryKeyCodes. This should be rare enough that we can afford to just compute - // the array on the fly when this happens. - codePoints = StringUtils.toCodePointArray(mTypedWordCache); - } else { - codePoints = mPrimaryKeyCodes; - } + // TODO: Don't make that copy. We can do this directly from mTypedWordCache. + final int[] codePoints = StringUtils.toCodePointArray(mTypedWordCache); if (expectedMoveAmount >= 0) { // Moving the cursor forward for the expected amount or until the end of the word has // been reached, whichever comes first. @@ -353,7 +314,12 @@ public final class WordComposer { } public int trailingSingleQuotesCount() { - return mTrailingSingleQuotesCount; + final int lastIndex = mTypedWordCache.length() - 1; + int i = lastIndex; + while (i >= 0 && mTypedWordCache.charAt(i) == Constants.CODE_SINGLE_QUOTE) { + --i; + } + return lastIndex - i; } /** @@ -443,9 +409,7 @@ public final class WordComposer { // Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK // or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate // the last composed word to ensure this does not happen. - final int[] primaryKeyCodes = mPrimaryKeyCodes; - mPrimaryKeyCodes = new int[MAX_WORD_LENGTH]; - final LastComposedWord lastComposedWord = new LastComposedWord(primaryKeyCodes, mEvents, + final LastComposedWord lastComposedWord = new LastComposedWord(mEvents, mInputPointers, mTypedWordCache.toString(), committedWord, separatorString, prevWord, mCapitalizedMode); mInputPointers.reset(); @@ -460,7 +424,6 @@ public final class WordComposer { mCombinerChain.reset(); mEvents.clear(); mCodePointSize = 0; - mTrailingSingleQuotesCount = 0; mIsFirstCharCapitalized = false; mCapitalizedMode = CAPS_MODE_OFF; refreshTypedWordCache(); @@ -480,7 +443,6 @@ public final class WordComposer { public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord, final String previousWord) { - mPrimaryKeyCodes = lastComposedWord.mPrimaryKeyCodes; mEvents.clear(); Collections.copy(mEvents, lastComposedWord.mEvents); mInputPointers.set(lastComposedWord.mInputPointers);