> 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
> }

What roger peppe said.

- Performing an unchecked type assertion is incorrect in the general case.

Example: https://play.golang.org/p/YmZyw0MnOK8

- Unsafe seems to be used for no good reason.

- Reflect ditto.

This code https://play.golang.org/p/4sd1yayjoNY prints

        50000000                35.3 ns/op

While this one https://play.golang.org/p/tSfWbrqv4PN prints

        100000000               18.7 ns/op

Using Intel® Xeon(R) CPU E5-1650 v2 @ 3.50GHz × 12.

PS: The last two links point to code that cannot run in the playground.

-- 

-j

-- 
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