LayoutInflater: Special case well-known View constructors

This avoids loading constructors with reflection for well-known View classes.

An average of 1300 app startups with and without this change shows the special
casing improves app startup time by about 2.5ms.

The classes listed here are taken from examining several app traces as well as
the similar list in AppCompatViewInflater.

Bug: 131421854
Change-Id: I676a50eec50b86fa0b385add4bc092a657d8e8bb
This commit is contained in:
Eric Holk
2019-05-02 13:41:55 -07:00
parent c0c7806ec0
commit cb694714f1

View File

@@ -53,6 +53,11 @@ public class PhoneLayoutInflater extends LayoutInflater {
call through to our super class.
*/
@Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
View fastView = fastCreateView(name, getContext(), attrs);
if (fastView != null) {
return fastView;
}
for (String prefix : sClassPrefixList) {
try {
View view = createView(name, prefix, attrs);
@@ -68,6 +73,67 @@ public class PhoneLayoutInflater extends LayoutInflater {
return super.onCreateView(name, attrs);
}
private static final View fastCreateView(String name, Context context, AttributeSet attrs) {
switch (name) {
case "ActionMenuView":
return new android.widget.ActionMenuView(context, attrs);
case "AutoCompleteTextView":
return new android.widget.AutoCompleteTextView(context, attrs);
case "Button":
return new android.widget.Button(context, attrs);
case "CheckBox":
return new android.widget.CheckBox(context, attrs);
case "CheckedTextView":
return new android.widget.CheckedTextView(context, attrs);
case "EditText":
return new android.widget.EditText(context, attrs);
case "FrameLayout":
return new android.widget.FrameLayout(context, attrs);
case "ImageButton":
return new android.widget.ImageButton(context, attrs);
case "ImageView":
return new android.widget.ImageView(context, attrs);
case "LinearLayout":
return new android.widget.LinearLayout(context, attrs);
case "ListView":
return new android.widget.ListView(context, attrs);
case "MultiAutoCompleteTextView":
return new android.widget.AutoCompleteTextView(context, attrs);
case "ProgressBar":
return new android.widget.ProgressBar(context, attrs);
case "RadioButton":
return new android.widget.RadioButton(context, attrs);
case "RatingBar":
return new android.widget.RatingBar(context, attrs);
case "RelativeLayout":
return new android.widget.RelativeLayout(context, attrs);
case "ScrollView":
return new android.widget.ScrollView(context, attrs);
case "SeekBar":
return new android.widget.SeekBar(context, attrs);
case "Space":
return new android.widget.Space(context, attrs);
case "Spinner":
return new android.widget.Spinner(context, attrs);
case "TextureView":
return new android.view.TextureView(context, attrs);
case "TextView":
return new android.widget.TextView(context, attrs);
case "ToggleButton":
return new android.widget.ToggleButton(context, attrs);
case "Toolbar":
return new android.widget.Toolbar(context, attrs);
case "View":
return new android.view.View(context, attrs);
case "ViewFlipper":
return new android.widget.ViewFlipper(context, attrs);
case "ViewStub":
return new android.view.ViewStub(context, attrs);
default:
return null;
}
}
public LayoutInflater cloneInContext(Context newContext) {
return new PhoneLayoutInflater(this, newContext);
}