8000 perf: Optimize format_word function performance by using direct unicode mapping by quake · Pull Request #245 · huacnlee/autocorrect · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

perf: Optimize format_word function performance by using direct unicode mapping #245

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 8000 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

Merged
merged 3 commits into from
Mar 23, 2025

Conversation

quake
Copy link
Contributor
@quake quake commented Mar 22, 2025

This PR optimizes the format_word fn to improve performance when converting full-width characters to half-width.

Changes

  • Replaced character-by-character string operations with direct Unicode code point mapping
  • Eliminated unnecessary string allocations by using a more efficient character transformation approach

This change should result in lower CPU usage and memory allocation when processing text with many full-width characters. for example, run the format_json_2k benchmark on my local pc:

format_json_2k          time:   [25.914 ms 25.944 ms 25.976 ms]
                        change: [-11.110% -10.959% -10.807%] (p = 0.00 < 0.05)
                        Performance has improved.

// checked char is in range of fullwidth number and alphabetic
unsafe { char::from_u32_unchecked(c as u32 - 0xFEE0) }
}
'\u{3000}' => ' ',
Copy link
Owner

Choose a reason for hiding this comment

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

你怎么知道我之前那个看不见的空格是 \u{3000} 的? 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)

U+FF00 does not correspond to a fullwidth ASCII 20 (space character), since that role is already fulfilled by U+3000 "ideographic space".

let out = text
.chars()
.map(|c| match c {
'\u{FF10}'..='\u{FF19}' | '\u{FF21}'..='\u{FF3A}' | '\u{FF41}'..='\u{FF5A}' => {
Copy link
Owner

Choose a reason for hiding this comment

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

这个 range 有没有参考链接,我看一下范围的情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

看起来这里可以改成完整的 Fullwidth 表格,之前还有一些我漏掉的,比如: -> @

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

我跟进,在你这个基础上补充一下

@huacnlee huacnlee merged commit d28cb09 into huacnlee:main Mar 23, 2025
6 checks passed
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
0