Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(686)

Delta Between Two Patch Sets: src/cmd/link/dead.go

Issue 51470043: code review 51470043: cmd/link: implement dead code removal (Closed)
Left Patch Set: diff -r 21fb28e644a1 https://code.google.com/p/go/ Created 11 years, 2 months ago
Right Patch Set: diff -r 7276108715ff https://code.google.com/p/go/ Created 11 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/cmd/link/dead_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2014 The Go Authors. All rights reserved. 1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Removal of dead code and data. 5 // Removal of dead code and data.
6 6
7 package main 7 package main
8 8
9 import "debug/goobj" 9 import "debug/goobj"
10 10
11 // dead removes unreachable code and data from the program. 11 // dead removes unreachable code and data from the program.
12 // It is basically a mark-sweep garbage collection: traverse all the 12 // It is basically a mark-sweep garbage collection: traverse all the
13 // symbols reachable from the entry (startSymID) and then delete 13 // symbols reachable from the entry (startSymID) and then delete
14 // the rest. 14 // the rest.
15 func (p *Prog) dead() { 15 func (p *Prog) dead() {
16 p.Dead = make(map[goobj.SymID]bool)
16 reachable := make(map[goobj.SymID]bool) 17 reachable := make(map[goobj.SymID]bool)
17 p.walkDead(p.startSym, reachable) 18 p.walkDead(p.startSym, reachable)
18 19
19 for sym := range p.Syms { 20 for sym := range p.Syms {
20 if !reachable[sym] { 21 if !reachable[sym] {
21 delete(p.Syms, sym) 22 delete(p.Syms, sym)
23 p.Dead[sym] = true
22 } 24 }
23 } 25 }
24 26
25 for sym := range p.Missing { 27 for sym := range p.Missing {
26 if !reachable[sym] { 28 if !reachable[sym] {
27 delete(p.Missing, sym) 29 delete(p.Missing, sym)
30 p.Dead[sym] = true
28 } 31 }
29 } 32 }
30 33
31 p.SymOrder = removeDead(p.SymOrder, reachable) 34 p.SymOrder = removeDead(p.SymOrder, reachable)
32 35
33 for _, pkg := range p.Packages { 36 for _, pkg := range p.Packages {
34 pkg.Syms = removeDead(pkg.Syms, reachable) 37 pkg.Syms = removeDead(pkg.Syms, reachable)
35 } 38 }
36 } 39 }
37 40
(...skipping 24 matching lines...) Expand all
62 // returning a shortened slice using the same underlying array. 65 // returning a shortened slice using the same underlying array.
63 func removeDead(syms []*Sym, reachable map[goobj.SymID]bool) []*Sym { 66 func removeDead(syms []*Sym, reachable map[goobj.SymID]bool) []*Sym {
64 keep := syms[:0] 67 keep := syms[:0]
65 for _, sym := range syms { 68 for _, sym := range syms {
66 if reachable[sym.SymID] { 69 if reachable[sym.SymID] {
67 keep = append(keep, sym) 70 keep = append(keep, sym)
68 } 71 }
69 } 72 }
70 return keep 73 return keep
71 } 74 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b