Use Dictionary.TryAdd instead of ContainsKey and indexer by reducing lookups.#917
Merged
xoofx merged 2 commits intoxoofx:masterfrom Jan 30, 2026
Merged
Use Dictionary.TryAdd instead of ContainsKey and indexer by reducing lookups.#917xoofx merged 2 commits intoxoofx:masterfrom
xoofx merged 2 commits intoxoofx:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves performance by replacing the ContainsKey + indexer pattern with the more efficient Dictionary.TryAdd method, which reduces dictionary lookups from two operations to one. A polyfill implementation is provided for older frameworks that don't have native TryAdd support.
Key Changes:
- Added a polyfill
DictionaryExtensions.TryAddfor frameworks prior to .NET Standard 2.1 - Replaced
ContainsKey+ indexer pattern withTryAddin three locations - Reduced dictionary lookup operations for better performance
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Markdig/Polyfills/DictionaryExtensions.cs | Adds TryAdd polyfill for older .NET frameworks |
| src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs | Uses TryAdd in Set method to reduce lookups |
| src/Markdig/Parsers/ParserList.cs | Uses TryAdd when counting opening characters |
| src/Markdig/Helpers/CharacterMap.cs | Uses TryAdd when building non-ASCII character map |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
gfoidl
reviewed
Nov 29, 2025
MihaZupan
approved these changes
Jan 4, 2026
Collaborator
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replace Dictionary.TryAdd instead of ContainsKey and indexer by reducing lookups.