From 664adf32706274333fe1053dc01853b117c31bce Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 29 Nov 2014 19:00:53 +0100 Subject: Optimize emph insertion Avoid unnecessary malloc/free if opener is removed. --- src/inlines.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/inlines.c b/src/inlines.c index 8487bf8..2652342 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -430,26 +430,26 @@ S_insert_emph(subject *subj, delimiter_stack *opener, delimiter_stack *closer) tempstack = nextstack; } - // create new emph or strong, and splice it in to our inlines - // between the opener and closer - emph = use_delims == 1 ? make_emph(inl->next) : make_strong(inl->next); - emph->next = closer->first_inline; - emph->prev = inl; - emph->parent = inl->parent; - inl->next = emph; - // if opener has 0 delims, remove it and its associated inline if (opener->delim_count == 0) { // replace empty opener inline with emph chunk_free(&(inl->as.literal)); - inl->type = emph->type; - inl->next = emph->next; - inl->first_child = emph->first_child; - free(emph); emph = inl; + emph->type = use_delims == 1 ? NODE_EMPH : NODE_STRONG; + emph->first_child = inl->next; // remove opener from stack remove_delimiter(subj, opener); } + else { + // create new emph or strong, and splice it in to our inlines + // between the opener and closer + emph = use_delims == 1 ? make_emph(inl->next) : make_strong(inl->next); + emph->parent = inl->parent; + emph->prev = inl; + inl->next = emph; + } + + emph->next = closer->first_inline; // fix tree structure tmp = emph->first_child; -- cgit v1.2.3