[PR #410] Optimized RendererBase #1008

Open
opened 2026-01-29 14:48:30 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/xoofx/markdig/pull/410

State: closed
Merged: Yes


This PR contains a few small optimizations in RendererBase.

It uses some recent C# features, such as pattern matching, to make the code more readable.

It does not changes the semantics of the code. In fact, I believe that the Write(MarkdownObject obj) method contains abug, and this PR does not attempt to fix it.

To understand why there may be a bug here, consider this block of code:

if (!renderersPerType.TryGetValue(objectType, out renderer))
{
    for (int i = 0; i < ObjectRenderers.Count; i++)
    {
        var testRenderer = ObjectRenderers[i];
        if (testRenderer.Accept(this, obj))
        {
            renderersPerType[objectType] = renderer = testRenderer;
            break;
        }
    }
}

Notice how renderersPerType caches renderers based on the type of the MarkdownObject, whereas the IMarkdownObjectRenderer.Accept(RendererBase renderer, MarkdownObject obj) method takes an actual MarkdownObject as a parameter, not just its type.

The easiest fix would be to change that method signature. Unfortunately, renderers exist that test instance property values in that method, e.g. NormalizeAutoLinkRenderer.

**Original Pull Request:** https://github.com/xoofx/markdig/pull/410 **State:** closed **Merged:** Yes --- This PR contains a few small optimizations in `RendererBase`. It uses some recent C# features, such as pattern matching, to make the code more readable. It does not changes the semantics of the code. In fact, I believe that the `Write(MarkdownObject obj)` method contains abug, and this PR does not attempt to fix it. To understand why there may be a bug here, consider this block of code: if (!renderersPerType.TryGetValue(objectType, out renderer)) { for (int i = 0; i < ObjectRenderers.Count; i++) { var testRenderer = ObjectRenderers[i]; if (testRenderer.Accept(this, obj)) { renderersPerType[objectType] = renderer = testRenderer; break; } } } Notice how renderersPerType caches renderers based on the type of the MarkdownObject, whereas the `IMarkdownObjectRenderer.Accept(RendererBase renderer, MarkdownObject obj)` method takes an actual MarkdownObject as a parameter, not just its type. The easiest fix would be to change that method signature. Unfortunately, renderers exist that test instance property values in that method, e.g. `NormalizeAutoLinkRenderer`.
claunia added the pull-request label 2026-01-29 14:48:30 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#1008