Skip to contentSkip to conversation

lib/string: memmove: use memcpy if it is safe to do so

PATCH New Peter Korsgaard 3 participants 5 messages Oct 28, 2011 – Oct 29, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/string: memmove: use memcpy if it is safe to do so

memmove is used in a number of performance critical places, like copying the linux kernel from nor flash to ram, so optimizing it can be interesting.

Unfortunately, an arch specific version of memmove often isn't used, and not supported at all on a number of archs (arm/mips/nds32/nios2/x86) - But memcpy typically is.

Often memmove is called for copies where src/dest don't overlap, so we could use the more efficient memcpy instead. Detect these situations and forward those request to memcpy instead.

Adds 40 bytes to memmove and speeds up boot with ~300ms on a 400MHz arm9.

---
 lib/string.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
lib/string.c +4 -0
index 2c4f0ec..239cc11 100644
@@ -504,12 +504,16 @@ void * memmove(void * dest,const void *src,size_t count)
 		return dest;
 
 	if (dest <= src) {
+		if (dest + count <= src)
+			return memcpy(dest, src, count);
 		tmp = (char *) dest;
 		s = (char *) src;
 		while (count--)
 			*tmp++ = *s++;
 		}
 	else {
+		if (src + count <= dest)
+			return memcpy(dest, src, count);
 		tmp = (char *) dest + count;
 		s = (char *) src + count;
 		while (count--)
Signature · 1 line
1.7.6.3
#1 lore raw

PATCH 1/1 lib/string: memmove: use memcpy if it is safe to do so

Dear Peter Korsgaard,

Quoted 6 lines · In message <[email protected]> you wrote:

memmove is used in a number of performance critical places, like copying the linux kernel from nor flash to ram, so optimizing it can be interesting.

Unfortunately, an arch specific version of memmove often isn't used, and not supported at all on a number of archs (arm/mips/nds32/nios2/x86) - But memcpy typically is.

So you are adding code, making the system even less efficient? This sounds to be acounter-productive approach.

Can you not instead arrange for arch specific, optimized versions memmove() to be used?

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Uncertain fortune is thoroughly mastered by the equity of the  calcu-
lation.                                               - Blaise Pascal
#2 lore raw

PATCH 1/1 lib/string: memmove: use memcpy if it is safe to do so

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Wolfgang" == Wolfgang Denk <[email protected]> writes:

Hi,

Quoted 6 lines
Quoted 6 lines

memmove is used in a number of performance critical places, like copying the linux kernel from nor flash to ram, so optimizing it can be interesting.

Unfortunately, an arch specific version of memmove often isn't used, and not supported at all on a number of archs (arm/mips/nds32/nios2/x86) - But memcpy typically is.

Wolfgang> So you are adding code, making the system even less efficient? This Wolfgang> sounds to be acounter-productive approach.

Well, slightly less efficient for the (uncommon) situation where src/dest overlaps in return for significantly more efficiency for the situation where they don't.

Wolfgang> Can you not instead arrange for arch specific, optimized versions Wolfgang> memmove() to be used?

That's another option, but more work for implementers - So I would prefer this trivial patch instead.

But I'll drop it if you disagree.

Signature · 1 line
Bye, Peter Korsgaard
#3 lore raw

PATCH 1/1 lib/string: memmove: use memcpy if it is safe to do so

Dear Peter Korsgaard,

Quoted 7 lines · In message <[email protected]> you wrote:

Wolfgang> So you are adding code, making the system even less efficient? This Wolfgang> sounds to be acounter-productive approach.

Well, slightly less efficient for the (uncommon) situation where src/dest overlaps in return for significantly more efficiency for the situation where they don't.

The test which situation we have needs to be always performed, and adds overhead, especially for short copies.

Quoted 7 lines

Wolfgang> Can you not instead arrange for arch specific, optimized versions Wolfgang> memmove() to be used?

That's another option, but more work for implementers - So I would prefer this trivial patch instead.

But I'll drop it if you disagree.

I prefer to keep this code as is, and add optimized versions of memmove() where needed / wanted.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Anyone who isn't confused here doesn't really know what's going on.
#4 lore raw

PATCH 1/1 lib/string: memmove: use memcpy if it is safe to do so

In reply to Wolfgang Denk
Quoted 8 lines · Wolfgang Denk
Quoted 6 lines · Peter Korsgaard

memmove is used in a number of performance critical places, like copying the linux kernel from nor flash to ram, so optimizing it can be interesting.

Unfortunately, an arch specific version of memmove often isn't used, and not supported at all on a number of archs (arm/mips/nds32/nios2/x86) - But memcpy typically is.

Quoted 5 lines

So you are adding code, making the system even less efficient? ?This sounds to be acounter-productive approach.

Can you not instead arrange for arch specific, optimized versions memmove() to be used?

i think this is kind of a loose-loose situation. in common code, we have to use memmove() if the two regions could *possibly* overlap. so in practice, we end up calling memmove() in more cases than strictly necessary. but the only real choice is to update all the call sites with region overlap checks.

at least with Peter's patch, we centralize all these checks in one place producing smaller code. in some cases, we probably add a little bit of runtime overhead when the new if() statement returns false, but it seems to be that one successful call quickly outweighs multiple unsuccessful if() checks.

i also think this falls into the same category as the previous commit b038db852b5b7120f1ff825d8e2a5c2cd14c2f0f:
memcpy/memmove: Do not copy to same address

so the ideal probably would be to have an optimized memmove in place, but for the (many) arches which don't have this, we seem to get a "win" overall ?
-mike