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

Fix quadratic performance issue in list numbering #473

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ static bool is_autolink(cmark_node *node) {

static int S_render_node(cmark_renderer *renderer, cmark_node *node,
cmark_event_type ev_type, int options) {
cmark_node *tmp;
int list_number;
cmark_delim_type list_delim;
size_t numticks;
Expand Down Expand Up @@ -216,13 +215,8 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
marker_width = 4;
} else {
list_number = cmark_node_get_list_start(node->parent);
list_number = cmark_node_get_item_index(node);
list_delim = cmark_node_get_list_delim(node->parent);
tmp = node;
while (tmp->prev) {
tmp = tmp->prev;
list_number += 1;
}
// we ensure a width of at least 4 so
// we get nice transition from single digits
// to double
Expand Down
8 changes: 1 addition & 7 deletions src/man.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static void S_outc(cmark_renderer *renderer, cmark_escaping escape, int32_t c,

static int S_render_node(cmark_renderer *renderer, cmark_node *node,
cmark_event_type ev_type, int options) {
cmark_node *tmp;
int list_number;
bool entering = (ev_type == CMARK_EVENT_ENTER);
bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options);
Expand Down Expand Up @@ -125,12 +124,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
LIT("\\[bu] 2");
} else {
list_number = cmark_node_get_list_start(node->parent);
tmp = node;
while (tmp->prev) {
tmp = tmp->prev;
list_number += 1;
}
list_number = cmark_node_get_item_index(node);
char list_number_s[LIST_NUMBER_SIZE];
snprintf(list_number_s, LIST_NUMBER_SIZE, "\"%d.\" 4", list_number);
LIT(list_number_s);
Expand Down
25 changes: 25 additions & 0 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,31 @@ int cmark_node_set_list_tight(cmark_node *node, int tight) {
}
}

int cmark_node_get_item_index(cmark_node *node) {
if (node == NULL) {
return 0;
}

if (node->type == CMARK_NODE_ITEM) {
return node->as.list.start;
} else {
return 0;
}
}

int cmark_node_set_item_index(cmark_node *node, int idx) {
if (node == NULL || idx < 0) {
return 0;
}

if (node->type == CMARK_NODE_ITEM) {
node->as.list.start = idx;
return 1;
} else {
return 0;
}
}

const char *cmark_node_get_fence_info(cmark_node *node) {
if (node == NULL) {
return NULL;
Expand Down
11 changes: 11 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ struct cmark_node {

CMARK_EXPORT int cmark_node_check(cmark_node *node, FILE *out);

/**
* Returns item index of 'node'. This is only used when rendering output
* formats such as commonmark, which need to output the index. It is not
* required for formats such as html or latex.
*/
int cmark_node_get_item_index(cmark_node *node);

/** Sets item index of 'node'. Returns 1 on success, 0 on failure.
*/
int cmark_node_set_item_index(cmark_node *node, int idx);

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ char *cmark_render(cmark_node *root, int options, int width,

while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
cur = cmark_iter_get_node(iter);
if (cur->type == CMARK_NODE_ITEM) {
// Calculate the list item's index, for the benefit of output formats
// like commonmark and plaintext.
if (cur->prev) {
cmark_node_set_item_index(cur, 1 + cmark_node_get_item_index(cur->prev));
} else {
cmark_node_set_item_index(cur, cmark_node_get_list_start(cur->parent));
}
}
if (!render_node(&renderer, cur, ev_type, options)) {
// a false value causes us to skip processing
// the node's contents. this is used for
Expand Down