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 performance of String and Vec<u8> #58

Merged
merged 19 commits into from
Mar 13, 2020
Merged

Conversation

evgenykuzyakov
Copy link
Contributor

@evgenykuzyakov evgenykuzyakov commented Mar 3, 2020

Optimizing strings/vec reads using custom implementation for Vec<u8> with a bit of unsafe code, but without UB.
Switching deserialization to use &mut &[u8] instead of BufRead. It allows us to switch to BorshError instead of std::io::Error in the future to go with no_std.

Test plan

Copy link
Contributor

@MaksymZavershynskyi MaksymZavershynskyi left a comment

Choose a reason for hiding this comment

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

Is it possible that https://doc.rust-lang.org/std/io/struct.BufReader.html implements this very functionality?

@evgenykuzyakov
Copy link
Contributor Author

Is it possible that https://doc.rust-lang.org/std/io/struct.BufReader.html implements this very functionality?

I think we can use BufReader with raw fill_buf() functionality for a lot of what I did manually with read_exact

@evgenykuzyakov evgenykuzyakov changed the title Faster string deserialization Faster deserialization for Strings, Vec<u8> and Vec<T> where T is integer Mar 7, 2020
@evgenykuzyakov
Copy link
Contributor Author

Input Highlights for gas fess:

Cost of Borsh input of (s: String) where s="" is 9,080,640,736
Average diff 131,604,521
Cost of Borsh input of (v: Vec) where v=[] is 8,455,908,634
Average diff 112,708,303
Cost of Borsh input of (v: Vec) where v=[] is 8,425,057,666
Average diff 651,364,504

Full results: near/nearcore#1829 (comment)

TODO: Add Vec<_> tests

@MaksymZavershynskyi
Copy link
Contributor

Could you rerun https://github.com/nearprotocol/borsh/tree/master/borsh-rs/benchmarks and re-generate the graphs?

@evgenykuzyakov evgenykuzyakov changed the title Faster deserialization for Strings, Vec<u8> and Vec<T> where T is integer Improve performance of String and Vec<u8> Mar 10, 2020
Copy link
Contributor

@MaksymZavershynskyi MaksymZavershynskyi left a comment

Choose a reason for hiding this comment

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

Could you also update the benchmarks so that http://borsh.io/ picks them up?

@MaksymZavershynskyi
Copy link
Contributor

Could you also bump the versions?

Copy link
Contributor

@MaksymZavershynskyi MaksymZavershynskyi left a comment

Choose a reason for hiding this comment

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

P.S. My github UI is chocking on 8k files :) Could we please remvoe the docs temporarily until we are through the reviews and then add them later before the merge?

borsh-rs/borsh/src/de/mod.rs Outdated Show resolved Hide resolved
borsh-rs/borsh/src/de/mod.rs Outdated Show resolved Hide resolved
@evgenykuzyakov
Copy link
Contributor Author

Trying switch to fn deserialize(buf: &mut [u8]) -> Result<Self, Error>;.
Will let you know

@evgenykuzyakov
Copy link
Contributor Author

evgenykuzyakov commented Mar 11, 2020

See #62 for slice reader version. Need to fix benchmarks, but overall looks clean.

Also once we switch serialize to &mut Vec<u8>, we can kill std:io::Error

* Switch to mut buf

* Fix benchmarks:
@evgenykuzyakov
Copy link
Contributor Author

Going to regenerate benchmarks with the slice run

@evgenykuzyakov
Copy link
Contributor Author

evgenykuzyakov commented Mar 12, 2020

Merged buf slice branch, so it's ready to go (once the new benchmarks are added)

@@ -1 +1,2 @@
docs/* linguist-vendored
docs/criterion/** linguist-generated=true -diff
Copy link
Contributor

Choose a reason for hiding this comment

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

How is it different from linguist-vendored and what does -diff do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know. It's used by nearcore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I'm not sure why you would request changes based on the question? Which changes do I need to make?

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
Contributor

Choose a reason for hiding this comment

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

I somehow failed to duckduckgo this info. It should've been a comment instead of request for changes.

@evgenykuzyakov evgenykuzyakov merged commit 0466418 into master Mar 13, 2020
@evgenykuzyakov evgenykuzyakov deleted the faster-string-deser branch March 13, 2020 17:13
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