From 6357d3b3f3522cb2e39d2bae8cf41a3ae31c1a34 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 2 May 2020 17:31:53 -0500 Subject: [PATCH] Avoid extra type assertion on native type Scan path Before: BenchmarkConnInfoScanInt4IntoBinaryDecoder-16 89744814 12.5 ns/op 0 B/op 0 allocs/op BenchmarkConnInfoScanInt4IntoGoInt32-16 27688370 41.1 ns/op 0 B/op 0 allocs/op After: BenchmarkConnInfoScanInt4IntoBinaryDecoder-16 88181061 12.4 ns/op 0 B/op 0 allocs/op BenchmarkConnInfoScanInt4IntoGoInt32-16 30402768 36.8 ns/op 0 B/op 0 allocs/op --- pgtype.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pgtype.go b/pgtype.go index 914e02d2..f7dc1379 100644 --- a/pgtype.go +++ b/pgtype.go @@ -337,12 +337,17 @@ func (ci *ConnInfo) DeepCopy() *ConnInfo { } func (ci *ConnInfo) Scan(oid uint32, formatCode int16, buf []byte, dest interface{}) error { - if dest, ok := dest.(BinaryDecoder); ok && formatCode == BinaryFormatCode { - return dest.DecodeBinary(ci, buf) - } - - if dest, ok := dest.(TextDecoder); ok && formatCode == TextFormatCode { - return dest.DecodeText(ci, buf) + switch formatCode { + case BinaryFormatCode: + if dest, ok := dest.(BinaryDecoder); ok { + return dest.DecodeBinary(ci, buf) + } + case TextFormatCode: + if dest, ok := dest.(TextDecoder); ok { + return dest.DecodeText(ci, buf) + } + default: + return errors.Errorf("unknown format code: %v", formatCode) } if dt, ok := ci.DataTypeForOID(oid); ok { @@ -366,8 +371,6 @@ func (ci *ConnInfo) Scan(oid uint32, formatCode int16, buf []byte, dest interfac } else { return errors.Errorf("%T is not a pgtype.BinaryDecoder", value) } - default: - return errors.Errorf("unknown format code: %v", formatCode) } if scanner, ok := dest.(sql.Scanner); ok {