From 8c89af620080cccc7bd1d6d765e566ec2cdd4f19 Mon Sep 17 00:00:00 2001 From: choueric Date: Fri, 9 Jun 2017 10:20:58 +0800 Subject: [PATCH] add 'notesRecord' to check footnote existence fast It is necessary to use vector for 'notes' instead of map to keep footnotes ordered. But checking for presence in a vector is not efficient. So add a map variable 'notesRecord' to tackle this problem. --- inline.go | 2 ++ markdown.go | 13 +++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/inline.go b/inline.go index eff1383..ca22af5 100644 --- a/inline.go +++ b/inline.go @@ -488,6 +488,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int { } p.notes = append(p.notes, ref) + p.notesRecord[string(ref.link)] = true link = ref.link title = ref.title @@ -501,6 +502,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int { if t == linkDeferredFootnote && !p.isFootnote(lr) { lr.noteId = len(p.notes) + 1 p.notes = append(p.notes, lr) + p.notesRecord[string(lr.link)] = true } // keep link and title from reference diff --git a/markdown.go b/markdown.go index c82be6b..ee03fda 100644 --- a/markdown.go +++ b/markdown.go @@ -218,7 +218,8 @@ type parser struct { // Footnotes need to be ordered as well as available to quickly check for // presence. If a ref is also a footnote, it's stored both in refs and here // in notes. Slice is nil if footnotes not enabled. - notes []*reference + notes []*reference + notesRecord map[string]bool } func (p *parser) getRef(refid string) (ref *reference, found bool) { @@ -242,13 +243,8 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) { } func (p *parser) isFootnote(ref *reference) bool { - for _, v := range p.notes { - if string(ref.link) == string(v.link) { - return true - } - } - - return false + _, ok := p.notesRecord[string(ref.link)] + return ok } // @@ -386,6 +382,7 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte { if extensions&EXTENSION_FOOTNOTES != 0 { p.notes = make([]*reference, 0) + p.notesRecord = make(map[string]bool) } first := firstPass(p, input)