8000 Avoid heap usage when encoding to base58 by sgeisler · Pull Request #191 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid heap usage when encoding to base58 #191

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

Merged
merged 2 commits into from
Dec 4, 2018

Conversation

sgeisler
Copy link
Contributor
@sgeisler sgeisler commented Nov 9, 2018

Instead of always allocating a Vec this implementation will work on the stack only as long as the output stays below 100 characters.

  • Test behavior with large messages
  • Cleanup

Closes #145

@sgeisler sgeisler changed the title WIP: Avoid heap usage when encoding to base58 Avoid heap usage when encoding to base58 Nov 9, 2018
@@ -134,14 +177,12 @@ pub fn from_check(data: &str) -> Result<Vec<u8>, Error> {
Ok(ret)
}

fn encode_iter_utf8<I>(data: I) -> Vec<u8>
fn encode_iter_utf8<I, W>(data: I, writer: &mut W) -> Result<(), fmt::Error>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is obvious to me that the function should be renamed since it'f no longer returning UTF-8 bytes but rather writing unicode chars to a writer. What would you think is a good name instead?

Copy link
Member

Choose a reason for hiding this comment

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

format_iter maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

String::from_utf8(ret).unwrap()
let mut ret = String::new();
encode_iter_utf8(data, &mut ret).expect("writing into string shouldn't fail");
ret
}

/// Directly encode a slice as base58 into a `Formatter`.
fn encode_iter_to_fmt<I>(fmt: &mut fmt::Formatter, data: I) -> fmt::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.

I just noticed that this function becomes redundant.

@apoelstra
Copy link
Member

Looks great, thanks!

@apoelstra apoelstra merged commit 8a14ddb into rust-bitcoin:master Dec 4, 2018
@sgeisler sgeisler deleted the base58 branch December 5, 2018 00:11
yancyribbens pushed a commit to yancyribbens/rust-bitcoin that referenced this pull request Mar 23, 2024
…straints

overhaul interpreter ("satisfied constraints") API
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