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

Commit 4f47b40e authored by Andreas Gampe's avatar Andreas Gampe
Browse files

Frameworks/base: Compile-time optimize SQLiteConnection

Do not cache compiled regular expression and date formatter. Both
cannot be compile-time created, are expensive at preloading time,
and only used in the rare case of dumping the connection (mainly
on errors).

Bug: 19498458
Bug: 19542228
Change-Id: Ia38491a3f852ccf699b815ff05289b338e932f2a
parent 189d2785
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
@@ -91,8 +91,6 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];

    private static final Pattern TRIM_SQL_PATTERN = Pattern.compile("[\\s]*\\n+[\\s]*");

    private final CloseGuard mCloseGuard = CloseGuard.get();

    private final SQLiteConnectionPool mPool;
@@ -1203,7 +1201,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
    }

    private static String trimSqlForDisplay(String sql) {
        return TRIM_SQL_PATTERN.matcher(sql).replaceAll(" ");
        // Note: Creating and caching a regular expression is expensive at preload-time
        //       and stops compile-time initialization. This pattern is only used when
        //       dumping the connection, which is a rare (mainly error) case. So:
        //       DO NOT CACHE.
        return sql.replaceAll("[\\s]*\\n+[\\s]*", " ");
    }

    /**
@@ -1437,9 +1439,6 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
    }

    private static final class Operation {
        private static final SimpleDateFormat sDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        public long mStartTime;
        public long mEndTime;
        public String mKind;
@@ -1494,7 +1493,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
        }

        private String getFormattedStartTime() {
            return sDateFormat.format(new Date(mStartTime));
            // Note: SimpleDateFormat is not thread-safe, cannot be compile-time created, and is
            //       relatively expensive to create during preloading. This method is only used
            //       when dumping a connection, which is a rare (mainly error) case. So:
            //       DO NOT CACHE.
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(mStartTime));
        }
    }
}