Skip to content

Commit

Permalink
Improve performance of math and logical operations
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jan 14, 2021
1 parent 17a6e82 commit b301f41
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import javassist.CannotCompileException;
import javassist.CtBehavior;
import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.text.MessageFormatter;
import org.panda_lang.utilities.commons.text.Formatter;

public final class CtCode<T extends CtBehavior> {

private final T behavior;
private final MessageFormatter formatter = new MessageFormatter();
private final Formatter formatter = new Formatter();

private CtCode(T behavior) {
this.behavior = behavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,70 @@
package org.panda_lang.utilities.commons.text;

import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.UnsafeUtils;
import org.panda_lang.utilities.commons.function.Option;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public final class MessageFormatter {
public final class Formatter {

private final Map<String, Supplier<?>> placeholders;
private final Function<String, Option<Exception>> verifier;

public MessageFormatter(Map<String, Supplier<?>> placeholders) {
public Formatter(Map<String, Supplier<?>> placeholders, Function<String, Option<Exception>> verifier) {
this.placeholders = placeholders;
this.verifier = verifier;
}

public MessageFormatter() {
this(new LinkedHashMap<>());
public Formatter(Function<String, Option<Exception>> verifier) {
this(new LinkedHashMap<>(), verifier);
}

public Formatter() {
this(new LinkedHashMap<>(), message -> Option.none());
}

public String format(String message) {
for (Map.Entry<String, Supplier<?>> placeholderEntry : placeholders.entrySet()) {
String key = placeholderEntry.getKey();
Object value = placeholderEntry.getValue().get();

if (!message.contains(key)) {
continue;
}

message = StringUtils.replace(message, key, value != null ? value.toString() : "<value not specified>");
Object value = placeholderEntry.getValue().get();

if (value == null) {
throw new NullPointerException("Placeholder " + key + " returns null value");
}

message = StringUtils.replace(message, key, Objects.toString(value));
}

Option<Exception> verificationResult = verifier.apply(message);

if (verificationResult.isPresent()) {
return UnsafeUtils.throwException(verificationResult.get());
}

return message;
}

public MessageFormatter register(String placeholder, Object value) {
public Formatter register(String placeholder, Object value) {
return register(placeholder, value::toString);
}

public MessageFormatter register(String placeholder, Supplier<?> value) {
public Formatter register(String placeholder, Supplier<?> value) {
this.placeholders.put(placeholder, value);
return this;
}

public MessageFormatter fork() {
return new MessageFormatter(new LinkedHashMap<>(this.placeholders));
public Formatter fork() {
return new Formatter(new LinkedHashMap<>(this.placeholders), verifier);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.number.NumberPriorities;

import java.util.function.Function;

final class CreaseExpression extends NumberPriorities implements DynamicExpression {

private final Accessor<?> accessor;
private final boolean grow;
private final boolean post;
private final int priority;
private final Function<Object, Object> function;

public CreaseExpression(Accessor<?> accessor, boolean grow, boolean post) {
this.accessor = accessor;
this.grow = grow;
this.post = post;
this.priority = getPriority(accessor.getKnownType());
this.function = toFunction(getPriority(accessor.getKnownType()));
}

@Override
Expand All @@ -46,34 +48,40 @@ public Object evaluate(ProcessStack stack, Object instance) throws Exception {
MemoryContainer memory = accessor.fetchMemoryContainer(stack, instance);
Object before = memory.get(accessor.getMemoryPointer());

Object after = of(before);
Object after = function.apply(before);
memory.set(accessor.getMemoryPointer(), after);

return post ? before : after;
}

private Object of(Object value) {
private Function<Object, Object> toFunction(int priority) {
switch (priority) {
case INT:
int intValue = (int) value;
return grow ? ++intValue : --intValue;
return grow
? value -> (int) value + 1
: value -> (int) value - 1;
case LONG:
long longValue = (long) value;
return grow ? ++longValue : --longValue;
return grow
? value -> (long) value + 1L
: value -> (long) value - 1L;
case DOUBLE:
double doubleValue = (double) value;
return grow ? ++doubleValue : --doubleValue;
return grow
? value -> (double) value + 1.0D
: value -> (double) value - 1.0D;
case FLOAT:
float floatValue = (float) value;
return grow ? ++floatValue : --floatValue;
return grow
? value -> (float) value + 1.0F
: value -> (float) value - 1.0F;
case BYTE:
byte byteValue = (byte) value;
return grow ? ++byteValue : --byteValue;
return grow
? value -> (byte) value + 1
: value -> (byte) value - 1;
case SHORT:
short shortValue = (short) value;
return grow ? ++shortValue : --shortValue;
return grow
? value -> (short) value + 1
: value -> (short) value - 1;
default:
throw new PandaParserException("Unknown number type: " + value);
throw new PandaParserException("Unknown number type: " + priority);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public final class ConcatenationOperatorSubparser implements OperationSubparser
List<Expression> values = new ArrayList<>((operation.getElements().size() - 1) / 2);
int lastIndex = 0;

for (int i = 0; i < operation.getElements().size(); i++) {
Operation.OperationElement element = operation.getElements().get(i);
for (int index = 0; index < operation.getElements().size(); index++) {
Operation.OperationElement element = operation.getElements().get(index);

if (element.isExpression() || !Operators.ADDITION.equals(element.getOperator())) {
continue;
}

if (!parseSubOperation(parser, context, values, operation, lastIndex, i)) {
if (!parseSubOperation(parser, context, values, operation, lastIndex, index)) {
return null;
}

lastIndex = i + 1;
lastIndex = index + 1;
}

if (!parseSubOperation(parser, context, values, operation, lastIndex, operation.getElements().size())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public abstract class ComparisonOperator extends NumericOperation<Boolean> {

public abstract RPNOperationAction<Boolean> of(int compared, Expression a, Expression b);
public abstract RPNOperationAction<Boolean> of(int typePriority, Expression a, Expression b);

@Override
public RPNOperationAction<Boolean> of(TypeLoader typeLoader, Expression a, Expression b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,43 @@
package org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.subparsers.logical;

import org.panda_lang.language.architecture.expression.Expression;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.language.interpreter.parser.PandaParserException;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.rpn.RPNOperationAction;

import java.util.function.BiFunction;

public final class GreaterThanOperator extends ComparisonOperator {

@Override
public RPNOperationAction<Boolean> of(int compared, Expression a, Expression b) {
public RPNOperationAction<Boolean> of(int typePriority, Expression a, Expression b) {
BiFunction<Number, Number, Boolean> comparison = toFunction(typePriority);

return new ComparisonOperatorAction(a, b) {
@Override
public Boolean get(ProcessStack stack, Object instance, Number a, Number b) {
switch (compared) {
case INT:
return a.intValue() > b.intValue();
case LONG:
return a.longValue() > b.longValue();
case DOUBLE:
return a.doubleValue() > b.doubleValue();
case FLOAT:
return a.floatValue() > b.floatValue();
case BYTE:
return a.byteValue() > b.byteValue();
case SHORT:
return a.shortValue() > b.shortValue();
default:
throw new PandaParserException("Unknown type " + compared);
}
return comparison.apply(a, b);
}
};
}

private BiFunction<Number, Number, Boolean> toFunction(int priority) {
switch (priority) {
case BYTE:
return (a, b) -> a.byteValue() > b.byteValue();
case SHORT:
return (a, b) -> a.shortValue() > b.shortValue();
case INT:
return (a, b) -> a.intValue() > b.intValue();
case LONG:
return (a, b) -> a.longValue() > b.longValue();
case FLOAT:
return (a, b) -> a.floatValue() > b.floatValue();
case DOUBLE:
return (a, b) -> a.doubleValue() > b.doubleValue();
default:
throw new PandaParserException("Unknown type " + priority);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,45 @@
package org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.subparsers.logical;

import org.panda_lang.language.architecture.expression.Expression;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.language.interpreter.parser.PandaParserException;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.number.NumberPriorities;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.rpn.RPNOperationAction;

import java.util.function.BiFunction;

public final class GreaterThanOrEqualsOperator extends ComparisonOperator {

@Override
@SuppressWarnings("DuplicatedCode")
public RPNOperationAction<Boolean> of(int compared, Expression a, Expression b) {
public RPNOperationAction<Boolean> of(int typePriority, Expression a, Expression b) {
BiFunction<Number, Number, Boolean> comparison = toFunction(typePriority);

return new ComparisonOperatorAction(a, b) {
@Override
public Boolean get(ProcessStack stack, Object instance, Number a, Number b) {
switch (compared) {
case NumberPriorities.INT:
return a.intValue() >= b.intValue();
case NumberPriorities.LONG:
return a.longValue() >= b.longValue();
case NumberPriorities.DOUBLE:
return a.doubleValue() >= b.doubleValue();
case NumberPriorities.FLOAT:
return a.floatValue() >= b.floatValue();
case NumberPriorities.BYTE:
return a.byteValue() >= b.byteValue();
case NumberPriorities.SHORT:
return a.shortValue() >= b.shortValue();
default:
throw new PandaParserException("Unknown type " + compared);
}
return comparison.apply(a, b);
}
};
}

private BiFunction<Number, Number, Boolean> toFunction(int priority) {
switch (priority) {
case BYTE:
return (a, b) -> a.byteValue() >= b.byteValue();
case SHORT:
return (a, b) -> a.shortValue() >= b.shortValue();
case INT:
return (a, b) -> a.intValue() >= b.intValue();
case LONG:
return (a, b) -> a.longValue() >= b.longValue();
case FLOAT:
return (a, b) -> a.floatValue() >= b.floatValue();
case DOUBLE:
return (a, b) -> a.doubleValue() >= b.doubleValue();
default:
throw new PandaParserException("Unknown type " + priority);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,43 @@
package org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.subparsers.logical;

import org.panda_lang.language.architecture.expression.Expression;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.language.interpreter.parser.PandaParserException;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.number.NumberPriorities;
import org.panda_lang.language.runtime.ProcessStack;
import org.panda_lang.panda.language.resource.syntax.expressions.subparsers.operation.rpn.RPNOperationAction;

import java.util.function.BiFunction;

public final class LessThanOperator extends ComparisonOperator {

@Override
public RPNOperationAction<Boolean> of(int compared, Expression a, Expression b) {
public RPNOperationAction<Boolean> of(int typePriority, Expression a, Expression b) {
BiFunction<Number, Number, Boolean> comparison = toFunction(typePriority);

return new ComparisonOperatorAction(a, b) {
@Override
public Boolean get(ProcessStack stack, Object instance, Number a, Number b) {
switch (compared) {
case NumberPriorities.INT:
return a.intValue() < b.intValue();
case NumberPriorities.LONG:
return a.longValue() < b.longValue();
case NumberPriorities.DOUBLE:
return a.doubleValue() < b.doubleValue();
case NumberPriorities.FLOAT:
return a.floatValue() < b.floatValue();
case NumberPriorities.BYTE:
return a.byteValue() < b.byteValue();
case NumberPriorities.SHORT:
return a.shortValue() < b.shortValue();
default:
throw new PandaParserException("Unknown type " + compared);
}
return comparison.apply(a, b);
}
};
}

private BiFunction<Number, Number, Boolean> toFunction(int priority) {
switch (priority) {
case BYTE:
return (a, b) -> a.byteValue() < b.byteValue();
case SHORT:
return (a, b) -> a.shortValue() < b.shortValue();
case INT:
return (a, b) -> a.intValue() < b.intValue();
case LONG:
return (a, b) -> a.longValue() < b.longValue();
case FLOAT:
return (a, b) -> a.floatValue() < b.floatValue();
case DOUBLE:
return (a, b) -> a.doubleValue() < b.doubleValue();
default:
throw new PandaParserException("Unknown type " + priority);
}
}

}
Loading

0 comments on commit b301f41

Please sign in to comment.