Skip to content

Solver: Deduplicate flags and stanzas in DependencyReason. - #4929

Merged
23Skidoo merged 4 commits into
haskell:masterfrom
sebright:duplicate-flag-message
Dec 19, 2017
Merged

23Skidoo merged 4 commits into
haskell:masterfrom
sebright:duplicate-flag-message

Conversation

@sebright

@sebright sebright commented Dec 4, 2017

Copy link
Copy Markdown
Collaborator

This commit changes DependencyReason's list fields to maps and sets. Duplicate
flags were possible when a flag appeared multiple times in nested conditionals
or a flag controlled a "Buildable: False" field. The duplicate flag could show
up in log messages:

Before:

[__5] trying: json-rpc-client:+demo
[__6] trying: process-1.6.1.0/installed-1.6... (dependency of json-rpc-client +demo +demo)

After:

[__5] trying: json-rpc-client:+demo
[__6] trying: process-1.6.1.0/installed-1.6... (dependency of json-rpc-client +demo)

Please include the following checklist in your PR:

  • Patches conform to the coding conventions.
  • Any changes that could be relevant to users have been recorded in the changelog.
  • The documentation has been updated, if necessary.
  • If the change is docs-only, [ci skip] is used to avoid triggering the build bots.

Please also shortly describe how you tested your change. Bonus points for added tests!

@sebright

sebright commented Dec 4, 2017

Copy link
Copy Markdown
Collaborator Author

I need to debug the test failures.

@23Skidoo 23Skidoo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, feel free to merge once you've fixed the failing tests.

@sebright
sebright force-pushed the duplicate-flag-message branch from d103caa to 0ebfc06 Compare December 10, 2017 00:26
@sebright

sebright commented Dec 10, 2017 •

Copy link
Copy Markdown
Collaborator Author

Thanks!

I fixed the bug in a second commit:

Solver: Remove redundant flag choices from FlaggedDeps.

This commit simplifies the solver's internal representation of package
conditionals by removing branches that are unreachable. For example, the solver
now simplifies

if flag(A)
  if flag(A)
    build-depends: package-a
  else
    build-depends: package-b

to

if flag(A)
  build-depends: package-a

because A must be true for the second conditional to be relevant to the
finalized PackageDescription.

This change probably won't improve performance by simplifying real packages.
However, it fixes a bug in the solver's handling of a certain edge case. Before
this change, it was possible for the solver to create DependencyReasons with
conflicting flag values. The conflicting values could cause a problem when
one was FlagBoth, the value used when the same dependency appears on both sides
of a conditional. Here is an example:

if flag(A)
  if flag(A)
    build-depends: unknown-package
  else
    build-depends: unknown-package

Previously, the solver inserted both A=FlagTrue and A=FlagBoth into the
DependencyReason for the dependency on unknown-package. When the previous
commit changed the association list of flag values into a map, FlagBoth replaced
FlagTrue and prevented the solver from trying -A.

This commit simplifies the conditionals to:

if flag(A)
  build-depends: unknown-package

It also adds some unit tests for dependencies occurring on both sides of
conditionals. "testBackjumpingWithCommonDependency" tests the example above.

EDIT: added more detail to the commit message

@sebright
sebright force-pushed the duplicate-flag-message branch from 0ebfc06 to 69a8574 Compare December 17, 2017 01:53
This commit changes DependencyReason's list fields to maps and sets. Duplicate
flags were possible when a flag appeared multiple times in nested conditionals
or a flag controlled a "Buildable: False" field. The duplicate flag could show
up in log messages:

Before:
[__5] trying: json-rpc-client:+demo
[__6] trying: process-1.6.1.0/installed-1.6... (dependency of json-rpc-client +demo +demo)

After:
[__5] trying: json-rpc-client:+demo
[__6] trying: process-1.6.1.0/installed-1.6... (dependency of json-rpc-client +demo)
This commit simplifies the solver's internal representation of package
conditionals by removing branches that are unreachable.  For example, the solver
now simplifies

if flag(A)
  if flag(A)
    build-depends: package-a
  else
    build-depends: package-b

to

if flag(A)
  build-depends: package-a

because A must be true for the second conditional to be relevant to the
finalized PackageDescription.

This change probably won't improve performance by simplifying real packages.
However, it fixes a bug in the solver's handling of a certain edge case.  Before
this change, it was possible for the solver to create DependencyReasons with
conflicting flag values.  The conflicting values could cause a problem when
one was FlagBoth, the value used when the same dependency appears on both sides
of a conditional.  Here is an example:

