mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 08:25:40 +02:00
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:
@@ -2885,9 +2885,9 @@ void set_attribute_value(const char *arg) {
|
||||
exit(EXIT_JSON);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < o->value.object.keys.size(); i++) {
|
||||
json_object_ptr k = o->value.object.keys[i];
|
||||
json_object_ptr v = o->value.object.values[i];
|
||||
for (size_t i = 0; i < o->keys().size(); i++) {
|
||||
json_object_ptr k = o->keys()[i];
|
||||
json_object_ptr v = o->values()[i];
|
||||
|
||||
if (k->type != JSON_STRING) {
|
||||
fprintf(stderr, "%s: --set-attribute %s: key %zu not a string\n", *av, arg, i);
|
||||
@@ -2895,7 +2895,7 @@ void set_attribute_value(const char *arg) {
|
||||
}
|
||||
|
||||
serial_val val = stringify_value(v, "json", 1, o);
|
||||
set_attributes.emplace(k->value.string.string, val);
|
||||
set_attributes.emplace(k->string(), val);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -2941,21 +2941,21 @@ void parse_json_source(const char *arg, struct source &src) {
|
||||
exit(EXIT_JSON);
|
||||
}
|
||||
|
||||
src.file = fname->value.string.string;
|
||||
src.file = fname->string();
|
||||
|
||||
json_object_ptr layer = json_hash_get(o, "layer");
|
||||
if (layer != nullptr && layer->type == JSON_STRING) {
|
||||
src.layer = layer->value.string.string;
|
||||
src.layer = layer->string();
|
||||
}
|
||||
|
||||
json_object_ptr description = json_hash_get(o, "description");
|
||||
if (description != nullptr && description->type == JSON_STRING) {
|
||||
src.description = description->value.string.string;
|
||||
src.description = description->string();
|
||||
}
|
||||
|
||||
json_object_ptr format = json_hash_get(o, "format");
|
||||
if (format != nullptr && format->type == JSON_STRING) {
|
||||
src.format = format->value.string.string;
|
||||
src.format = format->string();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user