Visitar URL original
Ruby: tweak join order in `API::Impl::edge` by nickrolfe · Pull Request #9159 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260528205931/https://github.com/github/codeql/pull/9159
Skip to content

Ruby: tweak join order in API::Impl::edge#9159

Merged
nickrolfe merged 1 commit into
mainfrom
nickrolfe/join_order_tweak
May 25, 2022
Merged

Ruby: tweak join order in API::Impl::edge#9159
nickrolfe merged 1 commit into
mainfrom
nickrolfe/join_order_tweak

Conversation

@nickrolfe
Copy link
Copy Markdown
Contributor

I still feel pretty new at this, so I'm not sure if this is a valuable change (is a nine-digit tuple count worth worrying about?), or if it's the right way of doing it.

Before

          1327277   ~0%    {3} r31 = JOIN r30 WITH CfgNodes::ExprCfgNode::getExpr#dispred#f0820431#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
           533064   ~0%    {3} r32 = JOIN r31 WITH Module::Cached::resolveConstant#fe82a56b#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                       
        112542045   ~0%    {3} r33 = JOIN r32 WITH project#Module::ResolveImpl::resolveConstantWriteAccessNonRec#fe82a56b#ffff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                       
           164888   ~0%    {3} r34 = JOIN r32 WITH project#Module::ResolveImpl::resolveConstantWriteAccessRec#fe82a56b#ffff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                       
        112706933   ~0%    {3} r35 = r33 UNION r34
           153405  ~15%    {3} r36 = JOIN r35 WITH Module::ClassDeclaration::getSuperclassExpr#dispred#f0820431#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
           146264  ~17%    {3} r37 = JOIN r36 WITH CfgNodes::ExprCfgNode::getExpr#dispred#f0820431#ff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
           144159   ~8%    {3} r38 = JOIN r37 WITH DataFlowPrivate::Cached::TExprNode#462ff392#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
           142934  ~18%    {3} r39 = JOIN r38 WITH ApiGraphs::API::Impl::MkUse#3116a2f3#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                       
           326113   ~8%    {3} r40 = r28 UNION r39
          3434003   ~5%    {3} r41 = r24 UNION r40
          5340923   ~3%    {3} r42 = r15 UNION r41
                           return r42

After

        1327277   ~0%    {3} r31 = JOIN r30 WITH CfgNodes::ExprCfgNode::getExpr#dispred#f0820431#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
          15400   ~1%    {3} r32 = JOIN r31 WITH Module::ClassDeclaration::getSuperclassExpr#dispred#f0820431#ff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                     
          14884   ~0%    {3} r33 = JOIN r32 WITH project#Module::ResolveImpl::resolveConstantWriteAccessNonRec#fe82a56b#ffff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                     
           3402   ~0%    {3} r34 = JOIN r32 WITH project#Module::ResolveImpl::resolveConstantWriteAccessRec#fe82a56b#ffff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
                     
          18286  ~20%    {3} r35 = r33 UNION r34
         153260  ~16%    {3} r36 = JOIN r35 WITH Module::Cached::resolveConstant#fe82a56b#ff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
         153324  ~15%    {3} r37 = JOIN r36 WITH CfgNodes::ExprCfgNode::getExpr#dispred#f0820431#ff_10#join_rhs ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
         153309  ~25%    {3} r38 = JOIN r37 WITH DataFlowPrivate::Cached::TExprNode#462ff392#ff ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2
         153309  ~26%    {3} r39 = JOIN r38 WITH ApiGraphs::API::Impl::MkUse#3116a2f3#ff ON FIRST 1 OUTPUT Lhs.2, Lhs.1, Rhs.1
                     
         336488  ~12%    {3} r40 = r28 UNION r39
        3444378   ~5%    {3} r41 = r24 UNION r40
        5351298   ~3%    {3} r42 = r15 UNION r41
                         return r42

@github-actions github-actions Bot added the Ruby label May 13, 2022
@nickrolfe nickrolfe marked this pull request as ready for review May 16, 2022 10:05
@nickrolfe nickrolfe requested a review from a team as a code owner May 16, 2022 10:05
@tausbn
Copy link
Copy Markdown
Contributor

tausbn commented May 16, 2022

I would say a nine-digit tuple count is definitely in the realm of "stuff to worry about".

The fix looks fine to me, though it bugs me a bit that the version that performs better is somewhat less readable. Perhaps you should poke the compiler team about this.

@nickrolfe nickrolfe added the no-change-note-required This PR does not need a change note label May 16, 2022
@nickrolfe
Copy link
Copy Markdown
Contributor Author

Perhaps you should poke the compiler team about this.

What would be your reason for poking them? Is it that you think the compiler should have been able to choose the improved join order on the old code?

@tausbn
Copy link
Copy Markdown
Contributor

tausbn commented May 16, 2022

What would be your reason for poking them? Is it that you think the compiler should have been able to choose the improved join order on the old code?

It is my understanding that they are generally interested in performance fixes that make the code "worse" (e.g. less readable).

This makes me wonder, though, if this wouldn't be better fixed with an appropriate unbinding (or refactoring using a nomagicked helper predicate). For all we know, a future iteration of the compiler may decide to pick the bad join order again.

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented May 25, 2022

I believe you can achieve the same effect by replacing

c.getSuperclassExpr() = a.asExpr().getExpr()

with

pragma[only_bind_into](c).getSuperclassExpr() = a.asExpr().getExpr()

which forces getSuperclassExpr to come before resolveConstantWriteAccess.

@nickrolfe nickrolfe force-pushed the nickrolfe/join_order_tweak branch from 3a507d6 to 134cf4e Compare May 25, 2022 10:12
@nickrolfe
Copy link
Copy Markdown
Contributor Author

That does appear to work, thanks!

@nickrolfe nickrolfe merged commit 79fb9e8 into main May 25, 2022
@nickrolfe nickrolfe deleted the nickrolfe/join_order_tweak branch May 25, 2022 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Ruby

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants