Skip to content

Commit 3db12eb

Browse files
committedAug 16, 2017
Reorg so that Get() is implemented fully in assembly on amd64
This is a minor perf win and reduces future maintenance. The runtime structures are now encapsulated in the runtime_goX.Y.go files. The same assembly implementation of Get() can be used until the runtime structure changes significantly (i.e. `g.goid` is moved, which is unlikely). name old time/op new time/op delta Get-8 2.88ns ± 3% 1.70ns ± 2% -41.05% (p=0.000 n=10+9)
1 parent 88fe90f commit 3db12eb

8 files changed

+59
-28
lines changed
 

‎goid_go1.5_amd64.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
16+
// +build amd64 amd64p32
17+
// +build go1.5
18+
19+
package goid
20+
21+
func Get() int64

‎goid_go1.5plus.s ‎goid_go1.5_amd64.s

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
// +build amd64 amd64p32
1919
// +build go1.5
2020

21+
#include "go_asm.h"
2122
#include "textflag.h"
2223

23-
// func getg() *g
24-
TEXT ·getg(SB),NOSPLIT,$0-8
25-
MOVQ (TLS), BX
26-
MOVQ BX, ret+0(FP)
24+
// func Get() int64
25+
TEXT ·Get(SB),NOSPLIT,$0-8
26+
MOVQ (TLS), R14
27+
MOVQ g_goid(R14), R13
28+
MOVQ R13, ret+0(FP)
2729
RET

‎goid_go1.5_arm.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
16+
// +build arm
17+
// +build go1.5
18+
19+
package goid
20+
21+
// Backdoor access to runtime·getg().
22+
func getg() *g // in goid_go1.5plus.s
23+
24+
func Get() int64 {
25+
return getg().goid
26+
}
File renamed without changes.

‎goid_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ func TestGet(t *testing.T) {
4242
}
4343
}
4444
}
45+
46+
func BenchmarkGet(b *testing.B) {
47+
for i := 0; i < b.N; i++ {
48+
Get()
49+
}
50+
}

‎goid_go1.5.go ‎runtime_go1.5.go

-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// permissions and limitations under the License. See the AUTHORS file
1414
// for names of contributors.
1515

16-
// +build amd64 amd64p32 arm
1716
// +build go1.5,!go1.6
1817

1918
package goid
@@ -55,10 +54,3 @@ type g struct {
5554
stackLock uint32
5655
goid int64 // Here it is!
5756
}
58-
59-
// Backdoor access to runtime·getg().
60-
func getg() *g // in goid_go1.5plus.s
61-
62-
func Get() int64 {
63-
return getg().goid
64-
}

‎goid_go1.6plus.go ‎runtime_go1.6.go

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// +build amd64 amd64p32 arm
21
// +build go1.6,!go1.9
32

43
package goid
@@ -41,10 +40,3 @@ type g struct {
4140
stackLock uint32
4241
goid int64 // Here it is!
4342
}
44-
45-
// Backdoor access to runtime·getg().
46-
func getg() *g // in goid_go1.5plus{,_arm}.s
47-
48-
func Get() int64 {
49-
return getg().goid
50-
}

‎goid_go1.9plus.go ‎runtime_go1.9.go

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// +build amd64 amd64p32 arm
21
// +build go1.9
32

43
package goid
@@ -35,10 +34,3 @@ type g struct {
3534
stackLock uint32
3635
goid int64 // Here it is!
3736
}
38-
39-
// Backdoor access to runtime·getg().
40-
func getg() *g // in goid_go1.5plus{,_arm}.s
41-
42-
func Get() int64 {
43-
return getg().goid
44-
}

0 commit comments

Comments
 (0)
Please sign in to comment.