Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Don't do null check on every loop when filling buffer. - #2871

Merged
VSadov merged 1 commit into
dotnet:masterfrom
JonHanna:change_buffer_fill_loop
Aug 19, 2015
Merged

Don't do null check on every loop when filling buffer.#2871
VSadov merged 1 commit into
dotnet:masterfrom
JonHanna:change_buffer_fill_loop

Conversation

@JonHanna

Copy link
Copy Markdown
Contributor

Buffer has an optimisation of using a null array to represent an empty state, that
also plays well with it being a value type.

When the buffer is filled from an enumerable, this possible null state is checked
for on every loop.

Restructure to avoid this redundancy.

Buffer has an optimisation of using a null array to represent an empty state, that
also plays well with it being a value type.

When the buffer is filled from an enumerable, this possible null state is checked
for on every loop.

Restructure to avoid this redundancy.
@stephentoub

Copy link
Copy Markdown
Member

This duplicates #2668 from @James-Ko. However, this version addresses the concerns raised there, so I'd be inclined to take this version (with thanks to you both). There was a question on the other PR about perf impact... do you have measurements with results you can share?

cc: @VSadov

@jamesqo

jamesqo commented Aug 18, 2015

Copy link
Copy Markdown
Contributor

Great, I'll close my PR. Thanks for the heads-up.

@JonHanna

Copy link
Copy Markdown
Contributor Author

I compared some methods with just those two fragements, and measurements varied a lot depending on the source enumerable, from around less than 1% worse to 20% better (but only one case was that extreme), with most a couple of percent better.

If the source was something like an array or a list that could iterate quickly by moving an index against an array it actually did quite a bit worse, but that case wouldn't be hit here.

@JonHanna

Copy link
Copy Markdown
Contributor Author

@James-Ko I didn't see that PR. GMTA. The addition of returns in yours has an advantage, that we can also get with reordering, without abandoning the use of locals.

@stephentoub

Copy link
Copy Markdown
Member

@dotnet-bot test this please

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I liked the previous version that just touched the below code (optimizing away the null check on each iteration) much more than I like this version with the gotos. It seems like we're adding complexity and code simply to avoid an additional null check / branch on each ToArray call, but there's already a whole bunch of casts and null checks and branches on these code paths. Does this really amount to a measurable win?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're completely correct. The only case where it saves anything but a single compare is in the case of a zero-item collection source, and there not only does it not save much, but that case has a much more straightforward way of dealing with it by just bailing out then, anyway, but even that isn't worth the effort.

@JonHanna
JonHanna force-pushed the change_buffer_fill_loop branch from 83c5c62 to 6cf2b4c Compare August 19, 2015 14:33
@JonHanna

Copy link
Copy Markdown
Contributor Author

Have reset on the moment of madness in those last two commits.

@jamesqo

jamesqo commented Aug 19, 2015

Copy link
Copy Markdown
Contributor

@hackcraft Agreed, I didn't think the gotos should be necessary either.

@stephentoub

Copy link
Copy Markdown
Member

LGTM. Thanks.

@stephentoub

Copy link
Copy Markdown
Member

@VSadov, you ok with the change?

@VSadov

VSadov commented Aug 19, 2015

Copy link
Copy Markdown
Member

LGTM

VSadov added a commit that referenced this pull request Aug 19, 2015
Don't do null check on every loop when filling buffer.
@VSadov
VSadov merged commit 83a5b06 into dotnet:master Aug 19, 2015
@JonHanna
JonHanna deleted the change_buffer_fill_loop branch August 21, 2015 11:20
@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…ll_loop

Don't do null check on every loop when filling buffer.

Commit migrated from dotnet/corefx@83a5b06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants