-
Notifications
You must be signed in to change notification settings - Fork 50
perf: Use Cow to avoid creates String on format. #248
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
Conversation
我早就想优化这部分了,里面有太多的 format 不断创建新的 String。 Cow 正好解决这个问题,每次应该只有少量的情况有修改。 |
} | ||
|
||
/// Normalize chars to use general half width in Chinese contents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这行注释弄掉了,看起来像 AI 干的 😄
} else { | ||
result.severity = Severity::Error; | ||
if let Cow::Owned(new) = (self.format_fn)(&result.out) { | ||
if result.severity == Severity::Pass { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed changed check here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inside the Cow::Owned
block, which means it's changed already.
@@ -35,27 +37,27 @@ impl Rule { | |||
return; | |||
} | |||
|
|||
let new = (self.format_fn)(&result.out); | |||
if result.out.ne(&new) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed changed check here.
棒!👍👍👍 |
This pull request focuses on improving the performance and memory usage of the formatting functions by changing their return types to
Cow<str>
. This allows the functions to return either borrowed or owned data, reducing unnecessary allocations when the input is not modified.Most benchmarks gain 10% ~ 20% improvement on my local PC: