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
[Variant] Use BTreeMap for VariantBuilder.dict and ObjectBuilder.fields to maintain invariants upon entry writes (#7720)
# Which issue does this PR close?
- It doesn't directly close the issue, but it's related to
#7698
# Rationale for this change
This commit changes the `dict` field in `VariantBuilder` + the `fields`
field in `ObjectBuilder` to be `BTreeMap`s, and checks for existing
field names in a object before appending a new field.
These collections are often used in places where having an already
sorted structure would be more performant. Inside of
`ObjectBuilder::finish()`, we sort the fields by `field_name` and we can
use the fact that `VariantBuilder`'s `dict` maintains a sorted mapping
to `field_id` by `field_name`.
To check whether an existing field name exists in a object, it is simply
two lookups: 1) to find the `field_name: &str`'s unique `field_name_id`,
and 2) check if the `ObjectBuilder` `fields` already has a key with that
`field_name_id`.
We make `ObjectBuilder` `fields` a `BTreeMap` sorted by `field_id`.
Since `field_id`s correlate to insertion order, we now have some notion
of which fields were inserted first. This also improves the time to look
up the max field id, as it changes the linear scan over the entire
`fields` collection to a logarithmic call using `fields.keys().last()`.
0 commit comments