Skip to content

Commit a6c5d0b

Browse files
authored
Merge pull request #1300 from HubSpot/jasmith_hubspot/jinjava-3.0-perf
Performance optimizations for jinjava 3.0 expression resolution
2 parents 7d75f4a + 17dcd0d commit a6c5d0b

5 files changed

Lines changed: 27 additions & 39 deletions

File tree

src/main/java/com/hubspot/jinjava/el/JinjavaInterpreterResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ Object wrap(Object value) {
284284
if (value == null) {
285285
return null;
286286
}
287+
if (value instanceof String || value instanceof Number || value instanceof Boolean) {
288+
return value;
289+
}
287290
if (value instanceof PyishSerializable) {
288291
return value;
289292
}

src/main/java/com/hubspot/jinjava/el/ext/AllowlistReturnTypeValidator.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final class AllowlistReturnTypeValidator {
1111
AllowlistReturnTypeValidator.create(
1212
ReturnTypeValidatorConfig.builder().addDefaultAllowlistGroups().build()
1313
);
14-
private final ConcurrentHashMap<String, Boolean> allowedReturnTypesCache;
14+
private final ConcurrentHashMap<Class<?>, Boolean> allowedReturnTypesCache;
1515

1616
private final ImmutableSet<String> allowedCanonicalClassPrefixes;
1717
private final ImmutableSet<String> allowedCanonicalClassNames;
@@ -47,20 +47,25 @@ public Object validateReturnType(Object o) {
4747
if (o == null) {
4848
return null;
4949
}
50+
if (o instanceof String || o instanceof Number || o instanceof Boolean) {
51+
return o;
52+
}
5053
Class<?> clazz = o.getClass();
5154
if (clazz.isArray() && allowArrays) {
5255
return o;
5356
}
54-
String canonicalClassName = clazz.getCanonicalName();
55-
if (canonicalClassName == null) {
56-
onRejectedClass.accept(clazz);
57-
return null;
58-
}
5957
boolean isAllowedClassName = allowedReturnTypesCache.computeIfAbsent(
60-
canonicalClassName,
61-
c ->
62-
allowedCanonicalClassNames.contains(canonicalClassName) ||
63-
allowedCanonicalClassPrefixes.stream().anyMatch(canonicalClassName::startsWith)
58+
clazz,
59+
c -> {
60+
String canonicalClassName = c.getCanonicalName();
61+
if (canonicalClassName == null) {
62+
return false;
63+
}
64+
return (
65+
allowedCanonicalClassNames.contains(canonicalClassName) ||
66+
allowedCanonicalClassPrefixes.stream().anyMatch(canonicalClassName::startsWith)
67+
);
68+
}
6469
);
6570
if (!isAllowedClassName) {
6671
onRejectedClass.accept(clazz);

src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,11 @@ public List<TemplateError> getErrorsCopy() {
977977
ThreadLocal.withInitial(Stack::new);
978978

979979
public static JinjavaInterpreter getCurrent() {
980-
if (CURRENT_INTERPRETER.get().isEmpty()) {
980+
Stack<JinjavaInterpreter> stack = CURRENT_INTERPRETER.get();
981+
if (stack.isEmpty()) {
981982
return null;
982983
}
983-
984-
return CURRENT_INTERPRETER.get().peek();
984+
return stack.peek();
985985
}
986986

987987
public static Optional<JinjavaInterpreter> getCurrentMaybe() {

src/main/java/com/hubspot/jinjava/objects/serialization/PyishObjectMapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ private static ObjectMapper getPyishObjectMapper() {
5757
}
5858

5959
public static String getAsUnquotedPyishString(Object val) {
60-
if (val != null) {
61-
return WhitespaceUtils.unquoteAndUnescape(getAsPyishString(val, true));
60+
if (val == null) {
61+
return "";
6262
}
63-
return "";
63+
if (val instanceof String || val instanceof Number || val instanceof Boolean) {
64+
return val.toString();
65+
}
66+
return WhitespaceUtils.unquoteAndUnescape(getAsPyishString(val, true));
6467
}
6568

6669
public static String getAsPyishString(Object val) {

src/main/java/com/hubspot/jinjava/util/ScopeMap.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package com.hubspot.jinjava.util;
22

3-
import static com.hubspot.jinjava.util.Logging.ENGINE_LOG;
4-
53
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
64
import java.util.ArrayList;
7-
import java.util.Arrays;
85
import java.util.Collection;
96
import java.util.HashMap;
107
import java.util.HashSet;
118
import java.util.Map;
129
import java.util.Set;
13-
import java.util.stream.Collectors;
1410
import javax.annotation.Nonnull;
1511

1612
public class ScopeMap<K, V> implements Map<K, V> {
@@ -25,25 +21,6 @@ public ScopeMap() {
2521
public ScopeMap(ScopeMap<K, V> parent) {
2622
this.scope = new HashMap<>();
2723
this.parent = parent;
28-
29-
Set<ScopeMap<K, V>> parents = new HashSet<>();
30-
if (parent != null) {
31-
ScopeMap<K, V> p = parent.getParent();
32-
while (p != null) {
33-
parents.add(p);
34-
if (parents.contains(parent)) {
35-
ENGINE_LOG.error(
36-
"Parent loop detected:\n{}",
37-
Arrays
38-
.stream(Thread.currentThread().getStackTrace())
39-
.map(StackTraceElement::toString)
40-
.collect(Collectors.joining("\n"))
41-
);
42-
break;
43-
}
44-
p = p.getParent();
45-
}
46-
}
4724
}
4825

4926
public ScopeMap(ScopeMap<K, V> parent, Map<K, V> scope) {

0 commit comments

Comments
 (0)