mirror of
https://github.com/golang/go
synced 2024-11-22 02:04:40 -07:00
godoc: optimizations: don't call Write for 0-length data
- guard some calls to Write that frequently may have 0-length data - fix an invariant R=r CC=golang-dev https://golang.org/cl/4179041
This commit is contained in:
parent
fb9e37cd9b
commit
334f52ac49
@ -274,7 +274,7 @@ func relativePath(path string) string {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Tab conversion
|
// Tab conversion
|
||||||
|
|
||||||
var spaces = []byte(" ") // 16 spaces seems like a good number
|
var spaces = []byte(" ") // 32 spaces seems like a good number
|
||||||
|
|
||||||
const (
|
const (
|
||||||
indenting = iota
|
indenting = iota
|
||||||
@ -291,25 +291,31 @@ type tconv struct {
|
|||||||
|
|
||||||
func (p *tconv) writeIndent() (err os.Error) {
|
func (p *tconv) writeIndent() (err os.Error) {
|
||||||
i := p.indent
|
i := p.indent
|
||||||
for i > len(spaces) {
|
for i >= len(spaces) {
|
||||||
i -= len(spaces)
|
i -= len(spaces)
|
||||||
if _, err = p.output.Write(spaces); err != nil {
|
if _, err = p.output.Write(spaces); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// i < len(spaces)
|
||||||
|
if i > 0 {
|
||||||
_, err = p.output.Write(spaces[0:i])
|
_, err = p.output.Write(spaces[0:i])
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (p *tconv) Write(data []byte) (n int, err os.Error) {
|
func (p *tconv) Write(data []byte) (n int, err os.Error) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
pos := 0 // valid if p.state == collecting
|
pos := 0 // valid if p.state == collecting
|
||||||
var b byte
|
var b byte
|
||||||
for n, b = range data {
|
for n, b = range data {
|
||||||
switch p.state {
|
switch p.state {
|
||||||
case indenting:
|
case indenting:
|
||||||
switch b {
|
switch b {
|
||||||
case '\t', '\v':
|
case '\t':
|
||||||
p.indent += *tabwidth
|
p.indent += *tabwidth
|
||||||
case '\n':
|
case '\n':
|
||||||
p.indent = 0
|
p.indent = 0
|
||||||
@ -336,7 +342,7 @@ func (p *tconv) Write(data []byte) (n int, err os.Error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
n = len(data)
|
n = len(data)
|
||||||
if p.state == collecting {
|
if pos < n && p.state == collecting {
|
||||||
_, err = p.output.Write(data[pos:])
|
_, err = p.output.Write(data[pos:])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -352,6 +358,10 @@ func writeNode(w io.Writer, fset *token.FileSet, x interface{}) {
|
|||||||
// to ensure a good outcome in most browsers (there may still
|
// to ensure a good outcome in most browsers (there may still
|
||||||
// be tabs in comments and strings, but converting those into
|
// be tabs in comments and strings, but converting those into
|
||||||
// the right number of spaces is much harder)
|
// the right number of spaces is much harder)
|
||||||
|
//
|
||||||
|
// TODO(gri) rethink printer flags - perhaps tconv can be eliminated
|
||||||
|
// with an another printer mode (which is more efficiently
|
||||||
|
// implemented in the printer than here with another layer)
|
||||||
mode := printer.TabIndent | printer.UseSpaces
|
mode := printer.TabIndent | printer.UseSpaces
|
||||||
(&printer.Config{mode, *tabwidth}).Fprint(&tconv{output: w}, fset, x)
|
(&printer.Config{mode, *tabwidth}).Fprint(&tconv{output: w}, fset, x)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user