Repository navigation
perf(model): prefetch vtm pieces in parallel before reading a model #207
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
Open
BotellaA
wants to merge
5
commits into
next
Choose a base branch
from
perf/prefetch_vtm_pieces
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79e5116
perf(model): prefetch vtm pieces in parallel before reading a model
BotellaA 0ed8253
Apply prepare changes
BotellaA 1e476f5
Apply prepare changes
JulienChampagnol 843b46e
Merge remote-tracking branch 'origin/next' into HEAD
BotellaA b753a4c
Apply prepare changes
BotellaA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,4 +61,3 @@ wslink==2.6.0 | |
| yarl==1.* | ||
| # via aiohttp | ||
|
|
||
| opengeodeweb-microservice==1.*,>=1.4.0rc1 | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """Read the pieces of a VTK multiblock file ahead of the VTK reader.""" | ||
|
|
||
| # Standard library imports | ||
| import contextlib | ||
| import re | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| _PIECE_FILE = re.compile(r'\bfile="([^"]+)"') | ||
| _PREFETCH_WORKERS = 32 | ||
|
|
||
|
|
||
| def _read_piece(path: Path) -> None: | ||
| # The VTK reader reports missing or unreadable pieces itself | ||
| with contextlib.suppress(OSError): | ||
| path.read_bytes() | ||
|
|
||
|
|
||
| def prefetch_vtm_pieces(vtm_path: Path) -> int: | ||
| """Read every piece of a .vtm file once, in parallel, and return how many were read. | ||
|
|
||
| The XML multiblock reader opens its pieces one by one. Right after an import they | ||
| are new files, and an on-access antivirus scan (Windows Defender) makes each first | ||
| open slow: about 5 s for a model of 764 pieces, against 0.6 s for the VTK parsing | ||
| itself. Opening them first from threads, which release the GIL during I/O, runs | ||
| those scans in parallel; the VTK reader then opens files already scanned and cached. | ||
| """ | ||
| folder = vtm_path.parent | ||
| pieces = [folder / name for name in _PIECE_FILE.findall(vtm_path.read_text())] | ||
| with ThreadPoolExecutor(min(_PREFETCH_WORKERS, len(pieces) or 1)) as executor: | ||
| list(executor.map(_read_piece, pieces)) | ||
| return len(pieces) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from opengeodeweb_viewer import vtm_prefetch | ||
| from opengeodeweb_viewer.vtm_prefetch import prefetch_vtm_pieces | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pytest | ||
|
|
||
| DATA = Path(__file__).parent / "data" | ||
|
|
||
|
|
||
| def test_prefetch_reads_every_piece(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| read: list[Path] = [] | ||
| monkeypatch.setattr(vtm_prefetch, "_read_piece", read.append) | ||
| assert prefetch_vtm_pieces(DATA / "cube.vtm") == 45 | ||
| assert len(read) == 45 | ||
| assert all(path.is_file() for path in read) | ||
| assert all(path.parent.parent == DATA for path in read) | ||
|
|
||
|
|
||
| def test_prefetch_ignores_missing_pieces(tmp_path: Path) -> None: | ||
| (tmp_path / "piece.vtp").write_bytes(b"data") | ||
| vtm = tmp_path / "model.vtm" | ||
| vtm.write_text( | ||
| '<VTKFile><vtkMultiBlockDataSet><Block index="0">' | ||
| '<DataSet index="0" file="piece.vtp" />' | ||
| '<DataSet index="1" file="missing.vtp" />' | ||
| "</Block></vtkMultiBlockDataSet></VTKFile>" | ||
| ) | ||
| assert prefetch_vtm_pieces(vtm) == 2 | ||
|
|
||
|
|
||
| def test_prefetch_without_pieces(tmp_path: Path) -> None: | ||
| vtm = tmp_path / "empty.vtm" | ||
| vtm.write_text("<VTKFile><vtkMultiBlockDataSet /></VTKFile>") | ||
| assert prefetch_vtm_pieces(vtm) == 0 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be done only on windows ?