Skip to content

isReachableFrom creates a new RevWalk per tag #199

Closed

Description

@gnodet

Problem

In findVersionHintTags (JGitPropertySource.java, lines 919-936), every tag is checked for reachability by creating a separate RevWalk:

private boolean isReachableFrom(Repository repository, Ref tag, ObjectId head) {
    // ...
    try (RevWalk revWalk = new RevWalk(repository)) {
        RevCommit tagCommit = revWalk.parseCommit(tagObjectId);
        RevCommit headCommit = revWalk.parseCommit(head);
        return revWalk.isMergedInto(tagCommit, headCommit);
    }
    // ...
}

This is called from a stream filter in findVersionHintTags, so for a repository with T tags, it creates T separate RevWalk instances. Each isMergedInto call may traverse a significant portion of the commit graph. Total cost is O(T × depth).

Proposed fix

Reuse a single RevWalk across all reachability checks:

protected List<String> findVersionHintTags(Git git, String hintPattern, ObjectId head) throws GitAPIException {
    Repository repository = git.getRepository();
    try (RevWalk revWalk = new RevWalk(repository)) {
        RevCommit headCommit = revWalk.parseCommit(head);
        return git.tagList().call().stream()
            .filter(tag -> isReachableFrom(revWalk, repository, tag, headCommit))
            // ...
    }
}

Impact

Only affects projects using version hint tags. For most projects (few hint tags), the overhead is negligible. For projects with many tags, the repeated RevWalk creation adds up.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions