Life in one line

Hello, Khabrovites. We have prepared an interesting translation for you on the eve of the start of the "Golang Developer" course .


If reducing the size of Go binaries by 6% is what you desperately need, then this article is for you. (I did this experiment with help from Tailscale . Note that I'm an investor.) If you don't care too much about binary size, well, you might at least be interested in reading this for fun.

To get some rough figures for this article, I grabbed the first file I came across from my GOPATH. All specific numbers in this article refer to github.com/mvdan/sh/cmd/shfmt. After some experimentation, they seem pretty representative to me.

I use the Go toolchain commit 9d812cfa5c as the base commit . This is the master branch as of April 29, 2020; it will probably be similar to Go 1.15beta1. I'm using it, not Go 1.14, because it includes several size reductions for binaries, including one specific one , which you'll definitely need if size is important to you.

. , , . 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 , .

, -toolexec //line, . , , .

--- 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 . , .


, , .


:




All Articles