From: Dmitriy Vyukov Date: Wed, 12 Oct 2011 14:40:02 +0000 (+0300) Subject: runtime: faster strings X-Git-Tag: weekly.2011-10-18~115 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=af1232fe38b1decdeceaf7dc0050622a79c04b10;p=gostls13.git runtime: faster strings Use FlagNoPointers and do not zeroize memory when allocate strings. test/garbage/parser.out old new run #1 32.923s 32.065s run #2 33.047s 31.931s run #3 32.702s 31.841s run #4 32.718s 31.838s run #5 32.702s 31.868s R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5259041 --- diff --git a/src/cmd/prof/gopprof b/src/cmd/prof/gopprof index 83438b7cd6..49052ac064 100755 --- a/src/cmd/prof/gopprof +++ b/src/cmd/prof/gopprof @@ -2615,7 +2615,6 @@ sub RemoveUninterestingFrames { 'mal', 'runtime.new', 'makeslice1', - 'runtime.gostringsize', 'runtime.malloc', 'unsafe.New', 'runtime.mallocgc', diff --git a/src/pkg/runtime/string.goc b/src/pkg/runtime/string.goc index 8c59bdd337..53cd84c6e6 100644 --- a/src/pkg/runtime/string.goc +++ b/src/pkg/runtime/string.goc @@ -35,16 +35,18 @@ runtime·findnullw(uint16 *s) uint32 runtime·maxstring = 256; -String -runtime·gostringsize(int32 l) +static String +gostringsize(int32 l) { String s; uint32 ms; if(l == 0) return runtime·emptystring; - s.str = runtime·mal(l+1); // leave room for NUL for C runtime (e.g., callers of getenv) + // leave room for NUL for C runtime (e.g., callers of getenv) + s.str = runtime·mallocgc(l+1, FlagNoPointers, 1, 0); s.len = l; + s.str[l] = 0; for(;;) { ms = runtime·maxstring; if((uint32)l <= ms || runtime·cas(&runtime·maxstring, ms, (uint32)l)) @@ -60,7 +62,7 @@ runtime·gostring(byte *str) String s; l = runtime·findnull(str); - s = runtime·gostringsize(l); + s = gostringsize(l); runtime·memmove(s.str, str, l); return s; } @@ -70,7 +72,7 @@ runtime·gostringn(byte *str, int32 l) { String s; - s = runtime·gostringsize(l); + s = gostringsize(l); runtime·memmove(s.str, str, l); return s; } @@ -100,18 +102,23 @@ runtime·gostringnocopy(byte *str) String runtime·gostringw(uint16 *str) { - int32 n, i; + int32 n1, n2, i; byte buf[8]; String s; - n = 0; - for(i=0; str[i]; i++) - n += runtime·runetochar(buf, str[i]); - s = runtime·gostringsize(n+4); - n = 0; + n1 = 0; for(i=0; str[i]; i++) - n += runtime·runetochar(s.str+n, str[i]); - s.len = n; + n1 += runtime·runetochar(buf, str[i]); + s = gostringsize(n1+4); + n2 = 0; + for(i=0; str[i]; i++) { + // check for race + if(n2 >= n1) + break; + n2 += runtime·runetochar(s.str+n2, str[i]); + } + s.len = n2; + s.str[s.len] = 0; return s; } @@ -125,7 +132,7 @@ runtime·catstring(String s1, String s2) if(s2.len == 0) return s1; - s3 = runtime·gostringsize(s1.len + s2.len); + s3 = gostringsize(s1.len + s2.len); runtime·memmove(s3.str, s1.str, s1.len); runtime·memmove(s3.str+s1.len, s2.str, s2.len); return s3; @@ -144,7 +151,7 @@ concatstring(int32 n, String *s) l += s[i].len; } - out = runtime·gostringsize(l); + out = gostringsize(l); l = 0; for(i=0; i