From 395ce0cdca068184d86898eabc838fadbf7b9c01 Mon Sep 17 00:00:00 2001 From: Anshu Rustagi Date: Mon, 20 Feb 2017 12:18:25 -0800 Subject: [PATCH] Specify SubscriberIndex size when initializing HashMap Initializing the HashMap with a specific size is an optimization for when we're adding a large number of subscribers to the index. By default the HashMap will only be created with a capacity of 4, so if we add a large number of subscribers the map will have to expand it's size several times. This can cause garbage collections that slow down App launch. --- .../EventBusAnnotationProcessor.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/EventBusAnnotationProcessor/src/org/greenrobot/eventbus/annotationprocessor/EventBusAnnotationProcessor.java b/EventBusAnnotationProcessor/src/org/greenrobot/eventbus/annotationprocessor/EventBusAnnotationProcessor.java index c37c18a5..b82401ab 100644 --- a/EventBusAnnotationProcessor/src/org/greenrobot/eventbus/annotationprocessor/EventBusAnnotationProcessor.java +++ b/EventBusAnnotationProcessor/src/org/greenrobot/eventbus/annotationprocessor/EventBusAnnotationProcessor.java @@ -318,6 +318,14 @@ private void createInfoIndexFile(String index) { if (myPackage != null) { writer.write("package " + myPackage + ";\n\n"); } + + int indexSize = methodsByClass.size() - classesToSkip.size(); + + // sanity check + if (indexSize < 0) { + indexSize = 0; + } + 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"); @@ -329,7 +337,7 @@ private void createInfoIndexFile(String index) { writer.write("public class " + clazz + " implements SubscriberInfoIndex {\n"); writer.write(" private static final Map, SubscriberInfo> SUBSCRIBER_INDEX;\n\n"); writer.write(" static {\n"); - writer.write(" SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>();\n\n"); + writer.write(" SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>(" + indexSize + ");\n\n"); writeIndexLines(writer, myPackage); writer.write(" }\n\n"); writer.write(" private static void putIndex(SubscriberInfo info) {\n");