Skip to content

Commit

Permalink
GH-518 Remove reflections from initialization process and gain ~200ms
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed May 25, 2020
1 parent 88c0299 commit 50464ca
Show file tree
Hide file tree
Showing 28 changed files with 226 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,26 @@ public final class PipelineComponent<P extends Parser> extends Component<P> {

private static final Map<String, PipelineComponent<? extends Parser>> COMPONENTS = new HashMap<>();

private final Class<? extends PipelineComponents> container;

private PipelineComponent(Class<? extends PipelineComponents> container, String name, Class<P> type) {
private PipelineComponent(String name, Class<P> type) {
super(name, type, 0);
this.container = container;
}

@Override
public String toString() {
return container.getSimpleName() + "::" + super.toString();
return super.toString();
}

/**
* Create component
*
* @param container the parent container
* @param name the name of component
* @param type the type of component
* @param <T> generic type of component
* @return a new component
*/
@SuppressWarnings("unchecked")
public static <T extends Parser> PipelineComponent<T> of(Class<? extends PipelineComponents> container, String name, Class<T> type) {
return (PipelineComponent<T>) ofComponents(COMPONENTS, name, () -> new PipelineComponent<>(container, name, type));
public static <T extends Parser> PipelineComponent<T> of(String name, Class<T> type) {
return (PipelineComponent<T>) ofComponents(COMPONENTS, name, () -> new PipelineComponent<>(name, type));
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import org.panda_lang.framework.design.interpreter.parser.ContextParser;

import java.util.Arrays;
import java.util.Collection;

/**
* Default pipelines used by the framework
*/
public final class Pipelines implements PipelineComponents {
public final class Pipelines {

/**
* Text representation of {@link Pipelines#ALL}
Expand All @@ -30,7 +33,7 @@ public final class Pipelines implements PipelineComponents {
/**
* All pipelines
*/
public static final PipelineComponent<ContextParser> ALL = PipelineComponent.of(Pipelines.class, ALL_LABEL, ContextParser.class);
public static final PipelineComponent<ContextParser> ALL = PipelineComponent.of(ALL_LABEL, ContextParser.class);

/**
* Text representation of {@link Pipelines#HEAD}
Expand All @@ -39,7 +42,7 @@ public final class Pipelines implements PipelineComponents {
/**
* Head pipeline
*/
public static final PipelineComponent<ContextParser> HEAD = PipelineComponent.of(Pipelines.class, HEAD_LABEL, ContextParser.class);
public static final PipelineComponent<ContextParser> HEAD = PipelineComponent.of(HEAD_LABEL, ContextParser.class);

/**
* Text representation of {@link Pipelines#TYPE}
Expand All @@ -48,7 +51,7 @@ public final class Pipelines implements PipelineComponents {
/**
* Class type parsers, used by type parser
*/
public static final PipelineComponent<ContextParser> TYPE = PipelineComponent.of(Pipelines.class, TYPE_LABEL, ContextParser.class);
public static final PipelineComponent<ContextParser> TYPE = PipelineComponent.of(TYPE_LABEL, ContextParser.class);

/**
* Text representation of {@link Pipelines#SCOPE}
Expand All @@ -57,6 +60,15 @@ public final class Pipelines implements PipelineComponents {
/**
* Scope parsers
*/
public static final PipelineComponent<ContextParser> SCOPE = PipelineComponent.of(Pipelines.class, SCOPE_LABEL, ContextParser.class);
public static final PipelineComponent<ContextParser> SCOPE = PipelineComponent.of(SCOPE_LABEL, ContextParser.class);

/**
* Get collection of all components
*
* @return the collection of components
*/
public static Collection<PipelineComponent<?>> getPipelineComponents() {
return Arrays.asList(ALL, HEAD, TYPE, SCOPE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.panda_lang.framework.design.resource;

import io.vavr.control.Option;
import org.panda_lang.framework.design.interpreter.messenger.MessengerInitializer;
import org.panda_lang.framework.design.interpreter.messenger.MessengerOutputListener;
import org.panda_lang.framework.design.interpreter.parser.expression.ExpressionSubparsers;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelinePath;

Expand Down Expand Up @@ -44,6 +46,13 @@ public interface Resources {
*
* @return the messenger initializer
*/
MessengerInitializer getMessengerInitializer();
Option<MessengerInitializer> getMessengerInitializer();

/**
* Get output listener for {@link org.panda_lang.framework.design.interpreter.messenger.Messenger}
*
* @return the output listener to use by messenger
*/
Option<MessengerOutputListener> getOutputListener();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.panda_lang.framework.language.interpreter.messenger;

import io.vavr.control.Option;
import org.jetbrains.annotations.Nullable;
import org.panda_lang.framework.FrameworkController;
import org.panda_lang.framework.PandaFrameworkException;
import org.panda_lang.framework.design.interpreter.messenger.Messenger;
import org.panda_lang.framework.design.interpreter.messenger.MessengerFormatter;
Expand All @@ -34,25 +36,29 @@
public final class PandaMessenger implements Messenger {

private final Logger logger;
private final @Nullable MessengerInitializer initializer;
private final MessengerInitializer initializer;
private final MessengerFormatter formatter = new PandaMessengerFormatter(this);
private final List<MessengerMessageTranslator<Object>> translators = new ArrayList<>();
private MessengerOutputListener outputListener;
private boolean initialized;

public PandaMessenger(Logger logger, @Nullable MessengerInitializer initializer) {
public PandaMessenger(Logger logger, @Nullable MessengerInitializer initializer, @Nullable MessengerOutputListener outputListener) {
this.logger = logger;
this.initializer = initializer;
this.outputListener = new LoggerMessengerOutputListener(logger);
this.initializer = Option.of(initializer).getOrElse(messenger -> {});
this.outputListener = Option.of(outputListener).getOrElse(() -> new LoggerMessengerOutputListener(logger));
}

public PandaMessenger(FrameworkController controller) {
this(controller.getLogger(), controller.getResources().getMessengerInitializer().getOrNull(), controller.getResources().getOutputListener().getOrNull());
}

public PandaMessenger(Logger logger) {
this(logger, null);
this(logger, null, null);
}

@Override
public boolean send(Object message) {
if (!initialized && initializer != null) {
if (!initialized) {
initialized = true;
initializer.onInitialize(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.panda_lang.framework.language.resource;

import io.vavr.control.Option;
import org.panda_lang.framework.design.interpreter.messenger.MessengerInitializer;
import org.panda_lang.framework.design.interpreter.messenger.MessengerOutputListener;
import org.panda_lang.framework.design.interpreter.parser.expression.ExpressionSubparsers;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelinePath;
import org.panda_lang.framework.design.resource.Resources;
Expand All @@ -41,8 +43,13 @@ public PipelinePath getPipelinePath() {
}

@Override
public MessengerInitializer getMessengerInitializer() {
return builder.messengerInitializer;
public Option<MessengerInitializer> getMessengerInitializer() {
return Option.of(builder.messengerInitializer);
}

@Override
public Option<MessengerOutputListener> getOutputListener() {
return Option.of(builder.outputListener);
}

public static PandaResourcesBuilder builder() {
Expand All @@ -51,12 +58,18 @@ public static PandaResourcesBuilder builder() {

public static final class PandaResourcesBuilder {

public MessengerOutputListener outputListener;
public MessengerInitializer messengerInitializer;
public PipelinePath pipelinePath;
public ExpressionSubparsers expressionSubparsers;

private PandaResourcesBuilder() { }

public PandaResourcesBuilder withOutputListener(MessengerOutputListener outputListener) {
this.outputListener = outputListener;
return this;
}

public PandaResourcesBuilder withMessengerInitializer(MessengerInitializer messengerInitializer) {
this.messengerInitializer = messengerInitializer;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
import org.junit.jupiter.api.Test;
import org.panda_lang.framework.design.interpreter.parser.Parser;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelineComponent;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelineComponents;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelinePath;
import org.panda_lang.framework.language.interpreter.parser.PandaContext;
import org.panda_lang.framework.language.interpreter.token.PandaTokenInfo;
import org.panda_lang.framework.language.resource.syntax.TokenTypes;

import java.util.Arrays;

class PandaPipelinePathTest implements PipelineComponents {
class PandaPipelinePathTest {

private static final PipelineComponent<?> TEST_COMPONENT = PipelineComponent.of(PandaPipelinePathTest.class, "test", Parser.class);
private static final PipelineComponent<?> ANOTHER_TEST_COMPONENT = PipelineComponent.of(PandaPipelinePathTest.class, "another-test", Parser.class);
private static final PipelineComponent<?> TEST_COMPONENT = PipelineComponent.of("test", Parser.class);
private static final PipelineComponent<?> ANOTHER_TEST_COMPONENT = PipelineComponent.of("another-test", Parser.class);

private PipelinePath defaultPath;

Expand Down
30 changes: 22 additions & 8 deletions panda/src/main/java/org/panda_lang/panda/PandaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.assignation.AssignationParsers;
import org.slf4j.Logger;

import java.util.Arrays;

/**
* Simplify creation of Panda instance
*/
Expand All @@ -55,23 +57,35 @@ public Panda createPanda(Logger logger) {

// initialize messenger
.initializeMessenger()
.addLayouts(ExceptionTranslatorLayout.class)
.addLayouts(PandaLexerFailureTranslatorLayout.class, InterpreterFailureTranslatorLayout.class, ParserFailureTranslatorLayout.class)
.addLayouts(ProcessFailureTranslatorLayout.class)
.addDataFormatters(EnvironmentFormatter.class, ThrowableFormatter.class, StacktraceElementsFormatter.class, ExceptionFormatter.class)
.addDataFormatters(IndicatedSourceFormatter.class, ParserFailureFormatter.class)
.addDataFormatters(ProcessFailureFormatter.class)
.addLayouts(() -> Arrays.asList(
new ExceptionTranslatorLayout(),
new PandaLexerFailureTranslatorLayout(),
new InterpreterFailureTranslatorLayout(),
new ParserFailureTranslatorLayout(),
new ProcessFailureTranslatorLayout()
))
.addDataFormatters(() -> Arrays.asList(
new EnvironmentFormatter(),
new ThrowableFormatter(),
new StacktraceElementsFormatter(),
new ExceptionFormatter(),
new IndicatedSourceFormatter(),
new ParserFailureFormatter(),
new ProcessFailureFormatter()
))
.addDataMapper(new StacktraceMapper())
.collect()

// load pipelines
.initializePipelines()
.usePipelines(Pipelines.class, PandaPipeline.class)
.usePipelines(Pipelines.getPipelineComponents())
.usePipelines(PandaPipeline.getPipelineComponents())
.collect()

// load parsers and expressions subparsers
.initializeParsers()
.loadParsersClasses(PandaParsers.PARSERS, AssignationParsers.SUBPARSERS)
.loadParsers(PandaParsers.PARSERS)
.loadParsers(AssignationParsers.SUBPARSERS)
.loadDefaultExpressionSubparsers()
.collect()

Expand Down
Loading

0 comments on commit 50464ca

Please sign in to comment.