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

Commit 1c08f482 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Apply fixes for EfficientStringsChecker.

The recently-built Error Prone checker has found many instances where
we're always paying the cost of StringBuilder concatenation, even in
the typical cases where preconditions are successfully met.

Benchmarks have shown that even when replacing these with varargs
formatter strings, the default case is 20x faster.

Bug: 170978902
Test: none
Exempt-From-Owner-Approval: trivial refactoring
Change-Id: If8c00bc73467bfb91ec16c162969c9d26ca53646
parent df8dc2b4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1377,8 +1377,8 @@ public final class SystemServiceRegistry {

    /** Throws {@link IllegalStateException} if not during a static initialization. */
    private static void ensureInitializing(String methodName) {
        Preconditions.checkState(sInitializing, "Internal error: " + methodName
                + " can only be called during class initialization.");
        Preconditions.checkState(sInitializing, "Internal error: %s"
                + " can only be called during class initialization.", methodName);
    }
    /**
     * Creates an array which is used to cache per-Context service instances.
+7 −12
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ public abstract class AtomicFormula extends IntegrityFormula {
    private final @Key int mKey;

    public AtomicFormula(@Key int key) {
        checkArgument(isValidKey(key), String.format("Unknown key: %d", key));
        checkArgument(isValidKey(key), "Unknown key: %d", key);
        mKey = key;
    }

@@ -149,8 +149,7 @@ public abstract class AtomicFormula extends IntegrityFormula {
            super(key);
            checkArgument(
                    key == VERSION_CODE,
                    String.format(
                            "Key %s cannot be used with LongAtomicFormula", keyToString(key)));
                    "Key %s cannot be used with LongAtomicFormula", keyToString(key));
            mValue = null;
            mOperator = null;
        }
@@ -168,10 +167,9 @@ public abstract class AtomicFormula extends IntegrityFormula {
            super(key);
            checkArgument(
                    key == VERSION_CODE,
                    String.format(
                            "Key %s cannot be used with LongAtomicFormula", keyToString(key)));
                    "Key %s cannot be used with LongAtomicFormula", keyToString(key));
            checkArgument(
                    isValidOperator(operator), String.format("Unknown operator: %d", operator));
                    isValidOperator(operator), "Unknown operator: %d", operator);
            mOperator = operator;
            mValue = value;
        }
@@ -317,8 +315,7 @@ public abstract class AtomicFormula extends IntegrityFormula {
                            || key == INSTALLER_CERTIFICATE
                            || key == INSTALLER_NAME
                            || key == STAMP_CERTIFICATE_HASH,
                    String.format(
                            "Key %s cannot be used with StringAtomicFormula", keyToString(key)));
                    "Key %s cannot be used with StringAtomicFormula", keyToString(key));
            mValue = null;
            mIsHashedValue = null;
        }
@@ -339,8 +336,7 @@ public abstract class AtomicFormula extends IntegrityFormula {
                            || key == INSTALLER_CERTIFICATE
                            || key == INSTALLER_NAME
                            || key == STAMP_CERTIFICATE_HASH,
                    String.format(
                            "Key %s cannot be used with StringAtomicFormula", keyToString(key)));
                    "Key %s cannot be used with StringAtomicFormula", keyToString(key));
            mValue = value;
            mIsHashedValue = isHashed;
        }
@@ -365,8 +361,7 @@ public abstract class AtomicFormula extends IntegrityFormula {
                            || key == INSTALLER_CERTIFICATE
                            || key == INSTALLER_NAME
                            || key == STAMP_CERTIFICATE_HASH,
                    String.format(
                            "Key %s cannot be used with StringAtomicFormula", keyToString(key)));
                    "Key %s cannot be used with StringAtomicFormula", keyToString(key));
            mValue = hashValue(key, value);
            mIsHashedValue =
                    (key == APP_CERTIFICATE
+6 −8
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ public final class CompoundFormula extends IntegrityFormula implements Parcelabl
     */
    public CompoundFormula(@Connector int connector, List<IntegrityFormula> formulas) {
        checkArgument(
                isValidConnector(connector), String.format("Unknown connector: %d", connector));
                isValidConnector(connector), "Unknown connector: %d", connector);
        validateFormulas(connector, formulas);
        this.mConnector = connector;
        this.mFormulas = Collections.unmodifiableList(formulas);
@@ -93,7 +93,7 @@ public final class CompoundFormula extends IntegrityFormula implements Parcelabl
    CompoundFormula(Parcel in) {
        mConnector = in.readInt();
        int length = in.readInt();
        checkArgument(length >= 0, "Must have non-negative length. Got " + length);
        checkArgument(length >= 0, "Must have non-negative length. Got %d", length);
        mFormulas = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            mFormulas.add(IntegrityFormula.readFromParcel(in));
@@ -196,16 +196,14 @@ public final class CompoundFormula extends IntegrityFormula implements Parcelabl
            case OR:
                checkArgument(
                        formulas.size() >= 2,
                        String.format(
                        "Connector %s must have at least 2 formulas",
                                connectorToString(connector)));
                        connectorToString(connector));
                break;
            case NOT:
                checkArgument(
                        formulas.size() == 1,
                        String.format(
                        "Connector %s must have 1 formula only",
                                connectorToString(connector)));
                        connectorToString(connector));
                break;
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public class IntegrityUtils {
    public static byte[] getBytesFromHexDigest(String hexDigest) {
        checkArgument(
                hexDigest.length() % 2 == 0,
                "Invalid hex encoding " + hexDigest + ": must have even length");
                "Invalid hex encoding %s: must have even length", hexDigest);

        byte[] rawBytes = new byte[hexDigest.length() / 2];
        for (int i = 0; i < rawBytes.length; i++) {
+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ public final class Rule implements Parcelable {
    private final @Effect int mEffect;

    public Rule(@NonNull IntegrityFormula formula, @Effect int effect) {
        checkArgument(isValidEffect(effect), String.format("Unknown effect: %d", effect));
        checkArgument(isValidEffect(effect), "Unknown effect: %d", effect);
        this.mFormula = Objects.requireNonNull(formula);
        this.mEffect = effect;
    }
Loading