Commit 99484e9
optimization: comparison: when DataType is static, skip reflection (#542)
### Rationale for this change
Reflection is slow. When working with primitive data types - e.g.
PrimitiveType.Float64 - the DataType value is a constant consisting of
the (constant) type indicator, and the constant pointer to the only
instance of the Float64 type. For such cases, we can do a fast-path for
TypeEqual and detect the interface equality without the current path of
switching on the type of DataType, and using reflect.DeepEqual.
reflect.DeepEqual makes several allocations and is not particularly
fast. This patch cuts out 50-75% of the runtime of `schema.Equals` in
our real-world scenario in my testing.
### What changes are included in this PR?
A branch is added to the TypeEqual switch, which returns early when the
two types compare equal through shallow comparison. In such a case,
deeper comparison must necessarily show equality, as every property is
definitionally identical. Detecting this early can make e.g. schema
validations significantly faster.
### Are these changes tested?
Yes, though only with our internal schemas. I noticed arrow showing up
heavily in our profiles and started hacking on it to make it go away :)
### Are there any user-facing changes?
It runs faster? That's technically user-facing ;)
Co-authored-by: Noam Preil <noam@pixelhero.dev>1 parent a489ef0 commit 99484e9
1 file changed
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | | - | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
0 commit comments