Skip to content

Commit

Permalink
Merge pull request sifive#42 from sifive/improve-memoization
Browse files Browse the repository at this point in the history
Remove bound on parser memoization cache
  • Loading branch information
nategraff-sifive authored Jan 28, 2020
2 parents b876edb + c310d29 commit f8ff165
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: Run unit tests
run:
make test-unit
- name: Run unit tests with bounded parser cache
run:
make test-cache-size-bound
- name: Run integration tests
run:
make test-integration
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ test-unit: venv/bin/activate $(UNIT_TESTS)
. $< && python3 -m unittest $(UNIT_TESTS)
test: test-unit

.PHONY: test-cache-size-bound
test-cache-size-bound: venv/bin/activate $(UNIT_TESTS)
PYDEVICETREE_CACHE_SIZE_BOUND=128 . $< && python3 -m unittest $(UNIT_TESTS)
test: test-cache-size-bound

INTEGRATION_TESTS = tests/test_full_trees.py

.PHONY: test-integration
Expand Down
16 changes: 14 additions & 2 deletions pydevicetree/source/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
# Copyright (c) 2019 SiFive Inc.
# SPDX-License-Identifier: Apache-2.0

import os
import sys

import pyparsing as p # type: ignore

p.ParserElement.enablePackrat()
ENV_CACHE_OPTION = "PYDEVICETREE_CACHE_SIZE_BOUND"

cache_bound = None
if ENV_CACHE_OPTION in os.environ:
option = os.environ[ENV_CACHE_OPTION]
if option != "None":
try:
cache_bound = int(option)
except ValueError:
print("%s requires a valid integer" % ENV_CACHE_OPTION, file=sys.stderr)
p.ParserElement.enablePackrat(cache_bound)

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

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

0 comments on commit f8ff165

Please sign in to comment.