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

replace memcpy with memmove #3937

Closed
vtjnash opened this issue Aug 4, 2013 · 8 comments
Closed

replace memcpy with memmove #3937

vtjnash opened this issue Aug 4, 2013 · 8 comments
Labels
bug Indicates an unexpected problem or unintended behavior speculative Whether the change will be implemented is speculative
Milestone

Comments

@vtjnash
Copy link
Member

vtjnash commented Aug 4, 2013

Uses of memcpy in Base may need to be replaced by memmove, except where we can guarantee that there is no pointer aliasing. (similarly, uses of LAPACK copy should be avoided, unless we first check for pointer aliasing)

@stevengj
Copy link
Member

stevengj commented Aug 5, 2013

Some useful benchmarks here on whether it is even worth figuring out whether memcpy is safe. As a first pass, it looks like we wouldn't lose much if we did s/memcpy/memmove/ on everything.

@timholy
Copy link
Member

timholy commented Aug 5, 2013

I read those benchmarks as saying "just use loops." I know the author doesn't say that, but check out the curves: for all compilers except pgcc, the loops-with-indices result is right on top of memcpy.

That confirms something I had noticed recently: on my current machine, with a current julia, I also couldn't see a difference. I found this interesting because some time ago I went to a certain amount of effort (#190) to incorporate memcpy into getindex/setindex. At the time it was a major improvement (something like 5-fold, depending on array size etc). I don't know whether it's because I've since changed platforms (x86->x86_64), because of advances in LLVM, or because we're turning on more optimization passes, but when I benchmarked this a week or two ago I couldn't see the difference between them.

If other people can't see any differences between for loops and ccall(:memcpy,...) either, I propose we yank out calls to memcpy and go back to for loops. It's not often we get to shrink base...

@pao
Copy link
Member

pao commented Aug 5, 2013

I don't know whether it's because I've since changed platforms (x86->x86_64), because of advances in LLVM, or because we're turning on more optimization passes, but when I benchmarked this a week or two ago I couldn't see the difference between them.

I'd be wary of this if we don't know which change is the source of the speedup. I've seen (recently) order-of-magnitude differences in the speed of memcpy vs. loop copying of arrays (gcc 4.6.3, -O2, for what it's worth.)

@simonster
Copy link
Member

There are also LLVM intrinsics for memcpy/memmove, although I don't know whether there's any advantage to using them.

@vtjnash
Copy link
Member Author

vtjnash commented Aug 6, 2013

that trivial loop has the same bug I am advising against here. the use of loops in gcc is basically hoping that it's pattern matching algorithm will turn it into a call to memcpy (as shown in the blog).

@timholy
Copy link
Member

timholy commented Aug 6, 2013

Fair enough. I agree it looks like the performance cost is negligible.

@JeffBezanson JeffBezanson mentioned this issue May 9, 2014
@vtjnash vtjnash added this to the 0.4 milestone May 9, 2014
@dcjones dcjones closed this as completed in 27d9eed Jul 4, 2014
JeffBezanson added a commit that referenced this issue Jul 4, 2014
Use memmove instead of memcpy in copy! Fixes #3937
@vtjnash
Copy link
Member Author

vtjnash commented Jul 4, 2014

@dcjones was that the only memcpy usage that needed to be switched?

@dcjones
Copy link
Contributor

dcjones commented Jul 4, 2014

I looked at the memcpy ccalls in Base and this seemed like the only risky one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

6 participants