mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 16:35:40 +02:00
Store hash key/value pairs in one ordered vector
Replace the parallel std::vector<json_object_ptr> keys / values on
json_hash with a single std::vector<json_entry>, where json_entry is
a small {key, value} aggregate. This still preserves insertion order
(the property the parallel vectors were providing) but removes the
"keep two vectors in lockstep" pattern, and call sites can now use
range-for with structured bindings:
for (auto &[k, v] : o->entries()) { ... }
Side effects:
* sizeof(json_hash) drops from 72 to 48 bytes (one fewer vector
header), matching json_array.
* The keys() and values() accessors on json_object are replaced by a
single entries() accessor returning std::vector<json_entry>&.
* All call sites were swept from the old paired-index pattern
(`o->keys()[i]` / `o->values()[i]`) to entry-based access. Where the
original pattern relied on `nprop = 0` to short-circuit iteration on
a null or non-hash `properties`, the rewrite now guards the loop
explicitly with `if (o->type == JSON_HASH)` so that calling
entries() doesn't trip the asserting downcast.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+17
-18
@@ -34,6 +34,16 @@ struct json_pull;
|
||||
typedef std::shared_ptr<json_object> json_object_ptr;
|
||||
typedef std::shared_ptr<json_pull> json_pull_ptr;
|
||||
|
||||
// A single key/value pair inside a JSON_HASH. The pairs are stored in
|
||||
// insertion order in a single std::vector<json_entry> on json_hash, so
|
||||
// callers can range-for over `o->entries()` with structured bindings
|
||||
// (`for (auto &[k, v] : o->entries()) ...`) while still preserving the
|
||||
// order keys appeared in the source document.
|
||||
struct json_entry {
|
||||
json_object_ptr key;
|
||||
json_object_ptr value;
|
||||
};
|
||||
|
||||
// json_object is a small base type that just records the JSON type and
|
||||
// the back-pointers to its parent and parser. The actual value payload
|
||||
// lives in a type-specific subclass (json_number, json_string, json_array,
|
||||
@@ -83,10 +93,8 @@ struct json_object {
|
||||
inline std::vector<json_object_ptr> &array();
|
||||
inline const std::vector<json_object_ptr> &array() const;
|
||||
|
||||
inline std::vector<json_object_ptr> &keys();
|
||||
inline const std::vector<json_object_ptr> &keys() const;
|
||||
inline std::vector<json_object_ptr> &values();
|
||||
inline const std::vector<json_object_ptr> &values() const;
|
||||
inline std::vector<json_entry> &entries();
|
||||
inline const std::vector<json_entry> &entries() const;
|
||||
};
|
||||
|
||||
struct json_number : json_object {
|
||||
@@ -113,8 +121,7 @@ struct json_array : json_object {
|
||||
};
|
||||
|
||||
struct json_hash : json_object {
|
||||
std::vector<json_object_ptr> keys_value;
|
||||
std::vector<json_object_ptr> values_value;
|
||||
std::vector<json_entry> entries_value;
|
||||
|
||||
json_hash() : json_object(JSON_HASH) {}
|
||||
json_hash(json_object *p, json_pull *pl) : json_object(JSON_HASH, p, pl) {}
|
||||
@@ -163,21 +170,13 @@ inline const std::vector<json_object_ptr> &json_object::array() const {
|
||||
return static_cast<const json_array *>(this)->array_value;
|
||||
}
|
||||
|
||||
inline std::vector<json_object_ptr> &json_object::keys() {
|
||||
inline std::vector<json_entry> &json_object::entries() {
|
||||
assert(type == JSON_HASH);
|
||||
return static_cast<json_hash *>(this)->keys_value;
|
||||
return static_cast<json_hash *>(this)->entries_value;
|
||||
}
|
||||
inline const std::vector<json_object_ptr> &json_object::keys() const {
|
||||
inline const std::vector<json_entry> &json_object::entries() const {
|
||||
assert(type == JSON_HASH);
|
||||
return static_cast<const json_hash *>(this)->keys_value;
|
||||
}
|
||||
inline std::vector<json_object_ptr> &json_object::values() {
|
||||
assert(type == JSON_HASH);
|
||||
return static_cast<json_hash *>(this)->values_value;
|
||||
}
|
||||
inline const std::vector<json_object_ptr> &json_object::values() const {
|
||||
assert(type == JSON_HASH);
|
||||
return static_cast<const json_hash *>(this)->values_value;
|
||||
return static_cast<const json_hash *>(this)->entries_value;
|
||||
}
|
||||
|
||||
struct json_pull {
|
||||
|
||||
Reference in New Issue
Block a user