Skip to content

Performance: formatting float/double for json and text formats #392

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

Closed
tbkka opened this issue Mar 18, 2017 · 3 comments
Closed

Performance: formatting float/double for json and text formats #392

tbkka opened this issue Mar 18, 2017 · 3 comments

Comments

@tbkka
Copy link
Collaborator

tbkka commented Mar 18, 2017

Writing float and double values to JSON or text format is significantly slower than it should be.

The original code used String(_: Double) to convert doubles to String, and then converted the String to UTF8 to append to the output. PR #391 changes this to directly invoke the C library strtod and vsnprintf to parse and format doubles, respectively.

The direct use of strtod worked very well, but vsnprintf requires using Swift's CVarArg facility; this involves creating intermediate objects that we usually manage to avoid. If we could avoid using varargs in this way, we should see a sizable performance improvement. Unfortunately, the standard C library does not expose a function for formatting float/double that doesn't use varargs.

@tbkka
Copy link
Collaborator Author

tbkka commented Apr 17, 2017

Related: The other problem with String(_: Double) is that it's not exact. (Google's conformance test checks for this.) See https://bugs.swift.org/browse/SR-106 for more discussion.

@tbkka
Copy link
Collaborator Author

tbkka commented Apr 17, 2017

Hmmm.... Looks like the exactness problem was partially addressed in swiftlang/swift#348 (Double.debugDescription is now exact; Double.description behaves the same as before).

@tbkka
Copy link
Collaborator Author

tbkka commented Jun 4, 2019

Same as #752, resolved by #882.

ahmed-osama-saad pushed a commit to ahmed-osama-saad/swift-protobuf that referenced this issue Oct 12, 2023
ahmed-osama-saad pushed a commit to ahmed-osama-saad/swift-protobuf that referenced this issue Oct 12, 2023
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

No branches or pull requests

2 participants