Skip to content

gflow.d: double speed of fillInDNunambig()#12459

Merged
dlang-bot merged 1 commit intodlang:masterfrom
WalterBright:initDunambig
May 28, 2021
Merged

gflow.d: double speed of fillInDNunambig()#12459
dlang-bot merged 1 commit intodlang:masterfrom
WalterBright:initDunambig

Conversation

@WalterBright
Copy link
Member

@ibuclaw notes its quadratic performance here https://issues.dlang.org/show_bug.cgi?id=6401#c4

This change doubles its speed by noting that the matrix it creates is symmetric, so no need to redo half the work. But it's still quadratic.

The change:

  1. splits off allocation of the DNunambig vectors from their computation
  2. the inner loop starts at start rather than 0
  3. removes extra checks as being redundant with the DNunambig vector being non-null

It isn't obvious that it is correct. So I added a check that computed the vectors the old way, and compared for identity with the new results. After it passes, this check can be deleted as being too expensive.

@WalterBright WalterBright added Severity:Refactoring No semantic changes to code Compiler:Backend glue code, optimizer, code generation labels Apr 22, 2021
@dlang-bot
Copy link
Contributor

dlang-bot commented Apr 22, 2021

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#12459"

@WalterBright
Copy link
Member Author

Hmm, it passes on my local machine.

@WalterBright WalterBright added the Review:WIP Work In Progress - not ready for review or pulling label Apr 22, 2021
@WalterBright WalterBright removed the Review:WIP Work In Progress - not ready for review or pulling label Apr 23, 2021
@WalterBright
Copy link
Member Author

Looks like adding that check was a good idea, it found two bugs in the new version.

@RazvanN7
Copy link
Contributor

splits off allocation of the DNunambig vectors from their computation

Why is this necessary? How does this help performance?

After it passes, this check can be deleted as being too expensive.

It's green now.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 28, 2021

splits off allocation of the DNunambig vectors from their computation

Why is this necessary? How does this help performance?

Originally the nested foreach loop of 0..n meant number of iterations it did was (n*n), now the number of iterations would be around (n*n/2) (edit: actually to be precise (n*n/2)+n/2) - which as Walter notes is still quadratic.

* result from the previous implementation. (I already botched it once.)
* After it passes, remove.
*/
version = CheckAgainstOldMethod;
Copy link
Member

Choose a reason for hiding this comment

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

I guess this can be disabled now.

Copy link
Member Author

Choose a reason for hiding this comment

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

removed

@WalterWaldron
Copy link
Contributor

WalterWaldron commented May 3, 2021

I've posted a rough draft of a more linear solution here:
master...WalterWaldron:dnumambig

Once the hash helper functions are moved to an appropriate location, the resulting body of fillInDNunambig is quite concise.

It doesn't solve extreme degenerate cases such as https://issues.dlang.org/show_bug.cgi?id=7157 but it should out do this solution.

@ibuclaw
Copy link
Member

ibuclaw commented May 4, 2021

Bench 1 (Re-running because I'm getting different results now - I guess the background noise on my machine must be busier than usual)

Baseline bfad3f0 #12409 #12453 #12455 #12456 #12459 @WalterWaldron 0ea9d77 Baseline + WalterWaldron vs. GDC
32.543s 38.480s 36.161s 35.940s 36.314s 33.694s 32.649s 27.361s 0.985s

Bench 2

Baseline bfad3f0 #12409 #12453 #12455 #12456 #12459 @WalterWaldron 0ea9d77 Baseline + WalterWaldron vs. GDC
1m23.593s 1m34.041s 1m32.060s 1m32.213s 1m32.993s 1m24.946s 1m22.961s 1m17.279s 7.645s

At best, it's a marginal gain. Should really profile it to drill down a bit more on the lowest hanging fruit. As fixing this function isn't giving a satisfying boost, if the goal is to be as fast as gdc.

@WalterWaldron
Copy link
Contributor

WalterWaldron commented May 4, 2021

At best, it's a marginal gain. Should really profile it to drill down a bit more on the lowest hanging fruit. As fixing this function isn't giving a satisfying boost, if the goal is to be as fast as gdc.

True, it is simply the easiest of the problem functions to improve (i.e. a first step,) I'm planning to tackle the other O(N^2) backend functions mentioned in https://issues.dlang.org/show_bug.cgi?id=6401#c4 :

  • gother.d:accumda is simple to fix
  • The newly posted mscoffobj.d:MsCoffObj_getsegment is also a simple fix.
  • gflow.d:accumaecpx and gdag.d:aewalk are more complicated

It all boils down to using hash tables instead of O(N) search for each of the problem functions.

@RazvanN7
Copy link
Contributor

RazvanN7 commented May 7, 2021

ping @WalterBright

@RazvanN7
Copy link
Contributor

@WalterBright what are your plans with this PR?

@RazvanN7
Copy link
Contributor

ping @WalterBright

@WalterBright
Copy link
Member Author

I recommend pulling this and then when @WalterWaldron 's solution is ready, pull that one.

@RazvanN7
Copy link
Contributor

Thanks!

@dlang-bot dlang-bot merged commit 28a78d4 into dlang:master May 28, 2021
@WalterBright WalterBright deleted the initDunambig branch May 28, 2021 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Compiler:Backend glue code, optimizer, code generation Merge:auto-merge Severity:Refactoring No semantic changes to code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments