Skip to content

Commit 47695db

Browse files
jasmith-hsclaude
andcommitted
Remove parent-chain cycle detection from ScopeMap constructor
The cycle detection allocated a HashSet and walked the full parent chain on every new Context (i.e. every enterScope in for loops). This was a defensive check added long ago but is not needed in normal operation and causes measurable overhead in hot template rendering paths. Benchmark: +37% throughput on complexTemplateBenchmark (3,316 -> 4,542 ops/s) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1c0d59c commit 47695db

1 file changed

Lines changed: 0 additions & 23 deletions

File tree

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)