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:
Shawn Pearce
2013-08-05 15:19:57 -07:00
parent f4a6330088
commit 242a604b9f
8 changed files with 120 additions and 37 deletions

View File

@@ -27,7 +27,6 @@ import com.google.gerrit.httpd.auth.ldap.LdapAuthModule;
import com.google.gerrit.httpd.gitweb.GitWebModule;
import com.google.gerrit.httpd.rpc.UiRpcModule;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.CmdLineParserModule;
import com.google.gerrit.server.RemotePeer;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.config.CanonicalWebUrl;
@@ -130,7 +129,6 @@ public class WebModule extends FactoryModule {
DynamicSet.setOf(binder(), WebUiPlugin.class);
install(new AsyncReceiveCommits.Module());
install(new CmdLineParserModule());
bind(SocketAddress.class).annotatedWith(RemotePeer.class).toProvider(
HttpRemotePeerProvider.class).in(RequestScoped.class);

View File

@@ -16,15 +16,12 @@ package com.google.gerrit.pgm.util;
import com.google.gerrit.util.cli.CmdLineParser;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.gerrit.util.cli.OptionHandlers;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;
import java.io.StringWriter;
import java.util.Collections;
/** Base class for command line invocations of Gerrit Code Review. */
public abstract class AbstractProgram {
@@ -44,8 +41,7 @@ public abstract class AbstractProgram {
}
public final int main(final String[] argv) throws Exception {
final Injector empty = emptyInjector();
final CmdLineParser clp = new CmdLineParser(empty, this);
final CmdLineParser clp = new CmdLineParser(OptionHandlers.empty(), this);
try {
clp.parseArgument(argv);
} catch (CmdLineException err) {
@@ -81,10 +77,6 @@ public abstract class AbstractProgram {
}
}
private static Injector emptyInjector() {
return Guice.createInjector(Collections.<Module> emptyList());
}
/** Create a new exception to indicate we won't continue. */
protected static Die die(String why) {
return new Die(why);

View File

@@ -18,8 +18,6 @@ import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.args4j.AccountGroupIdHandler;
import com.google.gerrit.server.args4j.AccountGroupUUIDHandler;
import com.google.gerrit.server.args4j.AccountIdHandler;
@@ -28,11 +26,12 @@ import com.google.gerrit.server.args4j.ObjectIdHandler;
import com.google.gerrit.server.args4j.PatchSetIdHandler;
import com.google.gerrit.server.args4j.ProjectControlHandler;
import com.google.gerrit.server.args4j.SocketAddressHandler;
import com.google.gerrit.server.config.FactoryModule;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.util.cli.CmdLineParser;
import com.google.gerrit.util.cli.OptionHandlerUtil;
import com.google.gerrit.util.cli.OptionHandlers;
import org.eclipse.jgit.lib.ObjectId;
import org.kohsuke.args4j.spi.OptionHandler;
import java.net.SocketAddress;
@@ -44,6 +43,7 @@ public class CmdLineParserModule extends FactoryModule {
@Override
protected void configure() {
factory(CmdLineParser.Factory.class);
bind(OptionHandlers.class);
registerOptionHandler(Account.Id.class, AccountIdHandler.class);
registerOptionHandler(AccountGroup.Id.class, AccountGroupIdHandler.class);

View File

@@ -16,9 +16,11 @@ package com.google.gerrit.server.args4j;
import com.google.gerrit.common.ProjectUtil;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
@@ -27,17 +29,26 @@ import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class ProjectControlHandler extends OptionHandler<ProjectControl> {
private final ProjectControl.Factory projectControlFactory;
private static final Logger log = LoggerFactory
.getLogger(ProjectControlHandler.class);
private final ProjectControl.GenericFactory projectControlFactory;
private final Provider<CurrentUser> user;
@Inject
public ProjectControlHandler(
final ProjectControl.Factory projectControlFactory,
final ProjectControl.GenericFactory projectControlFactory,
Provider<CurrentUser> user,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<ProjectControl> setter) {
super(parser, option, setter);
this.projectControlFactory = projectControlFactory;
this.user = user;
}
@Override
@@ -59,13 +70,21 @@ public class ProjectControlHandler extends OptionHandler<ProjectControl> {
}
String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
Project.NameKey nameKey = new Project.NameKey(nameWithoutSuffix);
final ProjectControl control;
try {
Project.NameKey nameKey = new Project.NameKey(nameWithoutSuffix);
control = projectControlFactory.validateFor(nameKey, ProjectControl.OWNER | ProjectControl.VISIBLE);
control = projectControlFactory.validateFor(
nameKey,
ProjectControl.OWNER | ProjectControl.VISIBLE,
user.get());
} catch (NoSuchProjectException e) {
throw new CmdLineException(owner, e.getMessage());
} catch (IOException e) {
log.warn("Cannot load project " + nameWithoutSuffix, e);
throw new CmdLineException(
owner,
new NoSuchProjectException(nameKey).getMessage());
}
setter.addValue(control);

View File

@@ -29,6 +29,7 @@ import com.google.gerrit.rules.PrologModule;
import com.google.gerrit.rules.RulesCache;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.ApprovalsUtil;
import com.google.gerrit.server.CmdLineParserModule;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.InternalUser;
@@ -163,6 +164,7 @@ public class GerritGlobalModule extends FactoryModule {
install(ChangeCache.module());
install(new AccessControlModule());
install(new CmdLineParserModule());
install(new EmailModule());
install(new GitModule());
install(new PrologModule());

View File

@@ -14,12 +14,11 @@
package com.google.gerrit.sshd;
import static com.google.inject.Scopes.SINGLETON;
import static com.google.gerrit.extensions.registration.PrivateInternals_DynamicTypes.registerInParentInjectors;
import static com.google.inject.Scopes.SINGLETON;
import com.google.common.collect.Maps;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.CmdLineParserModule;
import com.google.gerrit.server.PeerDaemonUser;
import com.google.gerrit.server.RemotePeer;
import com.google.gerrit.server.config.FactoryModule;
@@ -68,7 +67,6 @@ public class SshModule extends FactoryModule {
configureRequestScope();
install(new AsyncReceiveCommits.Module());
install(new CmdLineParserModule());
configureAliases();
bind(SshLog.class);

View File

@@ -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));
}

View File

@@ -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;
}
}