Skip to content

Refactor file content searching for a 40% speed increase - #2505

Merged
vladsavelyev merged 4 commits into
MultiQC:mainfrom
rhpvorderman:fasterreportgather
Apr 29, 2024
Merged

vladsavelyev merged 4 commits into
MultiQC:mainfrom
rhpvorderman:fasterreportgather

Conversation

@rhpvorderman

Copy link
Copy Markdown
Contributor
  • This comment contains a description of changes (with reason)

This report does the following:

  • Create a new SearchFile class that contains a line_iterator function.
    • This iterator function caches the filehandle used to read the file.
    • SearchFile is a context manager, and closes the file on exit.

The previous code would reuse the content lines as much as possible, but would read the file again when not enough content lines were read already. By using a object, the existing filehandle can be kept alive. This prevents reopening and rereading.

Also by moving all the line reading code into a separate function, the resulting code in search_file has become more readable in my opinion.

The speed uplift is very significant. Tested with multiqc --force --no-data-dir --profile-runtime ../test-data/ (The multiqc test-data repo)

Before:

|           multiqc | Run took 24.66 seconds
|           multiqc |  - 9.70s: Searching files
|           multiqc |  - 8.40s: Running modules
|           multiqc |  - 5.07s: Compressing report data

After:

|           multiqc | Run took 20.25 seconds
|           multiqc |  - 5.12s: Searching files
|           multiqc |  - 8.44s: Running modules
|           multiqc |  - 5.11s: Compressing report data

A more than 40% reduction in searching files is very welcome. This was benchmarked on a nvme SSD, but I regularly work in a Network Filesystem which is orders of magnitude slower. These speed increases become very significant in such environments.

@rhpvorderman rhpvorderman changed the title Refactor file content searching Refactor file content searching for a 40% speed increase Apr 24, 2024
@vladsavelyev vladsavelyev added this to the MultiQC v1.22: Pydantic milestone Apr 26, 2024
@vladsavelyev

Copy link
Copy Markdown
Contributor

This is amazing!! I'll look into merging this today

Comment thread multiqc/utils/report.py Resolved
@vladsavelyev

vladsavelyev commented Apr 27, 2024 •

Copy link
Copy Markdown
Contributor

This is a really amazing refactoring, thanks so much for this! Love the use of the lazy line_iterator method, and encapsulation into the SearchFile class. And I always wished to see the search file as a class to enable static type checking and autocompletion.

Should probably go further and try to put SearchFile into report.files and expose to find_log_files() in all modules. Add type hints, replace the dict keying with object attribute access, and use the path property and the line iterator wherever it makes sense. Happy to give it a try in another PR.

@rhpvorderman

Copy link
Copy Markdown
Contributor Author

Should probably go further and try to put SearchFile into report.files and expose to find_log_files() in all modules. Add type hints, replace the dict keying with object attribute access, and use the path property and the line iterator whenever it makes sense. Happy to give it a try in another PR.

Before you do that, I have another PR in the waiting that depends on this PR and makes the file searching another 20% faster. I will open it as soon as this is merged.

@vladsavelyev
vladsavelyev self-requested a review April 29, 2024 09:54

@vladsavelyev vladsavelyev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great! Thanks a lot for the clear and very pythonic code. Very excited for future optimizations.

By the way, feel free to create stacked PRs if if you have more cool stuff downstream of an existing unmergeed PR - i.e. when pr_a is based from main, pr_b is based from pr_a, etc. Even if it's a draft. Good to anticipate more awesome stuff so I don't accidentally create unnecessary merge conflicts 👍

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