nlohmann / nlohmann/json

json destructor quite slow

Open
#3,583 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind: bug
Dominant language
C++
Stars
50.6k
Forks
7.5k
Avg merge
4d 17h
Merged PRs (30d)
58

Description

Description

I was running patch over a ~100Mb repodata file and noticed that it was slow due to copying and destroying the entire object multiple times.

I am quite curious if the performance of destroy() could be improved? On my computer, it takes roughly 1 second to parse the file and create the json object, but also 0.5 seconds to destroy the json object.

Reproduction steps

Download a large JSON file, such as https://conda.anaconda.org/conda-forge/linux-64/repodata.json (curl --compressed https://conda.anaconda.org/conda-forge/linux-64/repodata.json)

and run the following code:

#include <iostream>
#include <chrono>
#include <nlohmann/json.hpp>

int
main()
{
    std::ifstream rdata("repodata.json");
    std::unique_ptr<nlohmann::json> j = std::make_unique<nlohmann::json>();
    
    {
        auto t0 = std::chrono::high_resolution_clock::now();
        rdata >> (*j);
        auto t1 = std::chrono::high_resolution_clock::now();
        std::cout << "took " << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() <<" ms." << std::endl;
    }

    {
        auto t0 = std::chrono::high_resolution_clock::now();
        j.reset();
        auto t1 = std::chrono::high_resolution_clock::now();
        std::cout << "took " << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() <<" ms." << std::endl;
    }
}
Expected vs. actual results

destruction should have minimal runtime cost

Minimal code example

No response

Error messages

No response

Compiler and operating system

clang 12, macOS

Library version

3.10.5

Validation

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the destructor behavior of nlohmann::json in the header-only nlohmann/json.hpp implementation and reproduce the timing using the large repodata.json example from the issue. Profile destruction and compare the result against parsing, then run the project’s unit tests; done means a validated reduction in destruction time without regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.