json icon indicating copy to clipboard operation
json copied to clipboard

Remove `<algorithm>` dependency from `from_json.hpp`

Open vittorioromeo opened this issue 8 months ago • 4 comments

As discussed on Reddit, here's a PR taking a first small step towards the improvement of compilation times. Avoiding dependencies on "heavy" standard library header such as <algorithm> can help.

Here is the ClangBuildAnalyzer compilation time report: https://gist.github.com/vittorioromeo/7a89a1e9af8cb89ee217d4e85be83270

vittorioromeo avatar Apr 11 '25 15:04 vittorioromeo

Coverage Status

coverage: 99.187% (+0.001%) from 99.186% when pulling 8bf6d54c82a232c1641fb1f136632bdf457ff9ce on vittorioromeo:develop into 7ddea2686f091ea77a3f731ab74c59535db9f684 on nlohmann:develop.

coveralls avatar Apr 11 '25 16:04 coveralls

@vittorioromeo Thanks for the PR! Could you please sign off your commit (see https://github.com/nlohmann/json/pull/4734/checks?check_run_id=40403948884).

nlohmann avatar Apr 12 '25 08:04 nlohmann

Rather than rewrite all of these algorithms which have been in production for many years, and potentially break the use on a user-defined type that specializes std::front_insert_iterator or std::insert_iterator, I wonder if it would be better to just create a local version of transform (and since it's just used for the side effects, not the return, it's really a copy_transform, so it could be renamed):

template<class InputIt, class OutputIt, class UnaryOp>
void copy_transform(InputIt first1, InputIt last1,
                   OutputIt d_first, UnaryOp unary_op)
{
    for (; first1 != last1; ++d_first, ++first1)
        *d_first = unary_op(*first1);
}

based on "Possible Implementation" from https://en.cppreference.com/w/cpp/algorithm/transform

gregmarr avatar Apr 15 '25 15:04 gregmarr

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

github-actions[bot] avatar May 16 '25 00:05 github-actions[bot]