Skip to content

proposal: reflect: treat reflect.Type as a pointer where possible for generics & maps #52267

Closed
@twmb

Description

@twmb

reflect.Type cannot be used in any comparable generic code. As well, using reflect.Type as a key in maps compiles to ifaceeq 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 a map[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 up reflect.Type is ~12ns, vs. ~2ns for unsafe.Pointer -- it's a small increase, but it does add up, especially when using arrays.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions