Make command line option parsing ~6500% faster
Guice does not like to pass an Injector as part of an @Assisted invocation of an object. More recent versions of Guice are logging a warning advising a performance boost of ~6500% is possible if the class stops accepting Injector in the constructor and instead uses a more concrete type. In this case Guice was very correct about the code being slow. Every option declared required a linear scan through the entire Injector stack looking for any candidate binding that might match the OptionHandlerFactory signature. This scan ran on each request, which is incredibly slow. Build the map once in the injector and retain as a singleton. Change-Id: Ib620e7bf1e24241bd373d2cb55c89c3fe3284645
This commit is contained in:
@@ -40,8 +40,6 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
import org.kohsuke.args4j.Argument;
|
||||
@@ -76,7 +74,7 @@ public class CmdLineParser {
|
||||
CmdLineParser create(Object bean);
|
||||
}
|
||||
|
||||
private final Injector injector;
|
||||
private final OptionHandlers handlers;
|
||||
private final MyParser parser;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -95,9 +93,9 @@ public class CmdLineParser {
|
||||
* annotations incorrectly.
|
||||
*/
|
||||
@Inject
|
||||
public CmdLineParser(final Injector injector, @Assisted final Object bean)
|
||||
public CmdLineParser(OptionHandlers handlers, @Assisted final Object bean)
|
||||
throws IllegalAnnotationError {
|
||||
this.injector = injector;
|
||||
this.handlers = handlers;
|
||||
this.parser = new MyParser(bean);
|
||||
}
|
||||
|
||||
@@ -334,16 +332,10 @@ public class CmdLineParser {
|
||||
return add(super.createOptionHandler(option, setter));
|
||||
}
|
||||
|
||||
final Key<OptionHandlerFactory<?>> key =
|
||||
OptionHandlerUtil.keyFor(setter.getType());
|
||||
Injector i = injector;
|
||||
while (i != null) {
|
||||
if (i.getBindings().containsKey(key)) {
|
||||
return add(i.getInstance(key).create(this, option, setter));
|
||||
}
|
||||
i = i.getParent();
|
||||
OptionHandlerFactory<?> factory = handlers.get(setter.getType());
|
||||
if (factory != null) {
|
||||
return factory.create(this, option, setter);
|
||||
}
|
||||
|
||||
return add(super.createOptionHandler(option, setter));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.util.cli;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.inject.Binding;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@Singleton
|
||||
public class OptionHandlers {
|
||||
public static OptionHandlers empty() {
|
||||
ImmutableMap<Class<?>, Provider<OptionHandlerFactory<?>>> m =
|
||||
ImmutableMap.of();
|
||||
return new OptionHandlers(m);
|
||||
}
|
||||
|
||||
private final ImmutableMap<Class<?>, Provider<OptionHandlerFactory<?>>> map;
|
||||
|
||||
@Inject
|
||||
OptionHandlers(Injector parent) {
|
||||
this(build(parent));
|
||||
}
|
||||
|
||||
OptionHandlers(ImmutableMap<Class<?>, Provider<OptionHandlerFactory<?>>> m) {
|
||||
this.map = m;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
OptionHandlerFactory<?> get(Class<?> type) {
|
||||
Provider<OptionHandlerFactory<?>> b = map.get(type);
|
||||
return b != null ? b.get() : null;
|
||||
}
|
||||
|
||||
private static ImmutableMap<Class<?>, Provider<OptionHandlerFactory<?>>> build(Injector i) {
|
||||
ImmutableMap.Builder<Class<?>, Provider<OptionHandlerFactory<?>>> map =
|
||||
ImmutableMap.builder();
|
||||
for (; i != null; i = i.getParent()) {
|
||||
for (Entry<Key<?>, Binding<?>> e : i.getBindings().entrySet()) {
|
||||
TypeLiteral<?> type = e.getKey().getTypeLiteral();
|
||||
if (type.getRawType() == OptionHandlerFactory.class
|
||||
&& e.getKey().getAnnotation() == null
|
||||
&& type.getType() instanceof ParameterizedType) {
|
||||
map.put(getType(type), cast(e.getValue()).getProvider());
|
||||
}
|
||||
}
|
||||
}
|
||||
return map.build();
|
||||
}
|
||||
|
||||
private static Class<?> getType(TypeLiteral<?> t) {
|
||||
ParameterizedType p = (ParameterizedType) t.getType();
|
||||
return (Class<?>) p.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
private static Binding<OptionHandlerFactory<?>> cast(Binding<?> e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Binding<OptionHandlerFactory<?>> b = (Binding<OptionHandlerFactory<?>>) e;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user