yatatsu / AutoBundle

AutoBundle generates boilerplate code for field binding with android.os.Bundle
Apache License 2.0
135 stars 13 forks source link

Why not using instanceof operator in bind method? #12

Closed Piasy closed 8 years ago

Piasy commented 8 years ago

With v3.0.0, one of the generated bind method looked like below:

  public void bind(Object target, Intent intent) {
    final String className = target.getClass().getCanonicalName();
    if (className.equals(ProfileActivity.class.getName())) {
      ProfileActivityAutoBundle.bind((ProfileActivity)target, intent);
      return;
    }
  }

Why do you generate code to check their class name rather than use instanceof operator?

I create two methods to profiling their performance like below:

private void testInstanceOf() {
    Application app = getApplication();
    for (int i = 0; i < 1_000_000; i++) {
        if (app instanceof DialogInterface.OnClickListener) {
            Log.d("MainActivity", "can't be there");
        }
    }
}

private void testNameEquals() {
    Application app = getApplication();
    String name = app.getClass().getName();
    for (int i = 0; i < 1_000_000; i++) {
        if (name.equals("android.content.DialogInterface$OnClickListener")) {
            Log.d("MainActivity", "can't be there");
        }
    }
}

Their performance are close, time unit is ns:

D/MainActivity::testInstanceOf: 14030000
D/MainActivity::testNameEquals: 14689000
yatatsu commented 8 years ago

There is reason. As you said, I think it should improve performance by replacing name comparison to using instanceof. I' ll try it in a few days. Thanks!

yatatsu commented 8 years ago

3.1.0 has released with fix this issue. Thanks @Piasy !