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

Add IO::Sized#remaining=(value) to reuse an existing instance #10520

Merged

Conversation

jgaskins
Copy link
Contributor

IO::Sized is an excellent abstraction for segmenting an IO and gives you the tools to ensure that that segment is completely consumed when you leave a block regardless of whether it was consumed inside the block itself. But if you need to segment that IO into a lot of small-ish chunks you end up re-allocating hundreds, thousands, or even millions of IO::Sized instances, which can involve a lot of malloc, GC, and free calls.

You can see the impact in this PR to will/crystal-pg where we reduced CPU usage by 73% for a Postgres query that returns 500k values (5 columns x 100k rows). In that PR @straight-shoota recommended adding that functionality directly to IO::Sized.

jgaskins added a commit to jgaskins/crystal-pg that referenced this pull request Mar 19, 2021
This PR should be released with Crystal 1.0, but this will allow us to
use this feature until then.

See: crystal-lang/crystal#10520
@bcardiff bcardiff added this to the 1.0.0 milestone Mar 19, 2021
@bcardiff
Copy link
Member

I think we can include this small feature in 1.0, right?

@bcardiff bcardiff merged commit 3386930 into crystal-lang:master Mar 19, 2021
@jgaskins jgaskins deleted the make-io-sized-read-remaining-writable branch March 19, 2021 23:19
jgaskins added a commit to jgaskins/crystal-pg that referenced this pull request Mar 19, 2021
This was added in crystal-lang/crystal#10520 but
will not be released until Crystal 1.0. Once this shard drops support
for Crystal < 1.0, this commit can be reverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants