Skip to content

Commit

Permalink
Fix performance issue when checking for interpolations
Browse files Browse the repository at this point in the history
Fixes omry#622
  • Loading branch information
odelalleau committed Mar 19, 2021
1 parent f221ae6 commit ec69e33
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions benchmark/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def iterate(seq: Any) -> None:
("${a:b,c,d}", ValueKind.INTERPOLATION),
("${${b}}", ValueKind.INTERPOLATION),
("${a:${b}}", ValueKind.INTERPOLATION),
("${foooooooooooooooooooo}_${bar}", ValueKind.INTERPOLATION),
],
)
def test_get_value_kind(
Expand Down
4 changes: 2 additions & 2 deletions omegaconf/grammar_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

# Build regex pattern to efficiently identify typical interpolations.
# See test `test_match_simple_interpolation_pattern` for examples.
_id = "[a-zA-Z_]\\w*" # foo, foo_bar, abc123
_config_key = f"({_id}|\\$)+" # foo, $bar, $foo$bar$
_config_key = r"[$\w]+" # foo, $0, $bar, $foo_$bar123$
_node_path = f"(\\.)*({_config_key}(\\.{_config_key})*)?" # foo, .foo.$bar
_node_inter = f"\\${{\\s*{_node_path}\\s*}}" # node interpolation ${foo.bar}
_id = "[a-zA-Z_]\\w*" # foo, foo_bar, abc123
_resolver_name = f"({_id}(\\.{_id})*)?" # foo, ns.bar3, ns_1.ns_2.b0z
_arg = "[a-zA-Z_0-9/\\-\\+.$%*@]+" # string representing a resolver argument
_args = f"{_arg}(\\s*,\\s*{_arg})*" # list of resolver arguments
Expand Down
3 changes: 2 additions & 1 deletion tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ def visit() -> Any:
"\\${foo}",
"${foo.bar:boz}",
"${$foo.bar$.x$y}",
"${$0.1.2$}",
"${0foo}",
# relative interpolations
"${.}",
"${..}",
Expand All @@ -548,7 +550,6 @@ def test_match_simple_interpolation_pattern(expression: str) -> None:
"expression",
[
"${foo",
"${0foo}",
"${0foo:bar}",
"${foo.${bar}}",
"${foo:${bar}}",
Expand Down

0 comments on commit ec69e33

Please sign in to comment.