You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reflect.TypeOf(a) == reflect.TypeOf(b) should optimize to a simple comparison of the type pointers stored in the interface value (or in the itab if a and b aren't any)
Currently, the code compiles more naively: it's constructing new interface values with *rtypes stored in them, to then call ifaceeq.
@josharian offered the following analysis on Slack:
poking under the hood a bit, it looks like there are two stumbling blocks to that compiling down nicely:
reflect.TypeOf returns nil when passed nil. that introduces a branch into the inlined code that the compiler can't reason its way around.
there's a missing optimization. calls to runtime.ifaceeq can be converted to a single pointer equality if the compiler knows that the type is (a) non-nil and (b) a direct iface. which (if you remove point one above), the compiler should be able to know, at least as of ssa time.
note that it's not just enough to know locally that at the callsite that the argument to typeof is non-nil. it has to make it through a few layers:
the type punning in particular in TypeOf prevents the compiler from doing some optimizations. (i think maybe the optimizations there could be strengthened, but this is danger zone stuff.)
for reference, here is [i]faceeq:
you can see how for known non-nil types containing pointers (which are "direct"), this whole thing collapses to a pointer equality check.
The text was updated successfully, but these errors were encountered: