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:
Erica Fischer
2026-05-30 17:49:13 -07:00
co-authored by Cursor
parent f366b2c4aa
commit 4d9a48c3d4
11 changed files with 122 additions and 137 deletions
+25 -36
View File
@@ -134,7 +134,7 @@ static json_object_ptr add_object(json_pull *j, json_type type) {
}
} else if (c->type == JSON_HASH) {
if (c->expect == JSON_VALUE) {
c->values().back() = o;
c->entries().back().value = o;
c->expect = JSON_COMMA;
} else if (c->expect == JSON_KEY) {
if (type != JSON_STRING) {
@@ -142,8 +142,7 @@ static json_object_ptr add_object(json_pull *j, json_type type) {
return nullptr;
}
c->keys().push_back(o);
c->values().push_back(nullptr);
c->entries().push_back({o, nullptr});
c->expect = JSON_COLON;
} else {
j->error = "Expected a comma or colon";
@@ -164,14 +163,9 @@ json_object_ptr json_hash_get(json_object *o, const char *s) {
return nullptr;
}
const auto &keys = o->keys();
const auto &vals = o->values();
for (size_t i = 0; i < keys.size(); i++) {
const auto &key = keys[i];
if (key != nullptr && key->type == JSON_STRING) {
if (key->string() == s) {
return vals[i];
}
for (const auto &e : o->entries()) {
if (e.key != nullptr && e.key->type == JSON_STRING && e.key->string() == s) {
return e.value;
}
}
@@ -299,7 +293,7 @@ again:
}
if (cc->expect != JSON_COMMA) {
if (!(cc->expect == JSON_KEY && cc->keys().size() == 0)) {
if (!(cc->expect == JSON_KEY && cc->entries().size() == 0)) {
j->error = "Found } without final element";
return nullptr;
}
@@ -676,11 +670,9 @@ static void clear_back_pointers(json_object *o) {
}
if (o->type == JSON_HASH) {
const auto &keys = o->keys();
const auto &vals = o->values();
for (size_t i = 0; i < keys.size(); i++) {
clear_back_pointers(keys[i].get());
clear_back_pointers(vals[i].get());
for (const auto &e : o->entries()) {
clear_back_pointers(e.key.get());
clear_back_pointers(e.value.get());
}
} else if (o->type == JSON_ARRAY) {
const auto &arr = o->array();
@@ -713,28 +705,26 @@ void json_disconnect(json_object_ptr o) {
}
}
} else if (parent->type == JSON_HASH) {
auto &keys = parent->keys();
auto &vals = parent->values();
auto &entries = parent->entries();
for (size_t i = 0; i < keys.size(); i++) {
if (keys[i].get() == o.get()) {
for (size_t i = 0; i < entries.size(); i++) {
auto &e = entries[i];
if (e.key.get() == o.get()) {
// Leave a NULL placeholder in the key slot so the
// surrounding value isn't shifted; if the corresponding
// value is also detached the pair is removed below.
keys[i] = fabricate_object(parent->parser, parent, JSON_NULL);
e.key = fabricate_object(parent->parser, parent, JSON_NULL);
if (vals[i] != nullptr && vals[i]->type == JSON_NULL && keys[i]->type == JSON_NULL) {
keys.erase(keys.begin() + i);
vals.erase(vals.begin() + i);
if (e.value != nullptr && e.value->type == JSON_NULL && e.key->type == JSON_NULL) {
entries.erase(entries.begin() + i);
}
break;
}
if (vals[i].get() == o.get()) {
vals[i] = fabricate_object(parent->parser, parent, JSON_NULL);
if (e.value.get() == o.get()) {
e.value = fabricate_object(parent->parser, parent, JSON_NULL);
if (keys[i] != nullptr && keys[i]->type == JSON_NULL && vals[i]->type == JSON_NULL) {
keys.erase(keys.begin() + i);
vals.erase(vals.begin() + i);
if (e.key != nullptr && e.key->type == JSON_NULL && e.value->type == JSON_NULL) {
entries.erase(entries.begin() + i);
}
break;
}
@@ -815,13 +805,12 @@ static void json_print(std::string &val, json_object *o) {
} else if (o->type == JSON_HASH) {
string_append_c(val, '{');
const auto &keys = o->keys();
const auto &vals = o->values();
for (size_t i = 0; i < keys.size(); i++) {
json_print(val, keys[i].get());
const auto &entries = o->entries();
for (size_t i = 0; i < entries.size(); i++) {
json_print(val, entries[i].key.get());
string_append_c(val, ':');
json_print(val, vals[i].get());
if (i + 1 < keys.size()) {
json_print(val, entries[i].value.get());
if (i + 1 < entries.size()) {
string_append_c(val, ',');
}
}
+17 -18
View File
@@ -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 {