# str_repeat() patch

- Thread: https://externals.io/message/10402
- Original archive: https://news-web.php.net/php.internals/10402
- Started by Alexander Valyalkin on 2004-06-11T14:50:16+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10402 — Alexander Valyalkin — 2004-06-11T14:50:16+00:00

URL: https://externals.io/message/10402#10402

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/

---

## #10403 — Ilia Alshanetsky — 2004-06-11T14:55:20+00:00

URL: https://externals.io/message/10402#10403 · In reply to: #10402

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


> 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/

---

## #10404 — Derick Rethans — 2004-06-11T15:00:03+00:00

URL: https://externals.io/message/10402#10404 · In reply to: #10402

>
> 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

---

## #10408 — Alexander Valyalkin — 2004-06-12T14:27:45+00:00

URL: https://externals.io/message/10402#10408 · In reply to: #10404

On Fri, 11 Jun 2004 17:00:03 +0200 (CEST), Derick Rethans <derick@php.net>  
wrote:

> 
>
> > […]
>
> 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/

---

## #10410 — Alexander Valyalkin — 2004-06-12T16:36:40+00:00

URL: https://externals.io/message/10402#10410 · In reply to: #10403

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/

---

## #10411 — George Schlossnagle — 2004-06-12T16:58:46+00:00

URL: https://externals.io/message/10402#10411 · In reply to: #10408

> On Fri, 11 Jun 2004 17:00:03 +0200 (CEST), Derick Rethans 
> <derick@php.net> wrote:
>
> > […]
>
> 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

---

## #10418 — Michael Walter — 2004-06-13T12:55:31+00:00

URL: https://externals.io/message/10402#10418 · In reply to: #10411

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:
> 
> 
> 
> > […]
> 
> 
> That's not what my glibc sources look like.
> 
> George

---

## #10419 — Stefan Esser — 2004-06-13T12:58:54+00:00

URL: https://externals.io/message/10402#10419 · In reply to: #10418

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

---

## #10420 — Michael Walter — 2004-06-13T13:05:06+00:00

URL: https://externals.io/message/10402#10420 · In reply to: #10419

Stefan Esser wrote:
> The whole discussion about memcpy beeing faster than memmove is 
> completely pointless. [..]
Agreed.

Cheers,
Michael
