Make hackageIndexLayout faster#343
Open
Bodigrim wants to merge 2 commits into
Open
Conversation
`hackageIndexLayout` runs `fromPath` in a hot loop, for every file in the Hackage 01-index.tar. However, `fromPath` is rather wasteful when parsing `PackageIdentifier`: it concatenates together package name and version and asks `Cabal-syntax` to parse. This is very backwards, because `PackageIdentifier` is exactly a record with two fields: package name and its version. We already have the name at hand, there is no need to parse it again, and the version can be parsed more efficiently without resorting to `parsec`. This performance optimization was earlier attested in the `hackage-revdeps` package.
`hackageIndexLayout` runs `fromPath` in a hot loop, for every file in the Hackage 01-index.tar. However, `fromPath` is rather wasteful: it uses `splitFragments` (and underlying `System.FilePath.splitDirectories`) to explode `base/4.22.0.0/base.cabal` into `["base", "4.22.0.0", "base.cabal"]`. One could have expected that `splitDirectories` simply splits filepath by '/', but this is not the case. It's a general function, which does a lot of transformations such as checking for absolute paths, splitting drive letter, first retaining '/', then stripping them, etc. The patch replaces `splitDirectories` with a rather specialized operations, aiming to avoid traversing the same characters twice. It was previously attested in the `hackage-revdeps` package that this microptimization brings measurable performance benefits.
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
Mikolaj
approved these changes
Jul 10, 2026
Mikolaj
left a comment
Member
There was a problem hiding this comment.
LGTM
If the speedup is confirmed in practice, this is a great improvement!
Contributor
|
Do we want to a) release this asap and b) make sure cabal 3.18.1 0 will allow the latest release? |
Contributor
Author
|
This is not a breaking change, so presumably it can be released as |
fgaz
reviewed
Jul 11, 2026
Comment on lines
+110
to
+117
| case reverse basename of | ||
| -- ".cabal" reversed | ||
| 'l' : 'a' : 'b' : 'a' : 'c' : '.' : _ -> | ||
| return $ Some $ IndexPkgCabal pkgId | ||
| -- ".json" reversed | ||
| 'n' : 'o' : 's' : 'j' : '.' : _ -> | ||
| return $ Some $ IndexPkgMetadata pkgId | ||
| _ -> Nothing |
This file contains hidden or 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
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.
Before these patches
hackageIndexLayouttakes up to 50% ofcabal updatetime. With the patches applied this reduces to ~10% ofcabal update, making it almost 2x faster.I originally developed this code for
hackage-revdeps, so the approach has been exercised for some time. Besides,hackageIndexLayoutis covered by tests forhackage-security.