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

Generalize Buffer.BlockCopy optimization for either src or dst #4736

Merged
merged 2 commits into from
May 3, 2016

Conversation

jamesqo
Copy link

@jamesqo jamesqo commented May 3, 2016

Don't have that much experience in native programming, but here it goes...

I noticed from #3118 that an optimization was added in the case where both src and dst were byte arrays, but not when only one of them was. Therefore, the optimization would not apply to code like this:

var bytes = new byte[4] { 1, 2, 3, 4 };
var shorts = new shorts[2];
Buffer.BlockCopy(bytes, 0, shorts, 0, bytes.Length);

This PR changes that so the optimization runs if either the src or dst arrays is a byte array, otherwise falling back to the default implementation as usual.

cc @jkotas

@jkotas
Copy link
Member

jkotas commented May 3, 2016

I have never seen Buffer.BlockCopy usage pattern like this. Do you have any examples of code where this helps?

Checking for a special case - that pretty much never happens - is making the code slower for everybody else. It is net loss - not making any real world workload faster.

@jamesqo
Copy link
Author

jamesqo commented May 3, 2016

@jkotas Sure, it's one of the top answers on Buffer.BlockCopy on StackOverflow: http://stackoverflow.com/a/3783189/4077294

Besides (correct me if I'm wrong) I don't think this is adding any overhead to the cases where this optimization doesn't apply.

}
}

// Optimization: If copying to/fro the same array, then
Copy link
Member

Choose a reason for hiding this comment

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

Typo: fro -> from

@jkotas
Copy link
Member

jkotas commented May 3, 2016

I see. LGTM. Thanks!

@jkotas jkotas merged commit 32e44ef into dotnet:master May 3, 2016
@jamesqo jamesqo deleted the generalize-bytearray-opt branch May 3, 2016 11:51
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…t/coreclr#4736)

* Generalize Buffer.BlockCopy optimization for either src or dst

Commit migrated from dotnet/coreclr@32e44ef
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.

3 participants