Skip to content

Commit b61fbf9

Browse files
committed
[GR-10322] Preliminary thread-safety of statically cached parse trees
PullRequest: graalpython/90
2 parents e3888ba + 7abc7b9 commit b61fbf9

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
2929

30-
import java.util.HashMap;
31-
import java.util.Map;
30+
import java.util.concurrent.ConcurrentHashMap;
3231
import java.util.regex.Pattern;
3332

3433
import org.antlr.v4.runtime.CharStreams;
@@ -52,7 +51,7 @@
5251
import com.oracle.truffle.api.source.SourceSection;
5352

5453
public final class PythonParserImpl implements PythonParser {
55-
private static final Map<String, ParserRuleContext> cachedParseTrees = new HashMap<>();
54+
private static final ConcurrentHashMap<String, ParserRuleContext> cachedParseTrees = new ConcurrentHashMap<>();
5655

5756
private static Python3Parser getPython3Parser(CodePointCharStream fromString) {
5857
Python3Parser parser = new Builder.Parser(fromString).build();
@@ -75,11 +74,7 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
7574
Python3Parser parser = getPython3Parser(fromString);
7675
ParserRuleContext input;
7776
if (!core.isInitialized()) {
78-
input = cachedParseTrees.get(fileDirAndName);
79-
if (input == null) {
80-
input = parser.file_input();
81-
cachedParseTrees.put(fileDirAndName, input);
82-
}
77+
input = cachedParseTrees.computeIfAbsent(fileDirAndName, (key) -> parser.file_input());
8378
} else {
8479
try {
8580
if (source.isInteractive()) {

0 commit comments

Comments
 (0)