C considered dangerous
C considered dangerous
Posted Aug 30, 2018 21:49 UTC (Thu) by zlynx (guest, #2285)In reply to: C considered dangerous by epa
Parent article: C considered dangerous
memcpy and memmove are *NOT IDENTICAL*.
That extra comparison to determine copy direction costs time and branch prediction failure. The only reason everyone seems to think it's free is that common CPU types now run ahead and prime the branch prediction.
I had evidence from oprofile in 2005 that showed memmove was most definitely slower than memcpy. That was on Pentium 3 and 4. Pipeline bubbles on P4 were painful.
Just like all the other sharp edges in C like pointer aliasing, programmers need to *read the documentation* and *know what they're doing* when using memcpy. Having it blow up their program is entirely fair.
Posted Aug 31, 2018 6:07 UTC (Fri)
by epa (subscriber, #39769)
[Link] (1 responses)
Posted Aug 31, 2018 6:40 UTC (Fri)
by iabervon (subscriber, #722)
[Link]
Posted Sep 4, 2018 5:19 UTC (Tue)
by ncm (guest, #165)
[Link] (7 responses)
Complicating the interface for this and similar functions amounts to piling deck chairs on the Titanic into an obstacle course.
It is more than a little stupid to write a new program in C. All the typical pitfalls of pointer manipulations just don't arise in C++. C++ does still have integer overflow UB, as does Rust, and anyway unsigned integers wrapping around is nowhere near so benign as many like to believe.
There would be no problem with C++ in the Linux kernel if not for mindless bigotry, with many fewer opportunities for silly-bugger mistakes, and both faster and shorter code. It will happen eventually. It might happen first in FreeBSD, as the blinkered ancients there are closer to retirement.
Posted Sep 4, 2018 6:46 UTC (Tue)
by quotemstr (subscriber, #45331)
[Link] (5 responses)
That's absurd nonsense. You must be talking about different implementations of memcpy and memmove. so it's not an apples-to-apples comparison. You can always implement memcpy by taking memmove and baking in the memory-direction branches, which means that no matter how efficient your memmove, you can generate an even _more_ efficient memcpy out of it.
I can imagine memmove and memcpy being equally fast, but memmove being faster, by doing more work? Something is clearly wrong with the comparison.
Posted Sep 5, 2018 23:03 UTC (Wed)
by jreiser (subscriber, #11027)
[Link] (4 responses)
Posted Sep 5, 2018 23:45 UTC (Wed)
by quotemstr (subscriber, #45331)
[Link] (3 responses)
Posted Sep 6, 2018 3:42 UTC (Thu)
by jreiser (subscriber, #11027)
[Link] (2 responses)
Posted Sep 6, 2018 6:55 UTC (Thu)
by liw (subscriber, #6379)
[Link]
I tried writing a benchmark. GCC optimised it away.
Posted Sep 6, 2018 7:14 UTC (Thu)
by quotemstr (subscriber, #45331)
[Link]
Posted Sep 21, 2018 18:14 UTC (Fri)
by mathstuf (subscriber, #69389)
[Link]
No, Rust defines integer overflow as twos-complement (though in debug builds it will panic). You can use an always-panicking addition however, but that's not the default.
C considered dangerous
C considered dangerous
C considered dangerous
C considered dangerous
memmove faster than memcpy for small N
memmove faster than memcpy for small N
memmove faster than memcpy for small N
memmove faster than memcpy for small N
memmove faster than memcpy for small N
C considered dangerous
