You don't need to use reflect here. You may well find that something like
this is faster:

        func (b *Binlog) writeArgumentToOutput(writer writer, arg
interface{}) error {
                var i uint64
                switch arg := arg.(type) {
                case uintptr:
                        i = uint64(arg)
                case int:
                        i = uint64(arg)
                case int8:
                        i = uint64(arg)
                case int16:
                        i = uint64(arg)
                case int32:
                        i = uint64(arg)
                case int64:
                        i = uint64(arg)
                case uint8:
                        i = uint64(arg)
                case uint16:
                        i = uint64(arg)
                case uint32:
                        i = uint64(arg)
                case uint64:
                        i = arg
                default:
                        return fmt.Errorf("Unsupported type: %T\n",
reflect.TypeOf(arg))
                }
                return writer.write(b.ioWriter, &i)
        }

There are a couple of issues I'd point out with the original code:
- just because a value has a given Kind (e.g. reflect.Int16) does not mean
it can be converted to that type, because it may be some other named type
with an int16 underlying type. A better (and shorter, but probably no
faster) way to do it would be something like:
https://play.golang.org/p/jhpbaaDoY57
- it seems odd that you're passing an unsafe.Pointer to the write method.
How is that method supposed to know how many bytes to write?



On Sun, 25 Nov 2018 at 16:09, <lary...@gmail.com> wrote:

> The code below consumes ~40% of the total execution time. According to
> the profiler i := uint64(arg.(uint32)) is a major contributor
>
> // Cast the integer argument to uint64 and call a "writer"
> // The "writer" knows how many bytes to add to the binary stream
> // Type casts from interface{} to integer consume 40% of the overall
> // time. Can I do better? What is interface{} in Golang?
> func (b *Binlog) writeArgumentToOutput(writer writer, arg interface{},
> argKind reflect.Kind) error {
>     // unsafe pointer to the data depends on the data type
>     var err error
>     switch argKind {
>     case reflect.Int8:
>         i := uint64(arg.(int8))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Int16:
>         i := uint64(arg.(int16))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Int32:
>         i := uint64(arg.(int32))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Int64:
>         i := uint64(arg.(int64))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Uint8:
>         i := uint64(arg.(uint8))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Uint16:
>         i := uint64(arg.(uint16))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Uint32:
>         i := uint64(arg.(uint32))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Uint64:
>         i := uint64(arg.(uint64))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Int:
>         i := uint64(arg.(int))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     case reflect.Uint:
>         i := uint64(arg.(uint))
>         err = writer.write(b.ioWriter, unsafe.Pointer(&i))
>     default:
>         return fmt.Errorf("Unsupported type: %T\n", reflect.TypeOf(arg))
>     }
>     return err
> }
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to