str_repeat() patch

php.internals

Alexander Valyalkin

22 years ago
Read it: void *memmove(void *s1, const void *s2, size_t n); If s1 and s2 overlap, all bytes are copied in a preserving manner (unlike memcpy()) And anwer the question: are s1 and s2 overlap in the str_repeat() ? Why don't use memcpy() instead of memmove() ? ===========cut============= --- string.c Thu May 13 20:44:32 2004 +++ string_str_repeat.c Fri Jun 11 17:40:47 2004 @@ -3581,7 +3581,7 @@ while (e<ee) { l = (e-s) < (ee-e) ? (e-s) : (ee-e); - memmove(e, s, l); + memcpy(e, s, l); e += l; } } ===========cut=============
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Ilia A.

22 years ago
Read your memcpy manpages "The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas may not overlap." Ilia On June 11, 2004 10:50 am, Alexander Valyalkin wrote:

Alexander Valyalkin

22 years ago
On Fri, 11 Jun 2004 10:55:20 -0400, Ilia Alshanetsky <ilia@prohost.org> wrote:
> Read your memcpy manpages > > "The memcpy() function copies n bytes from memory area src to memory > area dest. The memory areas may not overlap." >
Well, I've read my manpages: The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas may not overlap. Use memmove() if the memory areas do overlap. Pay attention on the LAST sentence.
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Derick Rethans

22 years ago
On Fri, 11 Jun 2004, Alexander Valyalkin wrote:
> > Read it: > void *memmove(void *s1, const void *s2, size_t n); > If s1 and s2 overlap, all bytes are copied in a preserving manner (unlike > memcpy()) > > And anwer the question: are s1 and s2 overlap in the str_repeat() ? > Why don't use memcpy() instead of memmove() ?
Because it's slower and we're copying to newly allocated memory anyway so things CAN not overlap. Also, your patch is the wrong way around. Please stop wasting our time with those silly patches that fix nothing; there is plenty of other good things to do to help with PHP development (like translations as Antony mentioned). regards, Derick

Alexander Valyalkin

22 years ago
On Fri, 11 Jun 2004 17:00:03 +0200 (CEST), Derick Rethans <derick@php.net> wrote:
> On Fri, 11 Jun 2004, Alexander Valyalkin wrote: > >> >> Read it: >> void *memmove(void *s1, const void *s2, size_t n); >> If s1 and s2 overlap, all bytes are copied in a preserving manner >> (unlike >> memcpy()) >> >> And anwer the question: are s1 and s2 overlap in the str_repeat() ? >> Why don't use memcpy() instead of memmove() ? > > Because it's slower and we're copying to newly allocated memory anyway > so things CAN not overlap. Also, your patch is the wrong way around. >
Mybe I'm mistaken, but I think, that memmove() looks like that (without any error checks): void *memmove(void *dst, void *src, size_t len) { /* check overlapping of src & dst */ size_t distance = (char *)dst > (char *)src ? (char *)dst - (char *)src : (char *)src - (char *)dst; if (distance < len) { /* overlap. Use temporary buffer & memcpy(). Here could be used byte-to-byte copying or any oher tricks */ char *tmp = malloc(len); memcpy(tmp, src, len); memcpy(dst, tmp, len); free(tmp); } else { /* not overlap. Use memcpy() */ memcpy(dst, src, len); } return dst; } Are you still think that memcpy() is slower than memmove() ? I would tell that memcpy() at least not more slowly than memmove().
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

George Schlossnagle

22 years ago
On Jun 12, 2004, at 10:27 AM, Alexander Valyalkin wrote:
> On Fri, 11 Jun 2004 17:00:03 +0200 (CEST), Derick Rethans > <derick@php.net> wrote: > >> On Fri, 11 Jun 2004, Alexander Valyalkin wrote: >> >>> >>> Read it: >>> void *memmove(void *s1, const void *s2, size_t n); >>> If s1 and s2 overlap, all bytes are copied in a preserving manner >>> (unlike >>> memcpy()) >>> >>> And anwer the question: are s1 and s2 overlap in the str_repeat() ? >>> Why don't use memcpy() instead of memmove() ? >> >> Because it's slower and we're copying to newly allocated memory anyway >> so things CAN not overlap. Also, your patch is the wrong way around. >> > > Mybe I'm mistaken, but I think, that memmove() looks like that (without > any error checks):
That's not what my glibc sources look like. George

Michael Walter

22 years ago
Well, surely memmove() needs an additional branch to check for overlapping, though (the OP was wrong in that memmove() would need a temporary buffer, though - it doesn't, it usually copies from high addres to low in the overlapping case). Cheers, Michael George Schlossnagle wrote:

Stefan Esser

22 years ago
Michael Walter wrote:
> Well, surely memmove() needs an additional branch to check for > overlapping, though (the OP was wrong in that memmove() would need a > temporary buffer, though - it doesn't, it usually copies from high > addres to low in the overlapping case).
The whole discussion about memcpy beeing faster than memmove is completely pointless. It is obvious that copying without checking is faster than coyping with checking. But the performance impact is minimal, otherwise it would not be fact that the BSD's all have memcpy==memmove. Stefan

Michael Walter

22 years ago
Stefan Esser wrote:
> The whole discussion about memcpy beeing faster than memmove is > completely pointless. [..]
Agreed. Cheers, Michael