Subclass json_object so primitives shrink from 168 to 24 bytes

The previous "every member in a struct" layout cost 168 bytes per
json_object, even for JSON_NULL / JSON_TRUE / JSON_FALSE nodes that
have no payload. Splitting json_object into a small base class plus
json_number / json_string / json_array / json_hash subclasses brings
each instance down to just the size of its actual contents:

  json_object (base, TRUE / FALSE / NULL)   24 bytes
  json_number                                48 bytes
  json_string                                48 bytes
  json_array  (empty)                        48 bytes
  json_hash   (empty)                        72 bytes

Other size wins along the way:

* Drop enable_shared_from_this<json_object> (its embedded weak_ptr
  was 16 bytes per node). json_pull now keeps an explicit
  container_stack and the parser no longer needs to resurrect a
  shared_ptr from a raw `parent` walk.
* Remove the unused `refcon` slot from the string variant.
* No virtual destructor: shared_ptr keeps the deleter from the
  original std::make_shared<json_xxx> call, so destroying a
  shared_ptr<json_object> still runs the right subclass dtor.

The base class exposes type-tagged accessors (o->string(),
o->number(), o->array(), o->keys(), o->values(), o->large_signed(),
o->large_unsigned()) that assert the type matches and downcast to
the appropriate subclass storage. All call sites were swept from
the old `o->value.X.Y` field paths to these accessors. A raw-pointer
overload of json_hash_get() replaces the few external uses of
shared_from_this() that survived in geojson-loop.cpp.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Erica Fischer
2026-05-30 17:49:08 -07:00
co-authored by Cursor
parent 3da03c6075
commit f366b2c4aa
14 changed files with 424 additions and 291 deletions
+7 -7
View File
@@ -74,7 +74,7 @@ void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
int i;
int is_geometry = 0;
for (i = 0; i < GEOM_TYPES; i++) {
if (type->value.string.string == geometry_names[i]) {
if (type->string() == geometry_names[i]) {
is_geometry = 1;
break;
}
@@ -84,14 +84,14 @@ void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
if (j->parent != nullptr) {
if (j->parent->type == JSON_ARRAY && j->parent->parent != nullptr) {
if (j->parent->parent->type == JSON_HASH) {
json_object_ptr geometries = json_hash_get(j->parent->parent->shared_from_this(), "geometries");
json_object_ptr geometries = json_hash_get(j->parent->parent, "geometries");
if (geometries != nullptr) {
// Parent of Parent must be a GeometryCollection
is_geometry = 0;
}
}
} else if (j->parent->type == JSON_HASH) {
json_object_ptr geometry = json_hash_get(j->parent->shared_from_this(), "geometry");
json_object_ptr geometry = json_hash_get(j->parent, "geometry");
if (geometry != nullptr) {
// Parent must be a Feature
is_geometry = 0;
@@ -104,7 +104,7 @@ void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
json_object *jo = j.get();
while (jo != nullptr) {
if (jo->parent != nullptr && jo->parent->type == JSON_HASH) {
if (json_hash_get(jo->parent->shared_from_this(), "properties").get() == jo) {
if (json_hash_get(jo->parent, "properties").get() == jo) {
// Ancestor is the value corresponding to a properties key
is_geometry = 0;
break;
@@ -126,8 +126,8 @@ void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
}
}
if (type->value.string.string != "Feature") {
if (type->value.string.string == "FeatureCollection") {
if (type->string() != "Feature") {
if (type->string() == "FeatureCollection") {
jfa->check_crs(j);
json_free(j);
}
@@ -161,7 +161,7 @@ void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
json_object *jo = j.get();
while (jo != nullptr) {
if (jo->parent != nullptr && jo->parent->type == JSON_HASH) {
if (json_hash_get(jo->parent->shared_from_this(), "properties").get() == jo) {
if (json_hash_get(jo->parent, "properties").get() == jo) {
// Ancestor is the value corresponding to a properties key
is_feature = false;
break;