Fix quadratic performance issue in list numbering#473
Conversation
|
Thanks for submitting this! I haven't had a chance to look at it in detail yet. |
|
This modifies the source document for no good reason. Why not store the current list index in a local variable? |
Sorry, it's been a really long time since I wrote this code and I don't remember all the details. The performance issue is caused by this code: Lines 221 to 225 in 7d450d8 It takes linear time to recompute the index, which is what causes the quadratic performance. I fixed it by caching the index on the node so that it doesn't need to be recomputed. As far as I can remember, I couldn't find a place to store it in a local variable, which is why I implemented it this way. |
|
We don't want to modify the source, but if there's a way to handle this with a local variable, great. In general I haven't been too worried about performance-related issues in the commonmark or man renderer. (HTML, yes.) And the issue is one that will only arise if you have an unusually long list. |
|
I just realized that for nested lists, you'd need a dynamic array to store list indexes. This would make things more complicated and adds a memory allocation to the rendering code which so far seems to be allocation-free. So I can see why the approach of storing indexes in the input document was chosen. |
|
The rendering code isn't allocation-free; |
|
Maybe a hashtable indexed by the node pointer? That would be functionally equivalent to storing data on the node itself, without actually modifying the node. |
|
Something very similar to the existing |
This is the same fix as github#322. It fixes the bug described in GHSA-r8vr-c48j-fcc5.
To reproduce the bug:
Increasing the number 10000 in the above command causes the running time to increase quadratically.
This PR replaces #472, which didn't work correctly (see github#321).