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

Commit 511a2054 authored by Charles Munger's avatar Charles Munger
Browse files

Avoid string and varargs allocations for preconditions checks in hot methods

Compiler explorer shows a 4x reduction in code size for the method on the hot path.

BUG=327646201
Change-Id: Idcd1e3cc62ce5c9360e0ab334b01ecb2c3d73be1
parent ae675357
Loading
Loading
Loading
Loading
+30 −20
Original line number Diff line number Diff line
@@ -29,11 +29,13 @@ import android.util.Log;
import com.android.internal.util.Preconditions;

import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.NeverInline;

import libcore.util.NativeAllocationRegistry;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
import java.util.Objects;

/**
@@ -83,6 +85,30 @@ public class MeasuredText {
        return mChars;
    }

    private void rangeCheck(int start, int end) {
        if (start < 0 || start > end || end > mChars.length) {
            throwRangeError(start, end);
        }
    }

    @NeverInline
    private void throwRangeError(int start, int end) {
        throw new IllegalArgumentException(String.format(Locale.US,
            "start(%d) end(%d) length(%d) out of bounds", start, end, mChars.length));
    }

    private void offsetCheck(int offset) {
        if (offset < 0 || offset >= mChars.length) {
            throwOffsetError(offset);
        }
    }

    @NeverInline
    private void throwOffsetError(int offset) {
        throw new IllegalArgumentException(String.format(Locale.US,
            "offset (%d) length(%d) out of bounds", offset, mChars.length));
    }

    /**
     * Returns the width of a given range.
     *
@@ -91,12 +117,7 @@ public class MeasuredText {
     */
    public @FloatRange(from = 0.0) @Px float getWidth(
            @IntRange(from = 0) int start, @IntRange(from = 0) int end) {
        Preconditions.checkArgument(0 <= start && start <= mChars.length,
                "start(%d) must be 0 <= start <= %d", start, mChars.length);
        Preconditions.checkArgument(0 <= end && end <= mChars.length,
                "end(%d) must be 0 <= end <= %d", end, mChars.length);
        Preconditions.checkArgument(start <= end,
                "start(%d) is larger than end(%d)", start, end);
        rangeCheck(start, end);
        return nGetWidth(mNativePtr, start, end);
    }

@@ -118,12 +139,7 @@ public class MeasuredText {
     */
    public void getBounds(@IntRange(from = 0) int start, @IntRange(from = 0) int end,
            @NonNull Rect rect) {
        Preconditions.checkArgument(0 <= start && start <= mChars.length,
                "start(%d) must be 0 <= start <= %d", start, mChars.length);
        Preconditions.checkArgument(0 <= end && end <= mChars.length,
                "end(%d) must be 0 <= end <= %d", end, mChars.length);
        Preconditions.checkArgument(start <= end,
                "start(%d) is larger than end(%d)", start, end);
        rangeCheck(start, end);
        Preconditions.checkNotNull(rect);
        nGetBounds(mNativePtr, mChars, start, end, rect);
    }
@@ -137,12 +153,7 @@ public class MeasuredText {
     */
    public void getFontMetricsInt(@IntRange(from = 0) int start, @IntRange(from = 0) int end,
            @NonNull Paint.FontMetricsInt outMetrics) {
        Preconditions.checkArgument(0 <= start && start <= mChars.length,
                "start(%d) must be 0 <= start <= %d", start, mChars.length);
        Preconditions.checkArgument(0 <= end && end <= mChars.length,
                "end(%d) must be 0 <= end <= %d", end, mChars.length);
        Preconditions.checkArgument(start <= end,
                "start(%d) is larger than end(%d)", start, end);
        rangeCheck(start, end);
        Objects.requireNonNull(outMetrics);

        long packed = nGetExtent(mNativePtr, mChars, start, end);
@@ -158,8 +169,7 @@ public class MeasuredText {
     * @param offset an offset of the character.
     */
    public @FloatRange(from = 0.0f) @Px float getCharWidthAt(@IntRange(from = 0) int offset) {
        Preconditions.checkArgument(0 <= offset && offset < mChars.length,
                "offset(%d) is larger than text length %d" + offset, mChars.length);
        offsetCheck(offset);
        return nGetCharWidthAt(mNativePtr, offset);
    }