Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of call executions: P4_NeedLoosen by caching to local. #16

Merged
merged 2 commits into from Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ROADMAP.md
Expand Up @@ -19,6 +19,7 @@
- [ ] Auto generate documentation for functions and defined macros. Example: doxygen.
- [ ] Python Binding: example: cffi, misaka.
- [ ] Benchmark: example: json-c. `valgrind --tool=massif --massif-out-file=massif.out ./build/tests/test_example_json && ms_print massif.out ms_print.out`.
- [ ] Add depth setter.
- [x] Add Valgrind to the tests. Added in v1.3.0.
- [x] New Expression Kind: BackReference. Added in v1.2.0.
- [x] Add Flag Set/Unset functions. Added in v1.1.0.
Expand Down
9 changes: 6 additions & 3 deletions peppapeg.c
Expand Up @@ -578,13 +578,15 @@ P4_MatchSequence(P4_Source* s, P4_Expression* e) {
return NULL;
}

bool needloosen = P4_NeedLoosen(s, e);

P4_MarkPosition(s, startpos);

for (int i = 0; i < e->count; i++) {
member = e->members[i];

// Optional `WHITESPACE` and `COMMENT` are inserted between every member.
if (P4_NeedLoosen(s, e)
if (needloosen
&& i > 0) {
whitespace = P4_MatchSpacedExpressions(s, NULL);
if (!NO_ERROR(s)) goto finalize;
Expand Down Expand Up @@ -707,14 +709,15 @@ P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
min = e->repeat_min;
max = e->repeat_max;

bool needloosen = P4_NeedLoosen(s, e);
P4_Position startpos = P4_GetPosition(s);
P4_Token *head = NULL, *tail = NULL, *tok = NULL, *whitespace = NULL;

while (*P4_RemainingText(s) != '\0') {
P4_MarkPosition(s, before_implicit);

// SPACED rule expressions are inserted between every repetition.
if (P4_NeedLoosen(s, e)
if (needloosen
&& repeated > 0 ) {
whitespace = P4_MatchSpacedExpressions(s, NULL);
if (!NO_ERROR(s)) goto finalize;
Expand All @@ -727,7 +730,7 @@ P4_MatchRepeat(P4_Source* s, P4_Expression* e) {
assert(tok == NULL);

// considering the case: MATCH WHITESPACE MATCH WHITESPACE NO_MATCH
if (P4_NeedLoosen(s, e) // ^ ^
if (needloosen // ^ ^
&& repeated > 0) // ^ ^ we are here
P4_SetPosition(s, before_implicit); // ^ puke extra whitespace
// ^ now we are here
Expand Down