mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Added []byte->string optimization for switch and string comparisons
parent
221e1dd1a1
commit
cd947d9741
@ -27,9 +27,21 @@ For a map `m` of type `map[string]T` and `[]byte b`, `m[string(b)]` doesn't allo
|
|||||||
* **gc:** 1.4+
|
* **gc:** 1.4+
|
||||||
* **gccgo:** ?
|
* **gccgo:** ?
|
||||||
|
|
||||||
|
### `switch` on `[]byte`(s)
|
||||||
|
|
||||||
|
No allocation done on the `switch` line.
|
||||||
|
|
||||||
|
```go
|
||||||
|
var b []byte
|
||||||
|
switch string(b) {
|
||||||
|
case "AA":
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### `range` over `[]byte`(s)
|
### `range` over `[]byte`(s)
|
||||||
|
|
||||||
Avoiding allocating `[]byte` of a `string` when ranging over the bytes:
|
No allocation when converting a `string` into a `[]byte` for ranging over the bytes:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
s := "foo"
|
s := "foo"
|
||||||
@ -38,6 +50,17 @@ for i, c := range []byte(s) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### conversion for string comparison
|
||||||
|
|
||||||
|
No allocation done when converting a `[]byte` into a `string` for comparison purposes
|
||||||
|
|
||||||
|
```go
|
||||||
|
var b1 string
|
||||||
|
var b2 []byte
|
||||||
|
var x = string(b1) == string(b2) // memeq
|
||||||
|
var y = string(b1) < string(b2) // lexicographical comparison
|
||||||
|
```
|
||||||
|
|
||||||
* **gc:** 1.5+ (CL 3790)
|
* **gc:** 1.5+ (CL 3790)
|
||||||
* **gccgo:** ?
|
* **gccgo:** ?
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user