cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

ProgramMemory: avoid unnecessary copy in `erase_if()`

Open firewave opened this issue 1 year ago • 5 comments

firewave avatar Jul 31 '24 23:07 firewave

-D__GNUC__ --check-level=exhaustive ../lib/utils.cpp

Clang 17 773,369,412 -> 772,984,251 GCC 14 794,493,015 -> 795,010,529

The example from https://trac.cppcheck.net/ticket/10765#comment:4:

Clang 17 3,988,168,871 -> 3,989,222,640 GCC 14 4,098,327,498 -> 4,100,109,857

I thought this improves things slightly. Seems like I misread the local results I had before. But maybe it will improve things if the use count thing is fixed...

firewave avatar Jul 31 '24 23:07 firewave

The latest rework get rid of the unnecessary copy-on-writes and tries to minimizes the redundant work. Unfortunately this is not really faster at all with few conditions - needs some more looking at with more complex code though.

It exposed an issue with std::find_if though with the unexpected copies of objects which needs to be looked into.

firewave avatar Aug 15 '24 08:08 firewave

It exposed an issue with std::find_if though with the unexpected copies of objects which needs to be looked into.

    auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const decltype(mValues->cbegin())::value_type& entry) {
        return pred(entry.first);
    });

The actual type provided by this is std::pair<const ExprIdToken, ValueFlow::Value>. The missing const on the first type is causing the copies.

firewave avatar Aug 15 '24 09:08 firewave

The actual type provided by this is std::pair<const ExprIdToken, ValueFlow::Value>. The missing const on the first type is causing the copies.

I filed an upstream ticket about detecting this: https://github.com/llvm/llvm-project/issues/104572. Still need to file one about detecting it ourselves.

firewave avatar Aug 16 '24 09:08 firewave

This greatly reduces the amount of copies but in the used test cases it does not translate into any visible improvements. Let's see what the selfcheck says.

firewave avatar Dec 28 '24 13:12 firewave