From 22% of cpu in hasTransientInternal to 1.0x%
ClosedPublic

Authored by jtamate on Jan 19 2018, 3:57 PM.

Details

Summary

According to callgrind, checking kwin in release with debug symbols,
22% of cpu time in the method hasTransientInternal is spent doing
a cast to Client *, while at the begining of the method, the cast to
const Client * only uses 1.0x%.

Test Plan

The workload was just show 50 messagebox copying small files and being
unable to change the permissions.

Diff Detail

Repository
R108 KWin
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
jtamate created this revision.Jan 19 2018, 3:57 PM
Restricted Application added a project: KWin. · View Herald TranscriptJan 19 2018, 3:57 PM
Restricted Application added a subscriber: kwin. · View Herald Transcript
jtamate requested review of this revision.Jan 19 2018, 3:57 PM
graesslin accepted this revision.Jan 19 2018, 8:22 PM
graesslin added a subscriber: graesslin.

Very interesting finding. Any idea why that's the case?

Can go into 5.12 branch, that should be absolutely safe.

This revision is now accepted and ready to land.Jan 19 2018, 8:22 PM

Very interesting finding. Any idea why that's the case?

My guess is that dynamic_cast is the only type of cast that does runtime checking (see next link). Probably converting to const reduces the amount of checking.
http://gcc.gnu.org/ml/gcc/1999-12n/msg00544.html

Probably it should be safe to use static_cast that only does compile_time checks.

Can go into 5.12 branch, that should be absolutely safe.

Should it be done by cherry-pick?

Did you compare that to a profile with the updated cast?
This makes little sense and a simplified testcase doesn't measure significant differences in casting times on the wall clock time. And certainly not factor 22.

The screenshot with 22% is from the release with debug symbols from opensuse (you can see the first const cast there), the other one is from a debug build with the patch.

The 22% to 1.% is only in one method.

'key...?!
I'd assume the const casting avoids deep copies of the QList - at least that's the only explanation I could come up without a deeper investigation.

'key...?!
I'd assume the const casting avoids deep copies of the QList - at least that's the only explanation I could come up without a deeper investigation.

unlikely. That was my first guess as well, but the code uses const iterator, so no deep copy.

Very interesting finding. Any idea why that's the case?

My guess is that dynamic_cast is the only type of cast that does runtime checking (see next link). Probably converting to const reduces the amount of checking.
http://gcc.gnu.org/ml/gcc/1999-12n/msg00544.html

Probably it should be safe to use static_cast that only does compile_time checks.

In this case I fear we cannot make it a static_cast. It could be that a ShellClient (Wayland) is a transient to Client (example: any child window KWin shows for the Client, such as Alt+F3, rules dialog, kill helper).

Can go into 5.12 branch, that should be absolutely safe.

Should it be done by cherry-pick?

We normally push to stable branch and then merge the stable branch into master.

Could you additional try qobject_cast? It is supposed to be faster than dynamic_cast and Client subclasses QObject.

Yes, indeed, it is faster, from 1.04% to 0,03%

jtamate updated this revision to Diff 25738.Jan 22 2018, 8:22 AM

Use qobject_cast instead of dynamic_cast, it is faster.

I'm still new to arc.
This revision was created in a branch pointing to master, therefore when I'll do arc land, it will push into master.
How do I change the branch to land without creating a new revision?
And after landing the commit, should I do: >git checkout master, git merge <commit hash> ?

I'm still new to arc.
This revision was created in a branch pointing to master, therefore when I'll do arc land, it will push into master.

Just don't use arc land. It does terrible things. I always use normal git push.

This revision was automatically updated to reflect the committed changes.