]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/string.go
runtime: speed up non-ASCII rune decoding
[gostls13.git] / src / runtime / string.go
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime
6
7 import "unsafe"
8
9 // The constant is known to the compiler.
10 // There is no fundamental theory behind this number.
11 const tmpStringBufSize = 32
12
13 type tmpBuf [tmpStringBufSize]byte
14
15 // concatstrings implements a Go string concatenation x+y+z+...
16 // The operands are passed in the slice a.
17 // If buf != nil, the compiler has determined that the result does not
18 // escape the calling function, so the string data can be stored in buf
19 // if small enough.
20 func concatstrings(buf *tmpBuf, a []string) string {
21         idx := 0
22         l := 0
23         count := 0
24         for i, x := range a {
25                 n := len(x)
26                 if n == 0 {
27                         continue
28                 }
29                 if l+n < l {
30                         throw("string concatenation too long")
31                 }
32                 l += n
33                 count++
34                 idx = i
35         }
36         if count == 0 {
37                 return ""
38         }
39
40         // If there is just one string and either it is not on the stack
41         // or our result does not escape the calling frame (buf != nil),
42         // then we can return that string directly.
43         if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
44                 return a[idx]
45         }
46         s, b := rawstringtmp(buf, l)
47         for _, x := range a {
48                 copy(b, x)
49                 b = b[len(x):]
50         }
51         return s
52 }
53
54 func concatstring2(buf *tmpBuf, a [2]string) string {
55         return concatstrings(buf, a[:])
56 }
57
58 func concatstring3(buf *tmpBuf, a [3]string) string {
59         return concatstrings(buf, a[:])
60 }
61
62 func concatstring4(buf *tmpBuf, a [4]string) string {
63         return concatstrings(buf, a[:])
64 }
65
66 func concatstring5(buf *tmpBuf, a [5]string) string {
67         return concatstrings(buf, a[:])
68 }
69
70 // Buf is a fixed-size buffer for the result,
71 // it is not nil if the result does not escape.
72 func slicebytetostring(buf *tmpBuf, b []byte) string {
73         l := len(b)
74         if l == 0 {
75                 // Turns out to be a relatively common case.
76                 // Consider that you want to parse out data between parens in "foo()bar",
77                 // you find the indices and convert the subslice to string.
78                 return ""
79         }
80         if raceenabled && l > 0 {
81                 racereadrangepc(unsafe.Pointer(&b[0]),
82                         uintptr(l),
83                         getcallerpc(unsafe.Pointer(&buf)),
84                         funcPC(slicebytetostring))
85         }
86         if msanenabled && l > 0 {
87                 msanread(unsafe.Pointer(&b[0]), uintptr(l))
88         }
89         s, c := rawstringtmp(buf, l)
90         copy(c, b)
91         return s
92 }
93
94 // stringDataOnStack reports whether the string's data is
95 // stored on the current goroutine's stack.
96 func stringDataOnStack(s string) bool {
97         ptr := uintptr(stringStructOf(&s).str)
98         stk := getg().stack
99         return stk.lo <= ptr && ptr < stk.hi
100 }
101
102 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
103         if buf != nil && l <= len(buf) {
104                 b = buf[:l]
105                 s = slicebytetostringtmp(b)
106         } else {
107                 s, b = rawstring(l)
108         }
109         return
110 }
111
112 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
113 //
114 // Callers need to ensure that the returned string will not be used after
115 // the calling goroutine modifies the original slice or synchronizes with
116 // another goroutine.
117 //
118 // The function is only called when instrumenting
119 // and otherwise intrinsified by the compiler.
120 //
121 // Some internal compiler optimizations use this function.
122 // - Used for m[string(k)] lookup where m is a string-keyed map and k is a []byte.
123 // - Used for "<"+string(b)+">" concatenation where b is []byte.
124 // - Used for string(b)=="foo" comparison where b is []byte.
125 func slicebytetostringtmp(b []byte) string {
126         if raceenabled && len(b) > 0 {
127                 racereadrangepc(unsafe.Pointer(&b[0]),
128                         uintptr(len(b)),
129                         getcallerpc(unsafe.Pointer(&b)),
130                         funcPC(slicebytetostringtmp))
131         }
132         if msanenabled && len(b) > 0 {
133                 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
134         }
135         return *(*string)(unsafe.Pointer(&b))
136 }
137
138 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
139         var b []byte
140         if buf != nil && len(s) <= len(buf) {
141                 *buf = tmpBuf{}
142                 b = buf[:len(s)]
143         } else {
144                 b = rawbyteslice(len(s))
145         }
146         copy(b, s)
147         return b
148 }
149
150 func stringtoslicebytetmp(s string) []byte {
151         // Return a slice referring to the actual string bytes.
152         // This is only for use by internal compiler optimizations
153         // that know that the slice won't be mutated.
154         // The only such case today is:
155         // for i, c := range []byte(str)
156
157         str := stringStructOf(&s)
158         ret := slice{array: str.str, len: str.len, cap: str.len}
159         return *(*[]byte)(unsafe.Pointer(&ret))
160 }
161
162 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
163         // two passes.
164         // unlike slicerunetostring, no race because strings are immutable.
165         n := 0
166         for range s {
167                 n++
168         }
169
170         var a []rune
171         if buf != nil && n <= len(buf) {
172                 *buf = [tmpStringBufSize]rune{}
173                 a = buf[:n]
174         } else {
175                 a = rawruneslice(n)
176         }
177
178         n = 0
179         for _, r := range s {
180                 a[n] = r
181                 n++
182         }
183         return a
184 }
185
186 func slicerunetostring(buf *tmpBuf, a []rune) string {
187         if raceenabled && len(a) > 0 {
188                 racereadrangepc(unsafe.Pointer(&a[0]),
189                         uintptr(len(a))*unsafe.Sizeof(a[0]),
190                         getcallerpc(unsafe.Pointer(&buf)),
191                         funcPC(slicerunetostring))
192         }
193         if msanenabled && len(a) > 0 {
194                 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
195         }
196         var dum [4]byte
197         size1 := 0
198         for _, r := range a {
199                 size1 += encoderune(dum[:], r)
200         }
201         s, b := rawstringtmp(buf, size1+3)
202         size2 := 0
203         for _, r := range a {
204                 // check for race
205                 if size2 >= size1 {
206                         break
207                 }
208                 size2 += encoderune(b[size2:], r)
209         }
210         return s[:size2]
211 }
212
213 type stringStruct struct {
214         str unsafe.Pointer
215         len int
216 }
217
218 // Variant with *byte pointer type for DWARF debugging.
219 type stringStructDWARF struct {
220         str *byte
221         len int
222 }
223
224 func stringStructOf(sp *string) *stringStruct {
225         return (*stringStruct)(unsafe.Pointer(sp))
226 }
227
228 func intstring(buf *[4]byte, v int64) string {
229         var s string
230         var b []byte
231         if buf != nil {
232                 b = buf[:]
233                 s = slicebytetostringtmp(b)
234         } else {
235                 s, b = rawstring(4)
236         }
237         if int64(rune(v)) != v {
238                 v = runeError
239         }
240         n := encoderune(b, rune(v))
241         return s[:n]
242 }
243
244 // rawstring allocates storage for a new string. The returned
245 // string and byte slice both refer to the same storage.
246 // The storage is not zeroed. Callers should use
247 // b to set the string contents and then drop b.
248 func rawstring(size int) (s string, b []byte) {
249         p := mallocgc(uintptr(size), nil, false)
250
251         stringStructOf(&s).str = p
252         stringStructOf(&s).len = size
253
254         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
255
256         return
257 }
258
259 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
260 func rawbyteslice(size int) (b []byte) {
261         cap := roundupsize(uintptr(size))
262         p := mallocgc(cap, nil, false)
263         if cap != uintptr(size) {
264                 memclr(add(p, uintptr(size)), cap-uintptr(size))
265         }
266
267         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
268         return
269 }
270
271 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
272 func rawruneslice(size int) (b []rune) {
273         if uintptr(size) > _MaxMem/4 {
274                 throw("out of memory")
275         }
276         mem := roundupsize(uintptr(size) * 4)
277         p := mallocgc(mem, nil, false)
278         if mem != uintptr(size)*4 {
279                 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
280         }
281
282         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
283         return
284 }
285
286 // used by cmd/cgo
287 func gobytes(p *byte, n int) []byte {
288         if n == 0 {
289                 return make([]byte, 0)
290         }
291         x := make([]byte, n)
292         memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
293         return x
294 }
295
296 func gostring(p *byte) string {
297         l := findnull(p)
298         if l == 0 {
299                 return ""
300         }
301         s, b := rawstring(l)
302         memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
303         return s
304 }
305
306 func gostringn(p *byte, l int) string {
307         if l == 0 {
308                 return ""
309         }
310         s, b := rawstring(l)
311         memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
312         return s
313 }
314
315 func index(s, t string) int {
316         if len(t) == 0 {
317                 return 0
318         }
319         for i := 0; i < len(s); i++ {
320                 if s[i] == t[0] && hasprefix(s[i:], t) {
321                         return i
322                 }
323         }
324         return -1
325 }
326
327 func contains(s, t string) bool {
328         return index(s, t) >= 0
329 }
330
331 func hasprefix(s, t string) bool {
332         return len(s) >= len(t) && s[:len(t)] == t
333 }
334
335 func atoi(s string) int {
336         n := 0
337         for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
338                 n = n*10 + int(s[0]) - '0'
339                 s = s[1:]
340         }
341         return n
342 }
343
344 //go:nosplit
345 func findnull(s *byte) int {
346         if s == nil {
347                 return 0
348         }
349         p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
350         l := 0
351         for p[l] != 0 {
352                 l++
353         }
354         return l
355 }
356
357 func findnullw(s *uint16) int {
358         if s == nil {
359                 return 0
360         }
361         p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
362         l := 0
363         for p[l] != 0 {
364                 l++
365         }
366         return l
367 }
368
369 //go:nosplit
370 func gostringnocopy(str *byte) string {
371         ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
372         s := *(*string)(unsafe.Pointer(&ss))
373         return s
374 }
375
376 func gostringw(strw *uint16) string {
377         var buf [8]byte
378         str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
379         n1 := 0
380         for i := 0; str[i] != 0; i++ {
381                 n1 += encoderune(buf[:], rune(str[i]))
382         }
383         s, b := rawstring(n1 + 4)
384         n2 := 0
385         for i := 0; str[i] != 0; i++ {
386                 // check for race
387                 if n2 >= n1 {
388                         break
389                 }
390                 n2 += encoderune(b[n2:], rune(str[i]))
391         }
392         b[n2] = 0 // for luck
393         return s[:n2]
394 }