FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

perf: Оптимизация экранирования символов в ЗаписьJSON by Stivo182 · Pull Request #1524 · EvilBeaver/OneScript · GitHub

perf: Оптимизация экранирования символов в ЗаписьJSON - #1524

Merged
EvilBeaver merged 1 commit into
EvilBeaver:developfrom
Stivo182:perf/escape-characters
Mar 19, 2025
Merged

perf: Оптимизация экранирования символов в ЗаписьJSON#1524
EvilBeaver merged 1 commit into
EvilBeaver:developfrom
Stivo182:perf/escape-characters

Conversation

Stivo182 commented Mar 18, 2025
edited by coderabbitai Bot
Loading

Copy link
Copy Markdown
Contributor

BenchmarkDotNet v0.14.0, Windows 10 (10.0.17763.7009/1809/October2018Update/Redstone5)
Intel Core i5-8500 CPU 3.00GHz (Coffee Lake), 1 CPU, 6 logical and 6 physical cores

Method Mean Error StdDev
EscapeCharacters_Before_20KB 506.38 us 4.595 us 4.298 us
EscapeCharacters_After_20KB 86.93 us 0.826 us 0.772 us
EscapeCharacters_Before_1MB 1,746,430.33 us 31,249.919 us 29,231.193 us
EscapeCharacters_After_1MB 6,302.07 us 79.793 us 74.638 us
  // * Legends *
  Mean   : Arithmetic mean of all measurements
  Error  : Half of 99.9% confidence interval
  StdDev : Standard deviation of all measurements
  1 us   : 1 Microsecond (0.000001 sec)

Summary by CodeRabbit

  • Refactor
    • Enhanced the process for handling special characters in JSON output, ensuring more efficient and reliable generation of valid JSON strings.

coderabbitai Bot commented Mar 18, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

Walkthrough

The EscapeCharacters method in the JSONWriter class has been refactored to streamline the character escaping process. Instead of initializing the StringBuilder with the input string and performing in-place replacements, the method now initializes an empty StringBuilder and appends segments of unescaped characters along with their corresponding escaped values when needed. A temporary variable is used to store the escaped representation, and final string assembly now includes explicit insertion of quotation marks. No public or exported entity declarations have been altered.

Changes

Files Changed Summary of Changes
src/OneScript.StandardLibrary/Json/JSONWriter.cs Refactored the EscapeCharacters method: removed in-place replacements, introduced a temporary escapedValue variable, and adjusted the logic to append unescaped segments and escaped characters sequentially.

Sequence Diagram(s)

sequenceDiagram
    participant C as Caller
    participant J as JSONWriter
    participant SB as StringBuilder
    participant LP as Loop Process

    C->>J: Call EscapeCharacters(sval)
    J->>SB: Initialize empty StringBuilder
    J->>LP: Begin loop through each character
    LP-->>LP: Check if character requires escaping
    alt Character needs escaping
        LP-->>SB: Append unescaped segment (if any)
        LP-->>SB: Append escaped value using temporary variable
    else No escaping needed
        LP-->>LP: Continue iterating
    end
    J->>SB: Append final segment and wrap with quotes
    J-->>C: Return final escaped JSON string
Loading

Poem

I'm a rabbit with a savvy hop,
Leaping through code that won't ever stop.
Escaping characters with flair and art,
My logic dances, a modular spark.
Hop along with clean, crisp code so bright—
A carrot crunch of pure delight!
🥕🐇 Happy coding in the moonlit night!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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.

coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/OneScript.StandardLibrary/Json/JSONWriter.cs (1)

126-203: Great optimization of the string escaping logic!

The modified approach builds the result string incrementally rather than using in-place replacements in a pre-populated StringBuilder. This provides significant performance improvements by:

  1. Minimizing character shifting operations
  2. Reducing unnecessary string manipulations for sequences that don't need escaping
  3. Processing the string in a single pass

The benchmark results confirming this optimization (mentioned in the PR description) show impressive gains - reducing execution time from ~1.7s to ~6ms for 1MB strings.

One minor suggestion:

Consider pre-allocating the StringBuilder capacity based on the input string length to avoid potential resizing operations:

-            var sb = new StringBuilder();
+            var sb = new StringBuilder(sval.Length + 10); // +10 for escaped chars and quotes
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00723cb and 2b9006e.

📒 Files selected for processing (1)
  • src/OneScript.StandardLibrary/Json/JSONWriter.cs (1 hunks)

Stivo182 changed the title pref: Оптимизация экранирования символов в ЗаписьJSON perf: Оптимизация экранирования символов в ЗаписьJSON Mar 18, 2025

Copy link
Copy Markdown
Owner

Спасибо, что залезаете в те места куда никто давно не ходит

EvilBeaver merged commit c1d7e4f into EvilBeaver:develop Mar 19, 2025
Stivo182 deleted the perf/escape-characters branch March 19, 2025 08:40
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL