-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Commit 71446e9
os: optimise cgo clearenv
For programs with very large environments, calling unsetenv(3) for each
environment variable can be fairly expensive because of CGo overhead,
but clearenv(3) is much faster. The only thing we have to track is
whether GODEBUG is being unset by the operation, which can be done very
quickly without resorting to doing unsetenv(3) for every variable.
This change makes os.Clearenv() ~30% faster when run an environment with
1000 environment variable set (and gets better with more environment
variables):
cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics
│ old.txt │ new.txt │
│ sec/op │ sec/op vs base │
Env-16 213.6μ ± 1% 185.1μ ± 3% -13.32% (p=0.000 n=10)
(The time taken to set up 1000 variables in this benchmark is 117.2μs,
so while the naive timing is 13% the speedup for os.Clearenv() itself
the speedup is from 96.4μs to 67.9μs which is ~30%.)
Change-Id: I589794845854e1721e6fc5badc671474c2bbd0371 parent c46ba1f commit 71446e9
File tree
6 files changed
+87
-3
lines changed- src
- runtime
- cgo
- syscall
6 files changed
+87
-3
lines changedLines changed: 15 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + |
Lines changed: 19 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + |
Lines changed: 29 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + | ||
28 | + | ||
29 | + |
Lines changed: 18 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + |
Lines changed: 2 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
129 | 129 |
| |
130 | 130 |
| |
131 | 131 |
| |
132 | - | ||
133 | - | ||
134 | - | ||
132 | + | ||
133 | + | ||
135 | 134 |
| |
136 | 135 |
| |
137 | 136 |
| |
|
Lines changed: 4 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
104 | 104 |
| |
105 | 105 |
| |
106 | 106 |
| |
107 | + | ||
108 | + | ||
109 | + | ||
110 | + |
0 commit comments