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

improve stringbuilder::output #1730

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

illusory0x0
Copy link
Contributor

No description provided.

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 5532

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 92.567%

Totals Coverage Status
Change from base Build 5531: 0.001%
Covered Lines: 5878
Relevant Lines: 6350

💛 - Coveralls

@Yu-zh
Copy link
Collaborator

Yu-zh commented Mar 3, 2025

The improvement does not look obvious to me. Can you explain it?

pub impl Show for StringBuilder with output(self, logger) {
logger.write_string(
self.data.unsafe_to_bytes().to_unchecked_string(offset=0, length=self.len),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to_unchecked_string just simply copy bytes into string

Copy link
Collaborator

Choose a reason for hiding this comment

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

So basically you want avoid the extra copy here. My suggestion is that we should change the semantics of to_uncheck_string since Bytes has become immutable. Then we can still enjoy the batch processing in write_string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When using to_uncheck_string with backends that require extra copies, such as the js backend and wasm-gc backend with the js-string-builtin option turned on, use this implementation

for i = 0; i < self.len; i = i + 2 {
let low = self.data[i].to_int()
let high = self.data[i + 1].to_int()
let code_unit = (high << 8) | low |> Char::from_int
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code_unit is the same as String::op_get 's result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

convert two bytes to code_unit, like convert char* into uint16_t* then use pointer read data.

@peter-jerry-ye peter-jerry-ye marked this pull request as draft March 10, 2025 03:43
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.

3 participants