Skip to content

Commit

Permalink
optimize: avoid string cast
Browse files Browse the repository at this point in the history
43.9 ns/op
  • Loading branch information
felixge committed Jan 11, 2015
1 parent a48d892 commit 859ebc4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions xxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func XXD(r io.Reader, w io.Writer) error {

// Character values
b := buf[:n]
for _, c := range b {
for i, c := range b {
if c > 0x1f && c < 0x7f {
io.WriteString(w, string(c))
w.Write(buf[i : i+1])
} else {
w.Write(dot)
}
Expand Down

3 comments on commit 859ebc4

@matt2909
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you just w.Write(c) on line 82? and avoid using the index at all? Wouldn't that remove a bounds check on buf access by index?

@matt2909
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me, you need a slice and not a byte to a writer interface.

@felixge
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matt2909 yup.

Please # to comment.