Halo, Khabrovites. Kami telah menyiapkan terjemahan yang menarik untuk Anda menjelang dimulainya kursus "Pengembang Golang" .
Jika Anda sangat membutuhkan pengurangan ukuran binari Go sebesar 6%, artikel ini cocok untuk Anda. (Saya melakukan eksperimen ini dengan bantuan dari Tailscale . Perhatikan bahwa saya seorang investor.) Jika Anda tidak terlalu peduli tentang ukuran binari, Anda setidaknya mungkin tertarik membaca ini untuk bersenang-senang.
Untuk mendapatkan beberapa angka kasar untuk artikel ini, saya mengambil file pertama yang saya temukan dari GOPATH saya. Semua nomor spesifik dalam artikel ini merujuk ke github.com/mvdan/sh/cmd/shfmt. Setelah beberapa percobaan, mereka tampak cukup mewakili saya.
9d812cfa5c Go. master 29 2020 .; , Go 1.15beta1. , Go 1.14, , , , .
. , , . sync.Once. . (โฆ , ). , : -ldflags=-w go build.
, . . , .
Go , . , Go. . . . ( .)
.
. .
. . , ( gofmt) , .
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}package main
import ( "fmt" ); func main() { fmt.Println("Hello, playground") }Go , .
--- a/src/cmd/compile/internal/syntax/pos.go
+++ b/src/cmd/compile/internal/syntax/pos.go
@@ -23,3 +23,3 @@ type Pos struct {
// MakePos Pos PosBase, .
-func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, sat32(line), sat32(col)} }
+func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, 1, 1} }
@@ -101,2 +101,3 @@ type PosBase struct {
func NewFileBase(filename string) *PosBase {
+ filename = "x.go"
base := &PosBase{MakePos(nil, linebase, colbase), filename, linebase, colbase} x.go, 1. ( , DWARF).
. , , x.go:1:1.
DWARF . : DWARF, DWARF .
- cgo. , cgo. (, ), .
https://github.com/josharian/go/commit/1a3e66ceed.
, , x.go:1:1.
, -ldflags=-w, 3,126,800 2,938,384 , 6%.
. - .
-:
func f(x []byte) {
_ = x[0]
_ = x[1]
}func f(x []byte) {
_, _ = x[0], x[1]
} go tool compile -S x.go , , runtime.panicIndex. . , runtime.panicIndex , , . , . - , .
, , .
, ? , . - , , . . x.go:1. , , . Pprof - , , , .
. , ? 0,9%. , , 1 5,1%.
, . , , 16? diff :
--- a/src/cmd/compile/internal/syntax/pos.go
+++ b/src/cmd/compile/internal/syntax/pos.go
@@ -23,3 +23,3 @@ type Pos struct {
// MakePos Pos PosBase, .
-func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, 1, 1} }
+func MakePos(base PosBase, line, col uint) Pos { return Pos{base, sat32(line/1616 + 1), 1} }2,2%. . , 16? , , , ยซยป .
--- a/src/cmd/compile/internal/syntax/pos.go
+++ b/src/cmd/compile/internal/syntax/pos.go
@@ -23,3 +23,3 @@ type Pos struct {
// MakePos Pos PosBase, .
-func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, 1, 1} }
+func MakePos(base *PosBase, line, col uint) Pos { return Pos{base, sat32(line/16 + 1), 1} } 2,75%! /16 0,5% , /16*16?
varint . , .