-
Notifications
You must be signed in to change notification settings - Fork 178
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
More efficient Eq, Ord for Set, Map #1017
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
meooow25
force-pushed
the
fast-eq-ord
branch
2 times, most recently
from
August 6, 2024 20:01
aa6ecd0
to
a0ce261
Compare
For the record, if we have foo :: Set Int -> Set Int -> Ordering
foo = compare (with GHC 9.6.3) it gets compiled to the Core Rec {
-- RHS size: {terms: 101, types: 97, coercions: 33, joins: 0/0}
$wgo1 [InlPrag=[2], Occ=LoopBreaker]
:: Set Int -> Iterator Int -> (# Ordering, Iterator Int #)
[GblId[StrictWorker([!, !])],
Arity=2,
Str=<1L><1L>,
Unf=OtherCon []]
$wgo1
= \ (ds_s5eh :: Set Int) (eta_s5ei :: Iterator Int) ->
case ds_s5eh of {
Bin bx_a3pH k_a3pI ds1_a3pJ ds2_a3pK ->
case k_a3pI of { I# x#_s5zS ->
case bx_a3pH of {
__DEFAULT ->
case $wgo1 ds1_a3pJ eta_s5ei of wild2_X1E
{ (# ww_s5vE, ww1_s5vF #) ->
case ww_s5vE of {
__DEFAULT -> wild2_X1E;
EQ ->
case ww1_s5vF `cast` <Co:2> :: ... of {
Push x1_a3qi r1_a3qj stk'_a3qk ->
case x1_a3qi of { I# y#_s5zV ->
case iterDown @Int r1_a3qj stk'_a3qk of nt_a3qm { __DEFAULT ->
case <# x#_s5zS y#_s5zV of {
__DEFAULT ->
case ==# x#_s5zS y#_s5zV of {
__DEFAULT -> (# GT, nt_a3qm `cast` <Co:3> :: ... #);
1# -> $wgo1 ds2_a3pK (nt_a3qm `cast` <Co:3> :: ...)
};
1# -> (# LT, nt_a3qm `cast` <Co:3> :: ... #)
}
}
};
Nada -> (# GT, (Nada @Int) `cast` <Co:3> :: ... #)
}
}
};
1# ->
case eta_s5ei `cast` <Co:2> :: ... of {
Push x_a3qr r1_a3qs stk'_a3qt ->
case x_a3qr of { I# y#_s5A1 ->
case iterDown @Int r1_a3qs stk'_a3qt of nt_a3qv { __DEFAULT ->
case <# x#_s5zS y#_s5A1 of {
__DEFAULT ->
case ==# x#_s5zS y#_s5A1 of {
__DEFAULT -> (# GT, nt_a3qv `cast` <Co:3> :: ... #);
1# -> (# EQ, nt_a3qv `cast` <Co:3> :: ... #)
};
1# -> (# LT, nt_a3qv `cast` <Co:3> :: ... #)
}
}
};
Nada -> (# GT, (Nada @Int) `cast` <Co:3> :: ... #)
}
}
};
Tip ->
case eta_s5ei `cast` <Co:2> :: ... of nt_s54V { __DEFAULT ->
(# EQ, nt_s54V `cast` <Co:3> :: ... #)
}
}
end Rec }
-- RHS size: {terms: 20, types: 23, coercions: 5, joins: 0/0}
foo [InlPrag=INLINABLE] :: Set Int -> Set Int -> Ordering
[GblId,
Arity=2,
Str=<1L><1L>,
Unf=Unf{Src=<vanilla>, TopLvl=True,
Value=True, ConLike=True, WorkFree=True, Expandable=True,
Guidance=IF_ARGS [0 0] 110 20}]
foo
= \ (s1_a3oU :: Set Int) (s2_a3oV :: Set Int) ->
case $wgo1
s1_a3oU ((iterDown @Int s2_a3oV (Nada @Int)) `cast` <Co:3> :: ...)
of
{ (# ww_s5vE, ww1_s5vF #) ->
case ww_s5vE of wild1_a3qC {
__DEFAULT -> wild1_a3qC;
EQ ->
case ww1_s5vF `cast` <Co:2> :: ... of {
Push ds_a4A6 ds1_a4A7 ds2_a4A8 -> LT;
Nada -> EQ
}
}
} Which looks pretty good to me! |
@treeowl, would appreciate a review if you have the time for it. |
treeowl
reviewed
Aug 23, 2024
treeowl
reviewed
Aug 23, 2024
treeowl
reviewed
Aug 23, 2024
treeowl
reviewed
Aug 24, 2024
* Add tests and benchmarks. * Implement Eq and Ord using foldMap + iterator. Effect on benchmark times, using GHC 9.6.3: Set Int, eq: -61% Set Int, compare: -53% Map Int Int, eq: -68% Map Int Int, compare: -76%
Thanks for reviewing! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
More efficient implementation moving away from the
toList
based approach.For #1016.
Benchmarks, on GHC 9.6.3:
Note: Why is the improvement less for
Set
? This is because the benchmarks useSet Int
, and there happen to be specializations forEq [Int]
andOrd [Int]
. Without specializations, the improvement forSet
is (70-75%) just likeMap
(see numbers in #1016).