perf: Cache front matter format lookup - #2460
Open
ArnaudLigny wants to merge 1 commit into
Open
Conversation
- Move supported formats array to class constant SUPPORTED_FORMATS - Use strict type checking in in_array() call - Eliminates repeated array allocation on every convertFrontmatter() call - Improves performance of front matter parsing without behavior change Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes Cecil\Converter\Converter::convertFrontmatter() by avoiding per-call allocation of the supported-format list, moving it into a class constant and enabling strict checking in the format validation path.
Changes:
- Introduces a class constant to centralize the supported front matter formats list.
- Updates the
in_array()validation to use strict checking (true) and the new constant. - Keeps the conversion dispatch logic unchanged.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+32
to
+36
| /** | ||
| * Supported front matter formats. | ||
| */ | ||
| public const SUPPORTED_FORMATS = ['yaml', 'ini', 'toml', 'json']; | ||
|
|
Comment on lines
+52
to
54
| if (!\in_array($format, self::SUPPORTED_FORMATS, true)) { | ||
| throw new RuntimeException(\sprintf('The front matter format "%s" is not supported ("yaml", "ini", "toml" or "json").', $format)); | ||
| } |
| if (!\in_array($format, self::SUPPORTED_FORMATS, true)) { | ||
| throw new RuntimeException(\sprintf('The front matter format "%s" is not supported ("yaml", "ini", "toml" or "json").', $format)); | ||
| } | ||
| $method = \sprintf('convert%sToArray', ucfirst($format)); |
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.
Performance Improvement: Cache format lookup in Converter
Summary
This PR improves the performance of front matter parsing by eliminating repeated array allocation in the
convertFrontmatter()method.Changes
['yaml', 'ini', 'toml', 'json']to a class constantSUPPORTED_FORMATSin_array()call (prevents type juggling overhead)Impact
Testing
Performance Metrics