Commit 6dd734b
committed
drop init()
Initializing a package global map and compiling regexes increases the init
functions and adds up to the start time of executables.
This change implements two approaches:
- compile the regex on its first use
- rewrite the map to a switch case statement
Minimal example:
```
package main
import "github.com/hashicorp/go-version"
func main() {
v, err := version.NewSemver("1.2.3")
if err != nil {
panic(err)
}
println(v.String())
}
```
On Go 1.25.4 linux/amd64 the above code compiles to ~2728103 bytes and
GODEBUG=inittrace=1 shows the following:
init github.com/hashicorp/go-version @0.90 ms, 0.37 ms clock, 80992 bytes, 504 allocs
With this change applied on the same system, the above code compiles to ~2703283
bytes (reduction of ~24820 bytes) and GODEBUG=inittrace=1 does not list any init entry for
github.com/hashicorp/go-version anymore.
Signed-off-by: Florian Lehner <[email protected]>1 parent 0824a89 commit 6dd734b
2 files changed
+59
-38
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
13 | 31 | | |
14 | 32 | | |
15 | 33 | | |
| |||
29 | 47 | | |
30 | 48 | | |
31 | 49 | | |
32 | | - | |
33 | | - | |
34 | 50 | | |
35 | 51 | | |
36 | 52 | | |
37 | 53 | | |
38 | 54 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | 55 | | |
65 | 56 | | |
66 | 57 | | |
| |||
176 | 167 | | |
177 | 168 | | |
178 | 169 | | |
179 | | - | |
| 170 | + | |
180 | 171 | | |
181 | 172 | | |
182 | 173 | | |
| |||
186 | 177 | | |
187 | 178 | | |
188 | 179 | | |
189 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
190 | 199 | | |
191 | 200 | | |
192 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | | - | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
20 | 37 | | |
21 | 38 | | |
22 | 39 | | |
| |||
41 | 58 | | |
42 | 59 | | |
43 | 60 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | 61 | | |
50 | 62 | | |
51 | 63 | | |
52 | | - | |
| 64 | + | |
53 | 65 | | |
54 | 66 | | |
55 | 67 | | |
56 | 68 | | |
57 | 69 | | |
58 | 70 | | |
59 | | - | |
| 71 | + | |
60 | 72 | | |
61 | 73 | | |
62 | 74 | | |
| |||
0 commit comments