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

Commit 06013278 authored by Danny Baumann's avatar Danny Baumann Committed by Adnan Begovic
Browse files

Improve search performance.

The normalization regex is slow (5-10ms per entry on mako), so better
pre-cache it. Also cache the icons, as the number of icons is vastly
smaller than the number of potential search results, so they'll be
reused very often.

The regex change alone brings the duration of a single filter run down
to ~20ms from ~1200ms on mako.

Change-Id: Ia0fa9c554d0d585a52c8a2b87eecbf045388c961
parent 65791348
Loading
Loading
Loading
Loading
+7 −10
Original line number Diff line number Diff line
@@ -332,22 +332,19 @@ public class SearchPopulator extends IntentService {
            int keyIndex = c.getColumnIndex(DatabaseContract.Settings.ACTION_KEY);
            while (c.moveToNext()) {
                byte[] data = c.getBlob(headerIndex);
                SearchInfo info = new SearchInfo();
                Header header = null;
                if (data != null) {
                    Parcel p = Parcel.obtain();
                    p.setDataPosition(0);
                    p.unmarshall(data, 0, data.length);
                    p.setDataPosition(0);
                    Header h = new Header();
                    h.readFromParcel(p);
                    info.header = h;
                }
                info.level = c.getInt(levelIndex);
                info.fragment = c.getString(fragmentIndex);
                info.title = c.getString(titleIndex);
                info.iconRes = c.getInt(iconIndex);
                info.parentTitle = c.getInt(parentIndex);
                info.key = c.getString(keyIndex);
                    header = new Header();
                    header.readFromParcel(p);
                }

                SearchInfo info = new SearchInfo(header,
                        c.getInt(levelIndex), c.getString(fragmentIndex), c.getString(titleIndex),
                        c.getInt(iconIndex), c.getInt(parentIndex), c.getString(keyIndex));
                infos.add(info);
            }
            c.close();
+31 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.preference.PreferenceActivity.Header;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -48,17 +49,35 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
    private Drawable mDefaultIcon;
    private int mMatchHighlightColor;

    public static class SearchInfo {
        public Header header;
        public int level;
        public String fragment;
        public String title;
        public int iconRes;
        public int parentTitle;
        public String key;
    private SparseArray<Drawable> mIconCache = new SparseArray<Drawable>();

    public static class SearchInfo {
        public final Header header;
        public final int level;
        public final String fragment;
        public final String title;
        public final int iconRes;
        public final int parentTitle;
        public final String key;

        private String mNormalizedTitle;
        private int mMatchStart;
        private int mMatchEnd;

        public SearchInfo(Header header, int level, String fragment, String title,
                int iconRes, int parentTitle, String key) {
            this.header = header;
            this.level = level;
            this.fragment = fragment;
            this.title = title;
            this.iconRes = iconRes;
            this.parentTitle = parentTitle;
            this.key = key;

            mNormalizedTitle = removeNonAlphaNumeric(title.toLowerCase());
            mMatchStart = -1;
            mMatchEnd = -1;
        }
    }

    public SettingsSearchFilterAdapter(Context context, int resourceId,
@@ -92,9 +111,10 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
        SearchInfo info = mObjects.get(position);
        if (info != null) {
            if (holder.imageView != null) {
                Drawable d = null;
                Drawable d = mIconCache.get(info.iconRes);
                if (info.iconRes != 0) {
                    d = mResources.getDrawable(info.iconRes);
                    mIconCache.put(info.iconRes, d);
                }
                if (d == null) {
                    d = mDefaultIcon;
@@ -172,8 +192,8 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem

                for (int i = 0; i < mOriginalValues.size(); i++) {
                    SearchInfo item = mOriginalValues.get(i);
                    String title = item.title.toString().toLowerCase();
                    String filteredTitle = removeNonAlphaNumeric(title);
                    String title = item.title.toLowerCase();
                    String filteredTitle = item.mNormalizedTitle;

                    item.mMatchStart = -1;
                    item.mMatchEnd = -1;