vendredi 13 novembre 2015

What is the best way to work around the overhead of reflection?

I have a program that reads 173 (c) data structures from a memory map that need to be converted to Go. The value of the type is stored as a string in those structures. The structures are received 60 times per second.

I'm now using reflection (FieldByName) to get a reference to the go struct field and set the received data. But because there a many fields (173) and they get updated a lot this ads a lot of overhead and that function call is the slowest part of my program (jay go prof!).

What is the best way to make this faster? As far as I can see I have three options:

  1. cache the reflect.Value's in a map and make a function that receives data, use a template struct tied to the cache map, fill that struct and return a copy of that template-struct
  2. go generate all the setters and a giant switch statement for each received field
  3. Just code all the different setters

What would be the "best" option? Is there an option I'm overlooking?





Aucun commentaire:

Enregistrer un commentaire