Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying the slowdown caused by logging #188

Merged
merged 1 commit into from
Jul 22, 2024
Merged

Conversation

humdrum
Copy link
Contributor

@humdrum humdrum commented Jul 19, 2024

What this PR does / why we need it:

  • Some logging messages take many times to make a message. (eg. toJSON()) So I modified the log functions to prevent performing message generation functions when the log level is low.
  • For Example, the toJSON() function won't performed If the logging level is higher than the trace.
  Logger.trace("trying to update a local change: \(self.toJSON())") 

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Improved logging efficiency by allowing deferred evaluation of log messages, enhancing performance.
  • Bug Fixes

    • Addressed unnecessary string interpolation in logging functions when the log level is lower than the message's severity.

@humdrum humdrum self-assigned this Jul 19, 2024
Copy link
Contributor

coderabbitai bot commented Jul 19, 2024

Walkthrough

The recent changes to the Logger enum enhance performance by modifying the message parameter type in various logging functions from String to @autoclosure () -> String. This allows messages to be evaluated only when necessary, optimizing logging efficiency by preventing unnecessary string interpolation for lower log levels. All major logging functions—trace, debug, info, warning, error, and critical—have been updated to embrace this new approach.

Changes

Files Change Summary
Sources/Core/Logger.swift Updated logging functions (trace, debug, info, warning, error, critical, and log) to accept message parameters as @autoclosure () -> String for deferred evaluation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Logger

    User->>Logger: Call log(level, message)
    Logger->>Logger: Check current log level
    alt Log level is sufficient
        Logger->>Logger: Evaluate message
        Logger->>Logger: Output log
    else Log level is insufficient
        Logger->>Logger: Skip message evaluation
    end
Loading

Poem

🐇 In the meadow where bunnies play,
New logging changes brighten our day!
Messages wait, no rush to unfold,
Performance shines, a sight to behold.
So hop with joy, let the logging begin,
With swift, clever logs, we’ll surely win! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Copy link

codecov bot commented Jul 19, 2024

Codecov Report

Attention: Patch coverage is 60.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 45.09%. Comparing base (6860357) to head (32f9047).

Files Patch % Lines
Sources/Core/Logger.swift 60.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #188      +/-   ##
==========================================
- Coverage   45.11%   45.09%   -0.03%     
==========================================
  Files         105      105              
  Lines       19320    19321       +1     
==========================================
- Hits         8716     8712       -4     
- Misses      10604    10609       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6860357 and 32f9047.

Files selected for processing (1)
  • Sources/Core/Logger.swift (1 hunks)
Additional comments not posted (6)
Sources/Core/Logger.swift (6)

33-34: LGTM! The use of @autoclosure improves performance.

The trace function now defers the evaluation of the message until it is needed, which is beneficial for performance.


37-38: LGTM! The use of @autoclosure improves performance.

The debug function now defers the evaluation of the message until it is needed, which is beneficial for performance.


41-42: LGTM! The use of @autoclosure improves performance.

The info function now defers the evaluation of the message until it is needed, which is beneficial for performance.


45-46: LGTM! The use of @autoclosure improves performance.

The warning function now defers the evaluation of the message until it is needed, which is beneficial for performance.


49-50: LGTM! The use of @autoclosure improves performance.

The error function now defers the evaluation of the message until it is needed, which is beneficial for performance.


53-54: LGTM! The use of @autoclosure improves performance.

The critical function now defers the evaluation of the message until it is needed, which is beneficial for performance.

Comment on lines +57 to +60
static func log(level: Logging.Logger.Level, _ message: @autoclosure () -> String, error: Error? = nil, filename: String = #file, function: String = #function, line: UInt = #line) {
guard Logger.logLevel <= level else { return }

var log = message()
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! The use of @autoclosure improves performance.

The log function now defers the evaluation of the message until it is needed, which is beneficial for performance.

However, the guard statement should be updated to use >= instead of <= to ensure that the log level is properly checked.

-        guard Logger.logLevel <= level else { return }
+        guard Logger.logLevel >= level else { return }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static func log(level: Logging.Logger.Level, _ message: @autoclosure () -> String, error: Error? = nil, filename: String = #file, function: String = #function, line: UInt = #line) {
guard Logger.logLevel <= level else { return }
var log = message()
static func log(level: Logging.Logger.Level, _ message: @autoclosure () -> String, error: Error? = nil, filename: String = #file, function: String = #function, line: UInt = #line) {
guard Logger.logLevel >= level else { return }
var log = message()

@humdrum humdrum merged commit 10b1870 into main Jul 22, 2024
3 checks passed
@humdrum humdrum deleted the fix_slowdown_by_log branch July 22, 2024 02:12
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