-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: reflect: treat reflect.Type as a pointer where possible for generics & maps #52267
Comments
It is possible to wrap a reflect.Type and override methods. |
Interesting wrinkle in my hopes, I think wrappers may make any intrinsic "this is The other angle I was thinking to propose: is it worth considering adding something like |
My personal preference here is to adopt #51338 and change |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
#51338 is great and largely covers it, albeit @Merovius brought up the same wrinkle. I think #51338 is a better general approach, whereas this issue would be an optimization regardless of what's chosen. If the wrinkle cannot be solved, is it worth it to consider an alternative proposal of extracting the |
It sounds like we should decline this in favor of #51338. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
reflect.Type
cannot be used in anycomparable
generic code. As well, usingreflect.Type
as a key in maps compiles toifaceeq
when it could be compiled to pointer / number comparisons.Because
reflect.Type
has two internal methods, there cannot be any implementation outside of the reflect package. Inside reflect,Type
is always*rtype
which corresponds to a runtime type.Can we solve #51179 by specially compiling
reflect.Type
to its*rtype
representation, which makes it comparable? As well, can this force the discussion on #32303 by committing to "No, custom type implementations are not supported"?Lastly, storing pointers in maps rather than reflect.Type can help speed up essentially any serializer that caches some computation of
reflect.Type => {reflect.StructField}
. Notably, there is amap[reflect.Type]something
in encoding/binary, encoding/gob, encoding/json, and encoding/xml. I noticed this myself when writing a serializer for avro (I know others already exist). For the small toy struct that is commonly used in avro tests, serialization speeds up from 350ns to 300ns. In benchmarks, looking upreflect.Type
is ~12ns, vs. ~2ns forunsafe.Pointer
-- it's a small increase, but it does add up, especially when using arrays.The text was updated successfully, but these errors were encountered: