p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
667 stars 441 forks source link

Improve json library internals #4691

Open asl opened 4 months ago

asl commented 4 months ago

json library is a heavy cstring user for no particular reason. Essentially, jsons are huge string-keyed dictionaries. Let's see the common example of usage (this is from bfruntime.cpp):

isFieldSliceAnnot->emplace("value", "true");

emplace itself is essentially:

JsonObject *JsonObject::emplace(cstring label, IJson *value) {
...
    ordered_map<cstring, IJson *>::emplace(label, value);

Lots of bad things here:

As a results, jsons are quite expensive both memory- and performance- wise. Also, they pollute string cache for no particular reason. These strings are never released.

I think the proper solution would be to have a specialized StringMap container. Specialized towards string keys. Given that we'd always need to have string cache lookup currently, it will be uniformly better :)

fruffy commented 4 months ago

Is the invocation of these functions somewhere on the critical path for the compiler?

asl commented 4 months ago

Is the invocation of these functions somewhere on the critical path for the compiler?

Depends. But some backends essentially produce jsons only. I know also downstream cases where lots of jsons are emitted for control plane, etc.