Skip to content

Commit

Permalink
Optimize S_find_first_nonspace.
Browse files Browse the repository at this point in the history
We were needlessly redoing things we'd already done.
Now we skip the work if the first nonspace is greater
than the current offset.

This fixes pathological slowdown with deeply nested
lists (#255).  For N = 3000, the time goes from over
17s to about 0.7s.

Thanks to @mity for diagnosing the problem.
  • Loading branch information
jgm committed Apr 15, 2018
1 parent 55d7b88 commit d51bb0e
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,24 @@ static void S_find_first_nonspace(cmark_parser *parser, cmark_chunk *input) {
char c;
int chars_to_tab = TAB_STOP - (parser->column % TAB_STOP);

parser->first_nonspace = parser->offset;
parser->first_nonspace_column = parser->column;
while ((c = peek_at(input, parser->first_nonspace))) {
if (c == ' ') {
parser->first_nonspace += 1;
parser->first_nonspace_column += 1;
chars_to_tab = chars_to_tab - 1;
if (chars_to_tab == 0) {
if (parser->first_nonspace <= parser->offset) {
parser->first_nonspace = parser->offset;
parser->first_nonspace_column = parser->column;
while ((c = peek_at(input, parser->first_nonspace))) {
if (c == ' ') {
parser->first_nonspace += 1;
parser->first_nonspace_column += 1;
chars_to_tab = chars_to_tab - 1;
if (chars_to_tab == 0) {
chars_to_tab = TAB_STOP;
}
} else if (c == '\t') {
parser->first_nonspace += 1;
parser->first_nonspace_column += chars_to_tab;
chars_to_tab = TAB_STOP;
} else {
break;
}
} else if (c == '\t') {
parser->first_nonspace += 1;
parser->first_nonspace_column += chars_to_tab;
chars_to_tab = TAB_STOP;
} else {
break;
}
}

Expand Down Expand Up @@ -1160,6 +1162,9 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,

parser->offset = 0;
parser->column = 0;
parser->first_nonspace = 0;
parser->first_nonspace_column = 0;
parser->indent = 0;
parser->blank = false;
parser->partially_consumed_tab = false;

Expand Down

0 comments on commit d51bb0e

Please sign in to comment.