if flag(A)
  if flag(A)
    build-depends: unknown-package
  else
    build-depends: unknown-package

Previously, the solver inserted both A=FlagTrue and A=FlagBoth into the
DependencyReason for the dependency on unknown-package. When the previous
commit changed the association list of flag values into a map, FlagBoth replaced
FlagTrue and prevented the solver from trying -A.

This commit simplifies the conditionals to:

if flag(A)
  build-depends: unknown-package

It also adds some unit tests for dependencies occurring on both sides of
conditionals. "testBackjumpingWithCommonDependency" tests the example above.
…(.&&.).

It is probably better to simplify version ranges in most cases in the solver, in
order to simplify error messages and save memory.  Currently, (.||.) is used once
for lifting dependencies out of conditionals, and (.&&.) isn't used.
@sebright
sebright force-pushed the duplicate-flag-message branch from 69a8574 to 4e20b28 Compare December 17, 2017 01:58
…epends'.

This commit is unlikely to have an effect on real packages, but it reduces the
chance of performance problems caused by duplicate dependencies in the solver
quickcheck tests.  It also adds a regression test to memory-usage-tests.
@sebright
sebright force-pushed the duplicate-flag-message branch from 4e20b28 to 4f7ac10 Compare December 17, 2017 06:29
@sebright

Copy link
Copy Markdown
Collaborator Author

I think I fixed the issue that caused the quickcheck tests to frequently time out or run out of memory after I made the second commit. I added two more commits, with a regression test in the fourth commit:

-- | Test for an issue related to lifting dependencies out of conditionals when
-- converting a PackageDescription to the solver's internal representation.
--
-- Issue:
-- For each conditional and each package B, the solver combined each dependency
-- on B in the true branch with each dependency on B in the false branch. It
-- added the combined dependencies to the build-depends outside of the
-- conditional. Since dependencies could be lifted out of multiple levels of
-- conditionals, the number of new dependencies could grow exponentially in the
-- number of levels. For example, the following package generated 4 copies of B
-- under flag-2=False, 8 copies under flag-1=False, and 16 copies at the top
-- level:
--
-- if flag(flag-1)
--   build-depends: B, B
-- else
--   if flag(flag-2)
--     build-depends: B, B
--   else
--     if flag(flag-3)
--       build-depends: B, B
--     else
--       build-depends: B, B
--
-- This issue caused the quickcheck tests to start frequently running out of
-- memory after an optimization that pruned unreachable branches (See PR #4929).
-- Each problematic test case contained at least one build-depends field with
-- duplicate dependencies, which was then duplicated under multiple levels of
-- conditionals by the solver's "buildable: False" transformation, when
-- "buildable: False" was under multiple flags. Finally, the branch pruning
-- feature put all build-depends fields in consecutive levels of the condition
-- tree, causing the solver's representation of the package to follow the
-- pattern in the example above.
--
-- Now the solver avoids this issue by combining all dependencies on the same
-- package within a build-depends field before lifting them out of conditionals.
--
-- This test case is an expanded version of the example above, with library and
-- build-tool dependencies.
duplicateDependencies :: String -> SolverTest
duplicateDependencies name =
    mkTest pkgs name ["A"] $ solverSuccess [("A", 1), ("B", 1)]
  where
    copies, depth :: Int
    copies = 50
    depth = 50

    pkgs :: ExampleDb
    pkgs = [
        Right $ exAv "A" 1 (flaggedDependencies 1)
      , Right $ exAv "B" 1 [] `withExe` ExExe "exe" []
      ]

    flaggedDependencies :: Int -> [ExampleDependency]
    flaggedDependencies n
        | n > depth = buildDepends
        | otherwise = [exFlagged (flagName n) buildDepends
                                              (flaggedDependencies (n + 1))]
      where
        buildDepends = replicate copies (ExFix "B" 1)
                    ++ replicate copies (ExBuildToolFix "B" "exe" 1)

    flagName :: Int -> ExampleFlagName
    flagName x = "flag-" ++ show x

@23Skidoo
23Skidoo merged commit 86af280 into haskell:master Dec 19, 2017
@23Skidoo

Copy link
Copy Markdown
Member

Merged, thanks!

@sebright
sebright deleted the duplicate-flag-message branch January 14, 2018 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants