Skip to content

ValueFlow: avoid unnecessary ValuePtr and Value copies#4845

Open
firewave wants to merge 1 commit intodanmar:mainfrom
firewave:valueptr
Open

ValueFlow: avoid unnecessary ValuePtr and Value copies#4845
firewave wants to merge 1 commit intodanmar:mainfrom
firewave:valueptr

Conversation

@firewave
Copy link
Copy Markdown
Collaborator

@firewave firewave commented Mar 2, 2023

No description provided.

@firewave firewave marked this pull request as draft March 2, 2023 23:40
@firewave firewave marked this pull request as ready for review March 3, 2023 10:07
@firewave firewave marked this pull request as draft March 3, 2023 12:39
lib/valueptr.h Outdated
{}

template<class U>
ValuePtr(U&& value) : mPtr(cloner<U>::move(&value)), mClone(&cloner<U>::apply)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only move when its passed as an rvalue references, not for lvalue references. Also, this might be called for the copy constructor and move constructor, so it needs to be constrained.

    template<class U, REQUIRES("Must be rvalue", !is_lvalue_reference<U>), REQUIRES("Must not be ValuePtr", !std::is_base_of<ValuePtr, U>)>
    ValuePtr(U&& value) : mPtr(cloner<U>::move(&value)), mClone(&cloner<U>::apply)
    {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input. I need to check again what the actual impact of this was - probably not within the next few days.

I just pushed it again to clean up my local branches.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case where I came across this was makeAnalyzer. You would expect that it is being moved on return but it is copied so we essentially create it twice every time - with the destruction on top.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails to compile when we generate copies. I wonder if this even needs to be copyable at all.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code crashes. Will have a proper look in the next few days.

Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails to compile when we generate copies.

Yes of course, because you are trying to pass a const pointer to a non-const pointer. When you use forwarding, you need to use std::forward to conditionally move, but you are trying to unconditionally move which wont work(although you are not really moving).

Its kind of complicated to pick apply if U&& is is a const ref. Instead, its simpler to just add a requires that its an rvalue or not an lvalue(like REQUIRES("Must be rvalue", !is_lvalue_reference<U>) where I showed in the snippet). This way it can use the const U& constructor for copies.

I wonder if this even needs to be copyable at all.

It does need to be copyable. The whole purpose of this class is so polymorphic type can be a regular type instead of a pointer.

@firewave
Copy link
Copy Markdown
Collaborator Author

It appears to affect the long-running tests TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables.

@firewave firewave force-pushed the valueptr branch 2 times, most recently from 48e0761 to f0eeb7d Compare March 27, 2026 23:43
lib/valueptr.h Outdated
static_assert(false, "err");
}

template<class U>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should exclude lvalue references and valueptr so the default copy/move constructor can be called:

Suggested change
template<class U>
template<class U, REQUIRES("Must be rvalue", !is_lvalue_reference<U>), REQUIRES("Must not be ValuePtr", !std::is_same<ValuePtr, typename std::decay<U>::type>)>

Normally, I do !is_base_of for forwarding constructors so its not picked over the default copy/move constructor(because forwarding reference overloads are always picked first) but in this case we are wanting a type with a base of ValuePtr so that would not work at all, so doing is_same with decay should hopefully work.

@firewave
Copy link
Copy Markdown
Collaborator Author

Thanks for the insights. Obviously I have no idea what I am doing.

I will give another look starting from scratch with your notes. So maybe that makes things clearer.

@firewave firewave force-pushed the valueptr branch 2 times, most recently from 90b6973 to 46fcb75 Compare March 31, 2026 09:48
@firewave
Copy link
Copy Markdown
Collaborator Author

Please disregard the changes I pushed. I started with a better understanding but I think I still took a wrong turn somewhere. Will have another look tonight.

@firewave firewave force-pushed the valueptr branch 2 times, most recently from 2e8fd3d to 3b035d3 Compare April 2, 2026 13:10
Co-authored-by: Paul Fultz II <paul.fultz@amd.com>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 2, 2026

@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Apr 2, 2026

I started from scratch again and I am more confident now. The code makes sense and I looked at every copy (by adding an assert and running the unit tests) and there should no longer be any unnecessary ones. Still need to look at the case where it was taking up a visible part of the Ir.

Please have another look.

@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Apr 2, 2026

Still need to look at the case where it was taking up a visible part of the Ir.

Unfortunately that didn't help with this at all and the overall improvement from the changes here is minimal (about ~0,5% if I see it correctly).

It appears to affect the long-running tests TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables.

The "problem" there is that a lot of ValuePtr objects are created for each ValueFlow:.setValues() call. The construction/destruction of the underlying shared pointers amounts to over 6% of the total Ir (that is almost 20% of the ValueFlow:.setValues() call) in those tests,

@firewave firewave marked this pull request as ready for review April 2, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants