Skip to content

Commit 6c11e27

Browse files
committed
cmd/compile: use delta encoding for filenames in export data position info
This reduces the export data size significantly (15%-25%) for some packages, especially where the paths are very long or if there are many files involved. Slight (2%) reduction on average, with virtually no increases in export data size. Selected export data sizes for packages with |delta %| > 3%: package before after delta % cmd/asm/internal/arch 11647 11088 -559 -4% cmd/compile/internal/amd64 838 600 -238 -27% cmd/compile/internal/arm 7323 6793 -530 -6% cmd/compile/internal/arm64 19948 18971 -977 -4% cmd/compile/internal/big 9043 8548 -495 -4% cmd/compile/internal/mips64 645 482 -163 -24% cmd/compile/internal/ppc64 695 497 -198 -27% cmd/compile/internal/s390x 553 433 -120 -21% cmd/compile/internal/x86 744 555 -189 -24% cmd/dist 145 121 -24 -16% cmd/internal/objfile 17359 16474 -885 -4% cmd/internal/pprof/symbolz 8346 7941 -405 -4% cmd/link/internal/amd64 11178 10604 -574 -4% cmd/link/internal/arm 204 171 -33 -15% cmd/link/internal/arm64 210 175 -35 -16% cmd/link/internal/mips64 213 177 -36 -16% cmd/link/internal/ppc64 211 176 -35 -16% cmd/link/internal/s390x 210 175 -35 -16% cmd/link/internal/x86 203 170 -33 -15% cmd/trace 782 744 -38 -4% compress/lzw 402 383 -19 -4% crypto/aes 311 262 -49 -15% crypto/cipher 1138 959 -179 -15% crypto/des 315 288 -27 -8% crypto/elliptic 6063 5746 -317 -4% crypto/rc4 317 295 -22 -6% crypto/sha256 348 312 -36 -9% crypto/sha512 487 451 -36 -6% go/doc 3871 3649 -222 -5% go/internal/gccgoimporter 2063 1949 -114 -5% go/internal/gcimporter 3253 3096 -157 -4% math 4343 3572 -771 -17% math/cmplx 1580 1274 -306 -18% math/rand 982 926 -56 -5% net/internal/socktest 2159 2049 -110 -4% os/exec 7928 7492 -436 -4% os/signal 237 208 -29 -11% os/user 717 682 -35 -4% runtime/internal/atomic 728 693 -35 -4% runtime/internal/sys 2287 2107 -180 -7% sync 1306 1214 -92 -6% all packages 1509255 1465507 -43748 -2% Change-Id: I98a11521b552166b7f47f2039a29f106748bf5d4 Reviewed-on: https://go-review.googlesource.com/22580 Reviewed-by: Alan Donovan <[email protected]>
1 parent f04eb35 commit 6c11e27

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/cmd/compile/internal/gc/bexport.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
// Binary package export.
66
// (see fmt.go, parser.go as "documentation" for how to use/setup data structures)
7-
//
8-
// Use "-newexport" flag to enable.
97

108
/*
119
Export data encoding:
@@ -508,25 +506,52 @@ func (p *exporter) pos(n *Node) {
508506
return
509507
}
510508

511-
var file string
512-
var line int
513-
if n != nil {
514-
file, line = Ctxt.LineHist.AbsFileLine(int(n.Lineno))
515-
}
516-
517-
if file == p.prevFile && line != p.prevLine {
518-
// common case: write delta-encoded line number
519-
p.int(line - p.prevLine) // != 0
509+
file, line := fileLine(n)
510+
if file == p.prevFile {
511+
// common case: write line delta
512+
// delta == 0 means different file or no line change
513+
delta := line - p.prevLine
514+
p.int(delta)
515+
if delta == 0 {
516+
p.int(-1) // -1 means no file change
517+
}
520518
} else {
521-
// uncommon case: filename changed, or line didn't change
519+
// different file
522520
p.int(0)
523-
p.string(file)
524-
p.int(line)
521+
// Encode filename as length of common prefix with previous
522+
// filename, followed by (possibly empty) suffix. Filenames
523+
// frequently share path prefixes, so this can save a lot
524+
// of space and make export data size less dependent on file
525+
// path length. The suffix is unlikely to be empty because
526+
// file names tend to end in ".go".
527+
n := commonPrefixLen(p.prevFile, file)
528+
p.int(n) // n >= 0
529+
p.string(file[n:]) // write suffix only
525530
p.prevFile = file
531+
p.int(line)
526532
}
527533
p.prevLine = line
528534
}
529535

536+
func fileLine(n *Node) (file string, line int) {
537+
if n != nil {
538+
file, line = Ctxt.LineHist.AbsFileLine(int(n.Lineno))
539+
}
540+
return
541+
}
542+
543+
func commonPrefixLen(a, b string) int {
544+
if len(a) > len(b) {
545+
a, b = b, a
546+
}
547+
// len(a) <= len(b)
548+
i := 0
549+
for i < len(a) && a[i] == b[i] {
550+
i++
551+
}
552+
return i
553+
}
554+
530555
func isInlineable(n *Node) bool {
531556
if exportInlined && n != nil && n.Func != nil && n.Func.Inl.Len() != 0 {
532557
// when lazily typechecking inlined bodies, some re-exported ones may not have been typechecked yet.

src/cmd/compile/internal/gc/bimport.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,14 @@ func (p *importer) pos() {
297297

298298
file := p.prevFile
299299
line := p.prevLine
300-
301300
if delta := p.int(); delta != 0 {
301+
// line changed
302302
line += delta
303-
} else {
304-
file = p.string()
305-
line = p.int()
303+
} else if n := p.int(); n >= 0 {
304+
// file changed
305+
file = p.prevFile[:n] + p.string()
306306
p.prevFile = file
307+
line = p.int()
307308
}
308309
p.prevLine = line
309310

src/go/internal/gcimporter/bimport.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,14 @@ func (p *importer) pos() {
204204

205205
file := p.prevFile
206206
line := p.prevLine
207-
208207
if delta := p.int(); delta != 0 {
208+
// line changed
209209
line += delta
210-
} else {
211-
file = p.string()
212-
line = p.int()
210+
} else if n := p.int(); n >= 0 {
211+
// file changed
212+
file = p.prevFile[:n] + p.string()
213213
p.prevFile = file
214+
line = p.int()
214215
}
215216
p.prevLine = line
216217

0 commit comments

Comments
 (0)