Commit 3c783b99 authored by Steven Allen's avatar Steven Allen

Optimize readByte

Type asserting to a concrete type is ~10x faster than type asserting to an
interface. This change has a significant performance impact in my test. readByte
used to account for 10.8% of the time, now it accounts for 3.4%.
parent 021bda15
package typegen
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
......@@ -202,8 +204,19 @@ func (d *Deferred) UnmarshalCBOR(br io.Reader) error {
}
func readByte(r io.Reader) (byte, error) {
if br, ok := r.(io.ByteReader); ok {
return br.ReadByte()
// try to cast to a concrete type, it's much faster than casting to an
// interface.
switch r := r.(type) {
case *bytes.Buffer:
return r.ReadByte()
case *bytes.Reader:
return r.ReadByte()
case *bufio.Reader:
return r.ReadByte()
case *peeker:
return r.ReadByte()
case io.ByteReader:
return r.ReadByte()
}
var buf [1]byte
_, err := io.ReadFull(r, buf[:1])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment