How slow is Go's reflection effectively?

10,041 views
Skip to first unread message

tomwilde

unread,
Jul 17, 2012, 11:07:31 PM7/17/12
to golan...@googlegroups.com
In general we assume reflection is very slow, because for most languages this is painfully true.

But Go's reflection seems to be actually pretty lightweight. In my application I'm using the encoding/json package, as well as Gustavo Niemeyer's mgo MongoDB driver, both of which use reflection.

So, how bad does reflection impact a Go application's performance in general terms?

Jesse McNelis

unread,
Jul 17, 2012, 11:29:41 PM7/17/12
to tomwilde, golan...@googlegroups.com
'Slow' is relative to the problem being solved and the time-frame
given to solve it.
Reflection in Go is slow because it needs to do quite a few allocations.
Doing lots of allocations in a loop that is run many millions of times
would be slow if the loop needs to be complete in a few seconds. But
irrelevant if you have minutes.

Reflection can often lead to shorter and more reusable code which will
suit most people's needs until they profile their
solution and see if the reflection is actually going to be a
bottleneck. You won't know the impact of reflection on your
performance until you profile.


--
=====================
http://jessta.id.au

tomwilde

unread,
Jul 17, 2012, 11:32:52 PM7/17/12
to golan...@googlegroups.com, tomwilde
Let me ask this in another way...

By using reflection in my DB queries and some other interaction that my web application server does - am I in the end reducing the amount of requests that the web server can serve per second or is the impact so small it won't even be noticeable?

I will do some profiling by using file-based data sources instead of databases.

Am Mittwoch, 18. Juli 2012 05:29:41 UTC+2 schrieb Jesse McNelis:

David Symonds

unread,
Jul 17, 2012, 11:37:55 PM7/17/12
to tomwilde, golan...@googlegroups.com
On Wed, Jul 18, 2012 at 1:32 PM, tomwilde <sedevel...@gmail.com> wrote:

> By using reflection in my DB queries and some other interaction that my web
> application server does - am I in the end reducing the amount of requests
> that the web server can serve per second or is the impact so small it won't
> even be noticeable?

For that situation, I would be very surprised to see reflection having
any measurable impact.


Dave.

Dave Cheney

unread,
Jul 17, 2012, 11:59:48 PM7/17/12
to tomwilde, golan...@googlegroups.com, tomwilde
Compared to a disk seek or network transfer, the cost of reflection will be negligible. 
Reply all
Reply to author
Forward
0 new messages