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

Avoid unnecessary work for identical locations in Buffer.BlockCopy #36

Merged
merged 1 commit into from
Feb 4, 2015

Conversation

benaadams
Copy link
Member

Avoid unnecessary work for identical locations in Buffer.BlockCopy

Perform validity checks to ensure parameters are correct but short-circuit
out memmove when exactly the same data would be copied to the same location.

There are a number of occasions; which can be intentional or unintentional,
where the buffer being copied is the same place - e.g an internal buffer is
the same as the return buffer, and there is no need to call memmove's
overwrite safe copy.

Generally the call to BlockCopy will be in a library so it is more practical
to enable the check here rather than alter all the calling functions,
including 3rd party libraries to preform additional checks.

@AlexGhiondea
Copy link

Could you also update the commit message to follow the published guidelines?

https://github.com/dotnet/corefx/wiki/Contributing#commits

if (count > 0) {
memmove(dst->GetDataPtr() + dstOffset, src->GetDataPtr() + srcOffset, count);
if ((srcPtr != dstPtr) && (count > 0)) {
memmove(dstPtr, srcPtr, count);
}

FC_GC_POLL();
Copy link
Member Author

Choose a reason for hiding this comment

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

Should FC_GC_POLL() also be moved inside the if test, i.e. only run when work is done?

Copy link
Member

Choose a reason for hiding this comment

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

FC_GC_POLL should be done all the time even when no work is done. Otherwise, there is a risk of GC starvation - the suspension for GC would take hang if there is a thread that calls BlockCopy in a tight loop.

@jkotas
Copy link
Member

jkotas commented Feb 4, 2015

Could you please update the commit message to something like "Avoid unnecessary work for identical locations in Buffer.BlockCopy" so that it follows the guidelines that Alex mentioned above?

@benaadams benaadams changed the title BlockCopy: Zero copy for identical locations Avoid unnecessary work for identical locations in Buffer.BlockCopy Feb 4, 2015
@benaadams
Copy link
Member Author

Done

@jkotas
Copy link
Member

jkotas commented Feb 4, 2015

LGTM

@AlexGhiondea
Copy link

LGTM -- would you mind squashing the commits? Then we are good to go!

Perform validity checks to ensure parameters are correct but short-circuit
out memmove when exactly the same data would be copied to the same location.

There are a number of occasions; which can be intentional or unintentional,
where the buffer being copied is the same place - e.g an internal buffer is
the same as the return buffer, and there is no need to call memmove's
overwrite safe copy.

Generally the call to BlockCopy will be in a library so it is more practical
to enable the check here rather than alter all the calling functions,
including 3rd party libraries to preform additional checks.
@benaadams
Copy link
Member Author

Squashed

jkotas added a commit that referenced this pull request Feb 4, 2015
Avoid unnecessary work for identical locations in Buffer.BlockCopy
@jkotas jkotas merged commit cbf5cbf into dotnet:master Feb 4, 2015
@jkotas
Copy link
Member

jkotas commented Feb 4, 2015

Thanks for the contribution!

BruceForstall pushed a commit to BruceForstall/coreclr that referenced this pull request Aug 17, 2016
benaadams referenced this pull request in dotnet/corefx Sep 30, 2017
We end up draining the relevant bucket in the pool very quickly, such that we still end up doing a lot of allocating while also harming other users of the same bucket size.
@benaadams benaadams deleted the patch-1 branch March 27, 2018 05:11
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