EventBus全解析系列(四)

Subscriber Index

背景

  • Subscriber Index是EventBus 3.0新引入的功能,它能大幅提升初始注册过程的效率。其原理其实很简单,就是把运行时期做的事情放到了编译时期去做,从而提升了运行时期的效率。大家可以从EventBus作者的博客中的对比图得到感性的认识。 可以看到,开启了索引的3.0,其注册速度比2.4快了3到6倍。
    the resulting speed in registrations per second (higher is better)
  • 然而,该新功能是一个可选项,使用者必须通过一定的配置才能使得该新功能得以生效。

前提

  • 订阅者和订阅事件类必须是Public的,订阅者方法才能被索引到;另外,由于Java注解机制自身的限制,在匿名类中的@Subscribe注解是不能被识别的,从而不能生成索引。
  • 但这不会造成致命问题,因为在注册过程中存在这样的逻辑:如果使用不了索引,那么程序将自动使用反射机制。虽然这又回到了运行时期做事情的老路上,使得性能下降,但EventBus依然可以正常工作。

配置

Android Gradle 插件2.2.0之前,我们使用android-apt的方式,在编译期生成Subscriber Index类。所谓APT,即Annotation Processing Tool,其结合EventBus提供的EventBusAnnotationProcessor,就可以就在编译期,对源代码文件进行检测,找出其中的Annotation,从而生成目标文件,即Subscriber Index类。
然而,毕竟android-apt是第三方插件,自Android Gradle 插件2.2.0 之后,其官方提供了相同的能力,即annotationProcessor ,来完全代替 android-apt,配置起来更简单,推荐大家使用。

1.使用android-apt
buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
 
apply plugin: 'com.neenbedankt.android-apt'
 
dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
    apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
 
apt {
    arguments {
        eventBusIndex "com.monster.android.wild.MyEventBusIndex"
    }
}

com.monster.android.wild.MyEventBusIndex就是我们想要生成的Subscriber Index类

2.使用annotationProcessor
android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [eventBusIndex:'com.monster.android.wild.MyEventBusIndex']
            }
        }
    }
}

dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}   

同上,com.monster.android.wild.MyEventBusIndex就是我们想要生成的Subscriber Index类。可以看到,这样配置起来更简单。

3.代码配置

在build.gradle中配置完毕之后,再进行编译,Subscriber Index类MyEventBusIndex就会自动为我们生成了。
注意,我们的工程中需要有使用EventBus相关的代码,才能生成哦。

MyEventBusIndex_Path.JPG

接下来,在代码中,如果我们想使用Subscriber Index,就可以在构造EventBus时,如下这样

EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

但如果每次都这样写一大串,未免有些繁琐。既然我们引入了Subscriber Index,我们当然希望我们的工程中全部使用这样方式,这时,就可以这样

// 只设置一次,并且要在我们第一次使用EventBus之前进行设置
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// 这样每次获取到默认实例,都是使用Subscriber Index的了,代码得到了精简。
EventBus eventBus = EventBus.getDefault();

源代码

1.Subscriber Index类MyEventBusIndex是如何生成的

首先,我们需要明确,MyEventBusIndex是在编译时期,通过第三方提供的android-apt或者Gradle本身提供的annotationProcessor,结合EventBus提供的EventBusAnnotationProcessor,共同协作而生成的。而EventBusAnnotationProcessor由EventBus提供,也就说明具体生成逻辑是由EventBus控制,这算是经典的模板模式。
下面,我们大致看一下EventBusAnnotationProcessor是如何定义生成逻辑并生成目标Java类文件的。大家可以从EventBus 的Github上面看到完整源代码,这里只是简要的阐述:

@SupportedAnnotationTypes("org.greenrobot.eventbus.Subscribe")
@SupportedOptions(value = {"eventBusIndex", "verbose"})
public class EventBusAnnotationProcessor extends AbstractProcessor {

    // process()是该类的核心函数,就是在这里,实现收集和评估注解的代码,以及生成Java文件
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    // 保存订阅者的订阅方法信息
    private final ListMap<TypeElement, ExecutableElement> methodsByClass = new ListMap<>();
    // 保存需要跳过的订阅者
    private final Set<TypeElement> classesToSkip = new HashSet<>();
    
        try {
            // 从build.gradle中读取到arguments,即com.monster.android.wild.MyEventBusIndex
            String index = processingEnv.getOptions().get(OPTION_EVENT_BUS_INDEX);
            // 收集,填充methodsByClass 
            collectSubscribers(annotations, env, messager);
            // 评估,填充classesToSkip 
            checkForSubscribersToSkip(messager, indexPackage);

            if (!methodsByClass.isEmpty()) {
                // 生成java文件(根据methodsByClass和classesToSkip)
                createInfoIndexFile(index);
            } else {
            }
        } catch (RuntimeException e) {
        }
        return true;
    }
}

    // 生成java文件,其中有一部分是hardcode
    private void createInfoIndexFile(String index) {
        BufferedWriter writer = null;
        try {
            JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(index);
            int period = index.lastIndexOf('.');
            String myPackage = period > 0 ? index.substring(0, period) : null;
            String clazz = index.substring(period + 1);
            writer = new BufferedWriter(sourceFile.openWriter());
            if (myPackage != null) {
                writer.write("package " + myPackage + ";\n\n");
            }
            writer.write("import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;\n");
            writer.write("import org.greenrobot.eventbus.meta.SubscriberMethodInfo;\n");
            writer.write("import org.greenrobot.eventbus.meta.SubscriberInfo;\n");
            writer.write("import org.greenrobot.eventbus.meta.SubscriberInfoIndex;\n\n");
            writer.write("import org.greenrobot.eventbus.ThreadMode;\n\n");
            writer.write("import java.util.HashMap;\n");
            writer.write("import java.util.Map;\n\n");
            writer.write("/** This class is generated by EventBus, do not edit. */\n");
            writer.write("public class " + clazz + " implements SubscriberInfoIndex {\n");
            writer.write("    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;\n\n");
            writer.write("    static {\n");
            writer.write("        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();\n\n");
            writeIndexLines(writer, myPackage);
            writer.write("    }\n\n");
            writer.write("    private static void putIndex(SubscriberInfo info) {\n");
            writer.write("        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);\n");
            writer.write("    }\n\n");
            writer.write("    @Override\n");
            writer.write("    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {\n");
            writer.write("        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);\n");
            writer.write("        if (info != null) {\n");
            writer.write("            return info;\n");
            writer.write("        } else {\n");
            writer.write("            return null;\n");
            writer.write("        }\n");
            writer.write("    }\n");
            writer.write("}\n");
        } catch (IOException e) {
        } finally {
        }
    }

