Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up LCA check #4754

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,32 +2756,22 @@ before x y =
selectAncestorsOfY = ancestorSql y

lca :: CausalHashId -> CausalHashId -> Transaction (Maybe CausalHashId)
lca x y =
queryStreamCol (ancestorSql x) \nextX ->
queryStreamCol (ancestorSql y) \nextY -> do
let getNext = (,) <$> nextX <*> nextY
loop2 seenX seenY =
getNext >>= \case
(Just px, Just py) ->
let seenX' = Set.insert px seenX
seenY' = Set.insert py seenY
in if Set.member px seenY'
then pure (Just px)
else
if Set.member py seenX'
then pure (Just py)
else loop2 seenX' seenY'
(Nothing, Nothing) -> pure Nothing
(Just px, Nothing) -> loop1 nextX seenY px
(Nothing, Just py) -> loop1 nextY seenX py
loop1 getNext matches v =
if Set.member v matches
then pure (Just v)
else
getNext >>= \case
Just v -> loop1 getNext matches v
Nothing -> pure Nothing
loop2 (Set.singleton x) (Set.singleton y)
lca x y = do
queryMaybeCol
[sql|
WITH x_ancestors(id) AS (
$selectAncestorsOfX
), y_ancestors(id) AS (
$selectAncestorsOfY
) SELECT id FROM (
SELECT id FROM x_ancestors
INTERSECT
SELECT id FROM y_ancestors
) LIMIT 1
Comment on lines +2762 to +2770
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't tested this, (maybe I need a table alias?)

Not sure there's any guarantee of correct row order coming out of an INTERSECT, so should probably find a way to do that or we might not get the LEAST common ancestor.

|]
where
selectAncestorsOfX = ancestorSql x
selectAncestorsOfY = ancestorSql y

ancestorSql :: CausalHashId -> Sql
ancestorSql h =
Expand Down
Loading