gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.37k stars 172 forks source link

Reusing renderer instance #230

Closed baryluk closed 2 years ago

baryluk commented 2 years ago

Hi,

I am wondering, for a provided html renderer specifically.

Two questions:

  1. Can one create it once and use it multiple times for different markdowns. From the source code it looks like yes.
  2. Can it be used from multiple threads safely without external locking. From the source code it looks like also yes.

html renderer appears to just carry data that is initialized on creation only, then its content is not modified, thus my claims 1 and 2 above.

But, these are implicit assumptions. I really would like for the both to be true, so the creation of the renderer can be moved out of the loop when processing big numbers of markdown inputs. (Parser still needs to be created from scratch obviously).

kjk commented 2 years ago

You can't use it multi-threaded: we change Renderer state so it'll be buggy if done from multiple threads. -race flag typically exposes multi-threading issues.

You can't re-use html renderer because it carries per-markdown state (e.g. headingIDs).

You could add a Reset() method but I doubt that would be a significant perf win as Renderer state is small and therefore fast to allocate.

baryluk commented 2 years ago

You are right @kjk - headingIDs makes it a no go. I didn't notice it before.

Adding Reset() is not a good idea either.