Skip to content

Commit f8ff165

Browse files
Merge pull request #42 from sifive/improve-memoization
Remove bound on parser memoization cache
2 parents b876edb + c310d29 commit f8ff165

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
- name: Run unit tests
3333
run:
3434
make test-unit
35+
- name: Run unit tests with bounded parser cache
36+
run:
37+
make test-cache-size-bound
3538
- name: Run integration tests
3639
run:
3740
make test-integration

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ test-unit: venv/bin/activate $(UNIT_TESTS)
4747
. $< && python3 -m unittest $(UNIT_TESTS)
4848
test: test-unit
4949

50+
.PHONY: test-cache-size-bound
51+
test-cache-size-bound: venv/bin/activate $(UNIT_TESTS)
52+
PYDEVICETREE_CACHE_SIZE_BOUND=128 . $< && python3 -m unittest $(UNIT_TESTS)
53+
test: test-cache-size-bound
54+
5055
INTEGRATION_TESTS = tests/test_full_trees.py
5156

5257
.PHONY: test-integration

pydevicetree/source/grammar.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
# Copyright (c) 2019 SiFive Inc.
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import os
6+
import sys
7+
58
import pyparsing as p # type: ignore
69

7-
p.ParserElement.enablePackrat()
10+
ENV_CACHE_OPTION = "PYDEVICETREE_CACHE_SIZE_BOUND"
11+
12+
cache_bound = None
13+
if ENV_CACHE_OPTION in os.environ:
14+
option = os.environ[ENV_CACHE_OPTION]
15+
if option != "None":
16+
try:
17+
cache_bound = int(option)
18+
except ValueError:
19+
print("%s requires a valid integer" % ENV_CACHE_OPTION, file=sys.stderr)
20+
p.ParserElement.enablePackrat(cache_bound)
821

922
node_name = p.Word(p.alphanums + ",.-+_") ^ p.Literal("/")
1023
integer = p.pyparsing_common.integer ^ (p.Literal("0x").suppress() + p.pyparsing_common.hex_integer)
@@ -59,6 +72,5 @@
5972
devicetree.ignore("//" + p.SkipTo(p.lineEnd))
6073

6174
if __name__ == "__main__":
62-
import sys
6375
if len(sys.argv) > 1:
6476
devicetree.parseFile(sys.argv[1]).pprint()

0 commit comments

Comments
 (0)