接下来我们看看生成的Subscriber Index类MyEventBusIndex

/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        // 从以上createInfoIndexFile()的实现来看,除了类名MyEventBusIndex,只有这一段代码是非hardcode。
        // 以订阅者类为单位,将该订阅者类中的所有订阅函数以及相关参数,封装到SimpleSubscriberInfo类对象中,
        // 以供EventBus在注册过程中使用。注意SimpleSubscriberInfo类对象是在编译时期就生成的,
        // 因而在运行时期可以直接使用,省去了运行时期通过反射做相似操作的时间和资源消耗,从而提高效率,这里就是全文的精髓所在。
        putIndex(new SimpleSubscriberInfo(com.monster.android.wild.myeventbusdemo.MainActivity.class, true,
                new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onEvent",
                    com.monster.android.wild.myeventbusdemo.MainActivity.EventBusEvent.class, ThreadMode.MAIN),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    // 将会在EventBus在注册过程中使用,等会大家会看到
    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}
2.EventBus是如何使用Subscriber Index类MyEventBusIndex的

我们知道,在EventBus注册的时候,需要调用SubscriberMethodFinder的findSubscriberMethods()方法。忽略无关代码如下:

    List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        if (ignoreGeneratedIndex) {
            subscriberMethods = findUsingReflection(subscriberClass);
        } else {
            subscriberMethods = findUsingInfo(subscriberClass);
        }
    }

默认情况下,ignoreGeneratedIndex为false,所以会调用findUsingInfo()

    private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            // 焦点调用在这里,如何通过索引获取订阅者信息
            findState.subscriberInfo = getSubscriberInfo(findState);
            if (findState.subscriberInfo != null) {
                SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
                for (SubscriberMethod subscriberMethod : array) {
                    if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
                        findState.subscriberMethods.add(subscriberMethod);
                    }
                }
            } else {
                // 这一点与上文提到的相呼应:索引找不到,还可以回到老路上,即,使用反射来找,
                // 虽然性能会降低,但保证EventBus仍然可以正常工作
                findUsingReflectionInSingleClass(findState);
            }
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }

顺着焦点调用,我们继续往下看

    private SubscriberInfo getSubscriberInfo(FindState findState) {
        if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
            SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
            if (findState.clazz == superclassInfo.getSubscriberClass()) {
                return superclassInfo;
            }
        }
        // 焦点在这里,轮询subscriberInfoIndexes得到SubscriberInfoIndex对象,从而得到SubscriberInfo对象
        if (subscriberInfoIndexes != null) {
            for (SubscriberInfoIndex index : subscriberInfoIndexes) {
                SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
                if (info != null) {
                    return info;
                }
            }
        }
        return null;
    }

插一句,那subscriberInfoIndexes如何得来的呢?还记得,我们在代码配置的时候调用过

EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build(); 

索引就是这个时候传给EventBus的,我们看一下源码:

    /** Adds an index generated by EventBus' annotation preprocessor. */
    public EventBusBuilder addIndex(SubscriberInfoIndex index) {
        if(subscriberInfoIndexes == null) {
            subscriberInfoIndexes = new ArrayList<>();
        }
        subscriberInfoIndexes.add(index);
        return this;
    }

再回来,所以得到的SubscriberInfoIndex对象就是我们的MyEventBusIndex类对象。接着调用MyEventBusIndex类对象的getSubscriberInfo()方法,从而得到SubscriberInfo对象,而这正与MyEventBusIndex源代码中“等会大家会看到”的注解相呼应!
至此,代码逻辑就捋顺了~~~

3.总结

到这里,如何生成Subscriber Index类以及如何使用Subscriber Index类就分析完毕了。分析过程中,大家应该就可以看到了,核心思想就是把运行时所需要做的耗时操作转移到了编译时期去做,从而提高了整体性能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,594评论 25 707
  • 博文出处:EventBus源码解析,欢迎大家关注我的博客,谢谢! 0001B 时近年末,但是也没闲着。最近正好在看...
    俞其荣阅读 1,256评论 1 16
  • 对于Android开发老司机来说肯定不会陌生,它是一个基于观察者模式的事件发布/订阅框架,开发者可以通过极少的代码...
    飞扬小米阅读 1,422评论 0 50
  • EventBus 简介 EventBus 直译过来就是事件总线,熟悉计算机原理的人一定很熟悉总线的概念,所有设备都...
    DanieX阅读 1,013评论 0 1
  • 照片选择
    coderJerry01阅读 200评论 0 0