From fe9991e8b29d261f12a0a0a15b89d67a29f5299d Mon Sep 17 00:00:00 2001 From: Quan Yong Zhai Date: Tue, 12 Jul 2011 17:30:40 -0700 Subject: [PATCH] runtime: replace runtime.mcpy with runtime.memmove faster string operations, and more tested on linux/386 runtime_test.BenchmarkSliceToString 642 532 -17.13% runtime_test.BenchmarkStringToSlice 636 528 -16.98% runtime_test.BenchmarkConcatString 1109 897 -19.12% R=r, iant, rsc CC=golang-dev https://golang.org/cl/4674042 --- src/cmd/cgo/out.go | 2 +- src/pkg/runtime/386/closure.c | 2 +- src/pkg/runtime/386/memmove.s | 12 ------------ src/pkg/runtime/amd64/closure.c | 2 +- src/pkg/runtime/amd64/memmove.s | 11 ----------- src/pkg/runtime/arm/closure.c | 2 +- src/pkg/runtime/hashmap.h | 2 +- src/pkg/runtime/plan9/thread.c | 4 ++-- src/pkg/runtime/proc.c | 14 +++++++------- src/pkg/runtime/runtime.c | 11 ----------- src/pkg/runtime/runtime.h | 1 - src/pkg/runtime/string.goc | 14 +++++++------- src/pkg/runtime/symtab.c | 2 +- 13 files changed, 22 insertions(+), 57 deletions(-) diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 6802dd1cf3..5999807322 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -708,7 +708,7 @@ void ·_Cfunc_CString(String s, int8 *p) { p = runtime·cmalloc(s.len+1); - runtime·mcpy((byte*)p, s.str, s.len); + runtime·memmove((byte*)p, s.str, s.len); p[s.len] = 0; FLUSH(&p); } diff --git a/src/pkg/runtime/386/closure.c b/src/pkg/runtime/386/closure.c index b0d4cc41a9..b4d8677114 100644 --- a/src/pkg/runtime/386/closure.c +++ b/src/pkg/runtime/386/closure.c @@ -45,7 +45,7 @@ runtime·closure(int32 siz, byte *fn, byte *arg0) q = p + n - siz; if(siz > 0) { - runtime·mcpy(q, (byte*)&arg0, siz); + runtime·memmove(q, (byte*)&arg0, siz); // SUBL $siz, SP *p++ = 0x81; diff --git a/src/pkg/runtime/386/memmove.s b/src/pkg/runtime/386/memmove.s index 471553ba21..203a8187c0 100644 --- a/src/pkg/runtime/386/memmove.s +++ b/src/pkg/runtime/386/memmove.s @@ -27,9 +27,6 @@ TEXT runtime·memmove(SB), 7, $0 MOVL to+0(FP), DI MOVL fr+4(FP), SI MOVL n+8(FP), BX - CMPL BX, $0 - JLT fault - /* * check and set for backwards */ @@ -87,12 +84,3 @@ back: MOVL to+0(FP),AX RET -/* - * if called with negative count, - * treat as error rather than - * rotating all of memory - */ -fault: - MOVL $0,SI - MOVL 0(SI), AX - RET diff --git a/src/pkg/runtime/amd64/closure.c b/src/pkg/runtime/amd64/closure.c index 5033468d28..481b4a8882 100644 --- a/src/pkg/runtime/amd64/closure.c +++ b/src/pkg/runtime/amd64/closure.c @@ -45,7 +45,7 @@ runtime·closure(int32 siz, byte *fn, byte *arg0) q = p + n - siz; if(siz > 0) { - runtime·mcpy(q, (byte*)&arg0, siz); + runtime·memmove(q, (byte*)&arg0, siz); // SUBQ $siz, SP *p++ = 0x48; diff --git a/src/pkg/runtime/amd64/memmove.s b/src/pkg/runtime/amd64/memmove.s index fc9573f72e..e78be81455 100644 --- a/src/pkg/runtime/amd64/memmove.s +++ b/src/pkg/runtime/amd64/memmove.s @@ -28,8 +28,6 @@ TEXT runtime·memmove(SB), 7, $0 MOVQ to+0(FP), DI MOVQ fr+8(FP), SI MOVLQSX n+16(FP), BX - CMPQ BX, $0 - JLT fault /* * check and set for backwards @@ -88,12 +86,3 @@ back: MOVQ to+0(FP),AX RET -/* - * if called with negative count, - * treat as error rather than - * rotating all of memory - */ -fault: - MOVQ $0,SI - MOVQ 0(SI), AX - RET diff --git a/src/pkg/runtime/arm/closure.c b/src/pkg/runtime/arm/closure.c index 36a93bc532..119e91b611 100644 --- a/src/pkg/runtime/arm/closure.c +++ b/src/pkg/runtime/arm/closure.c @@ -83,7 +83,7 @@ runtime·closure(int32 siz, byte *fn, byte *arg0) *pc++ = 0xe52de000 | (siz + 4); if(siz > 0) { - runtime·mcpy(q, (byte*)&arg0, siz); + runtime·memmove(q, (byte*)&arg0, siz); // MOVW $vars(PC), R0 *pc = 0xe28f0000 | (int32)(q - (byte*)pc - 8); diff --git a/src/pkg/runtime/hashmap.h b/src/pkg/runtime/hashmap.h index d0fd3527fc..19ff416970 100644 --- a/src/pkg/runtime/hashmap.h +++ b/src/pkg/runtime/hashmap.h @@ -65,7 +65,7 @@ #define malloc runtime·mal #define memset(a,b,c) runtime·memclr((byte*)(a), (uint32)(c)) -#define memcpy(a,b,c) runtime·mcpy((byte*)(a),(byte*)(b),(uint32)(c)) +#define memcpy(a,b,c) runtime·memmove((byte*)(a),(byte*)(b),(uint32)(c)) #define assert(a) if(!(a)) runtime·throw("assert") #define free(x) runtime·free(x) #define memmove(a,b,c) runtime·memmove(a, b, c) diff --git a/src/pkg/runtime/plan9/thread.c b/src/pkg/runtime/plan9/thread.c index ef9a23e8e2..d428e7fcde 100644 --- a/src/pkg/runtime/plan9/thread.c +++ b/src/pkg/runtime/plan9/thread.c @@ -47,11 +47,11 @@ runtime·exit(int32) pid = pid/10; } p = buf; - runtime·mcpy((void*)p, (void*)"/proc/", 6); + runtime·memmove((void*)p, (void*)"/proc/", 6); p += 6; for(q--; q >= tmp;) *p++ = *q--; - runtime·mcpy((void*)p, (void*)"/notepg", 7); + runtime·memmove((void*)p, (void*)"/notepg", 7); /* post interrupt note */ fd = runtime·open(buf, OWRITE); diff --git a/src/pkg/runtime/proc.c b/src/pkg/runtime/proc.c index 17397ca821..1524a627da 100644 --- a/src/pkg/runtime/proc.c +++ b/src/pkg/runtime/proc.c @@ -720,7 +720,7 @@ runtime·oldstack(void) argsize = old.argsize; if(argsize > 0) { sp -= argsize; - runtime·mcpy(top->argp, sp, argsize); + runtime·memmove(top->argp, sp, argsize); } goid = old.gobuf.g->goid; // fault if g is bad, before gogo USED(goid); @@ -802,7 +802,7 @@ runtime·newstack(void) sp = (byte*)top; if(argsize > 0) { sp -= argsize; - runtime·mcpy(sp, m->moreargp, argsize); + runtime·memmove(sp, m->moreargp, argsize); } if(thechar == '5') { // caller would have saved its LR below args. @@ -903,7 +903,7 @@ runtime·newproc1(byte *fn, byte *argp, int32 narg, int32 nret, void *callerpc) sp = newg->stackbase; sp -= siz; - runtime·mcpy(sp, argp, narg); + runtime·memmove(sp, argp, narg); if(thechar == '5') { // caller's LR sp -= sizeof(void*); @@ -941,7 +941,7 @@ runtime·deferproc(int32 siz, byte* fn, ...) d->argp = (byte*)(&fn+2); // skip caller's saved link register else d->argp = (byte*)(&fn+1); - runtime·mcpy(d->args, d->argp, d->siz); + runtime·memmove(d->args, d->argp, d->siz); d->link = g->defer; g->defer = d; @@ -968,7 +968,7 @@ runtime·deferreturn(uintptr arg0) argp = (byte*)&arg0; if(d->argp != argp) return; - runtime·mcpy(argp, d->args, d->siz); + runtime·memmove(argp, d->args, d->siz); g->defer = d->link; fn = d->fn; runtime·free(d); @@ -1367,11 +1367,11 @@ os·setenv_c(String k, String v) return; arg[0] = runtime·malloc(k.len + 1); - runtime·mcpy(arg[0], k.str, k.len); + runtime·memmove(arg[0], k.str, k.len); arg[0][k.len] = 0; arg[1] = runtime·malloc(v.len + 1); - runtime·mcpy(arg[1], v.str, v.len); + runtime·memmove(arg[1], v.str, v.len); arg[1][v.len] = 0; runtime·asmcgocall(libcgo_setenv, arg); diff --git a/src/pkg/runtime/runtime.c b/src/pkg/runtime/runtime.c index 83af8dc5e2..7e37d66d41 100644 --- a/src/pkg/runtime/runtime.c +++ b/src/pkg/runtime/runtime.c @@ -116,17 +116,6 @@ runtime·panicstring(int8 *s) runtime·panic(err); } -void -runtime·mcpy(byte *t, byte *f, uint32 n) -{ - while(n > 0) { - *t = *f; - t++; - f++; - n--; - } -} - int32 runtime·mcmp(byte *s1, byte *s2, uint32 n) { diff --git a/src/pkg/runtime/runtime.h b/src/pkg/runtime/runtime.h index de0a21b956..ef17b72d69 100644 --- a/src/pkg/runtime/runtime.h +++ b/src/pkg/runtime/runtime.h @@ -406,7 +406,6 @@ uint32 runtime·rnd(uint32, uint32); void runtime·prints(int8*); void runtime·printf(int8*, ...); byte* runtime·mchr(byte*, byte, byte*); -void runtime·mcpy(byte*, byte*, uint32); int32 runtime·mcmp(byte*, byte*, uint32); void runtime·memmove(void*, void*, uint32); void* runtime·mal(uintptr); diff --git a/src/pkg/runtime/string.goc b/src/pkg/runtime/string.goc index 15b3459ada..34b167791e 100644 --- a/src/pkg/runtime/string.goc +++ b/src/pkg/runtime/string.goc @@ -60,7 +60,7 @@ runtime·gostring(byte *str) l = runtime·findnull(str); s = runtime·gostringsize(l); - runtime·mcpy(s.str, str, l); + runtime·memmove(s.str, str, l); return s; } @@ -70,7 +70,7 @@ runtime·gostringn(byte *str, int32 l) String s; s = runtime·gostringsize(l); - runtime·mcpy(s.str, str, l); + runtime·memmove(s.str, str, l); return s; } @@ -113,8 +113,8 @@ runtime·catstring(String s1, String s2) return s1; s3 = runtime·gostringsize(s1.len + s2.len); - runtime·mcpy(s3.str, s1.str, s1.len); - runtime·mcpy(s3.str+s1.len, s2.str, s2.len); + runtime·memmove(s3.str, s1.str, s1.len); + runtime·memmove(s3.str+s1.len, s2.str, s2.len); return s3; } @@ -134,7 +134,7 @@ concatstring(int32 n, String *s) out = runtime·gostringsize(l); l = 0; for(i=0; i buf && p[-1] != '/') *p++ = '/'; - runtime·mcpy(p, q, len+1); + runtime·memmove(p, q, len+1); p += len; } } -- 2.50.0