Skip to content

Conversation

@snakefoot
Copy link
Contributor

No description provided.

@snakefoot snakefoot added this to the 6.0.4 milestone Sep 2, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 2, 2025

Walkthrough

Optimizes Append4DigitsZeroPadded in StringBuilderExt by adding a Span-based append path for newer frameworks under conditional compilation and retains the original per-digit appends as fallback. Also rewrites a numeric range check expression. The changes are applied in two places within the file.

Changes

Cohort / File(s) Summary
StringBuilder digit append optimization
src/NLog/Internal/StringBuilderExt.cs
Added conditional path for NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER using a Span buffer and single Append; kept original per-digit appends under #else; minor rewrite of range check (semantically identical). Applied in two method locations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I thump my paws—four digits race,
In spans they sprint, a lean embrace.
For older trails, I hop per-char,
For newer paths, I bound afar.
Two spots tweaked, the burrow’s neat—
Faster logs beneath my feet. 🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/NLog/Internal/StringBuilderExt.cs (2)

424-435: Prefer AppendSpan (no temp buffer) and avoid C# 12 collection expressions

The collection expression Span<char> buffer = [ ... ]; requires C# 12 and might break older LangVersion targets. Also, using AppendSpan(4) avoids the extra copy from a temporary span into StringBuilder.

Proposed replacement within this block:

-                Span<char> buffer =
-                [
-                    (char)(((number / 1000) % 10) + '0'),
-                    (char)(((number / 100) % 10) + '0'),
-                    (char)(((number / 10) % 10) + '0'),
-                    (char)((number % 10) + '0'),
-                ];
-                builder.Append(buffer); // Single Append instead of many
+                var dest = builder.AppendSpan(4);
+                dest[0] = (char)(((number / 1000) % 10) + '0');
+                dest[1] = (char)(((number / 100) % 10) + '0');
+                dest[2] = (char)(((number / 10) % 10) + '0');
+                dest[3] = (char)((number % 10) + '0');

If AppendSpan isn’t available for all targeted TFMs, alternatively use a non-C#12 form:

-                Span<char> buffer = [ /* digits */ ];
-                builder.Append(buffer);
+                Span<char> buffer = stackalloc char[4];
+                buffer[0] = (char)(((number / 1000) % 10) + '0');
+                buffer[1] = (char)(((number / 100) % 10) + '0');
+                buffer[2] = (char)(((number / 10) % 10) + '0');
+                buffer[3] = (char)((number % 10) + '0');
+                builder.Append(buffer);

415-443: Clarify input domain (expects 0..9999); negative values render incorrect digits

The fallback digit math will produce garbage for negatives (e.g., -1 yields '/000'). Current call sites seem to pass non-negative values (year, fraction), but make the contract explicit.

Consider adding a debug-time guard near the start of the method:

 internal static void Append4DigitsZeroPadded(this StringBuilder builder, int number)
 {
+    System.Diagnostics.Debug.Assert(number >= 0 && number < 10000, "Append4DigitsZeroPadded expects 0..9999");

Alternatively, document the assumption in XML comments if you want zero runtime impact in Release.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 5397b53 and 7339d9f.

📒 Files selected for processing (1)
  • src/NLog/Internal/StringBuilderExt.cs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/NLog/Internal/StringBuilderExt.cs (1)
src/NLog/LayoutRenderers/LongDateLayoutRenderer.cs (1)
  • Append (61-80)
🔇 Additional comments (1)
src/NLog/Internal/StringBuilderExt.cs (1)

418-421: LGTM: fast-path for 4-digit values

The 4-digit range check keeps yyyy fast and avoids the zero-padding path. Looks good.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 2, 2025

@snakefoot snakefoot merged commit 3f7c9c2 into NLog:dev Sep 2, 2025
6 checks passed
@snakefoot snakefoot changed the title StringBuilder - Apply Span instead of multiple Append Append4DigitsZeroPadded - Apply Span instead of multiple StringBuilder Append Sep 7, 2025
@snakefoot snakefoot changed the title Append4DigitsZeroPadded - Apply Span instead of multiple StringBuilder Append Append4DigitsZeroPadded - Use Span instead of multiple StringBuilder Append Sep 7, 2025
This was referenced Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant