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

Commit 305ef9e1 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Migrate hot codepaths to formatSimple().

The recently added TextUtils.formatSimple() can efficiently format
simple strings, roughly 6.5x faster than using String.format().

This change starts using this new method in hot codepaths identified
from pprof traces, and this CL alone should reduce system_process CPU
usage by 0.24%.  Linked bug has detailed pprof screenshots.

Bug: 170978902
Test: atest FrameworksCoreTests:android.text.TextUtilsTest
Exempt-From-Owner-Approval: trivial refactoring
Change-Id: Ie5a21a24be6dfcdb29768fe235e20f050fc111c7
parent c76708bc
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package android.service.notification;

import static android.app.NotificationChannel.PLACEHOLDER_CONVERSATION_ID;
import static android.text.TextUtils.formatSimple;

import android.annotation.NonNull;
import android.app.Notification;
@@ -31,8 +31,6 @@ import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;

import com.android.internal.logging.InstanceId;
import com.android.internal.logging.nano.MetricsProto;
@@ -258,7 +256,7 @@ public class StatusBarNotification implements Parcelable {

    @Override
    public String toString() {
        return String.format(
        return formatSimple(
                "StatusBarNotification(pkg=%s user=%s id=%d tag=%s key=%s: %s)",
                this.pkg, this.user, this.id, this.tag,
                this.key, this.notification);
+2 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -697,7 +698,7 @@ public class DateFormat {
    }

    private static String zeroPad(int inValue, int inMinDigits) {
        return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
        return TextUtils.formatSimple("%0" + inMinDigits + "d", inValue);
    }

    /**
+2 −3
Original line number Diff line number Diff line
@@ -60,10 +60,9 @@ public final class LocalLog {
        }
        final String logLine;
        if (mUseLocalTimestamps) {
            logLine = String.format("%s - %s", LocalDateTime.now(), msg);
            logLine = LocalDateTime.now() + " - " + msg;
        } else {
            logLine = String.format(
                    "%s / %s - %s", SystemClock.elapsedRealtime(), Instant.now(), msg);
            logLine = SystemClock.elapsedRealtime() + " / " + Instant.now() + " - " + msg;
        }
        append(logLine);
    }
+3 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.internal.compat;

import static android.text.TextUtils.formatSimple;

import android.annotation.IntDef;
import android.util.Log;
import android.util.Slog;
@@ -175,7 +177,7 @@ public final class ChangeReporter {
    }

    private void debugLog(int uid, long changeId, int state) {
        String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId,
        String message = formatSimple("Compat change id reported: %d; UID %d; state: %s", changeId,
                uid, stateToString(state));
        if (mSource == SOURCE_SYSTEM_SERVER) {
            Slog.d(TAG, message);
+2 −0
Original line number Diff line number Diff line
@@ -853,6 +853,8 @@ public class TextUtilsTest {

    @Test
    public void testFormatSimple_Advanced() {
        assertEquals("000000000000002a.ext",
                formatSimple("%016x.%s", 42, "ext"));
        assertEquals("crtcl=0x002a:intrsv=Y:grnk=0x0018:gsmry=A:example:rnk=0x0000",
                formatSimple("crtcl=0x%04x:intrsv=%c:grnk=0x%04x:gsmry=%c:%s:rnk=0x%04x",
                        42, 'Y', 24, 'A', "example", 0));
Loading