-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add launchpad diff command for build-to-build size comparison
#642
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
Draft
trevor-e
wants to merge
1
commit into
main
Choose a base branch
from
trevorelkins/eme-1224-build-something-cool-7b46
base: main
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.
Draft
Changes from all commits
Commits
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
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,89 @@ | ||
| """Compute size differences between two analyses.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections import defaultdict | ||
| from typing import Dict | ||
|
|
||
| from launchpad.size.models.common import BaseAnalysisResults, FileInfo | ||
| from launchpad.size.models.diff import CategoryDiff, ChangeKind, FileChange, SizeDiffResults | ||
|
|
||
|
|
||
| def _build_label(results: BaseAnalysisResults) -> str: | ||
| app_info = getattr(results, "app_info", None) | ||
| if app_info is None: | ||
| return "unknown" | ||
| return f"{app_info.version} ({app_info.build})" | ||
|
|
||
|
|
||
| def _category_totals(results: BaseAnalysisResults) -> Dict[str, int]: | ||
| totals: Dict[str, int] = defaultdict(int) | ||
| for item in results.file_analysis.items: | ||
| if item.is_dir: | ||
| continue | ||
| totals[item.treemap_type.value] += item.size | ||
| return totals | ||
|
|
||
|
|
||
| def _file_sizes(results: BaseAnalysisResults) -> Dict[str, FileInfo]: | ||
| # Top-level files only; nested children (e.g. assets inside a .car) roll up into their parent. | ||
| return {item.path: item for item in results.file_analysis.items if not item.is_dir} | ||
|
|
||
|
|
||
| def compute_diff(base: BaseAnalysisResults, head: BaseAnalysisResults) -> SizeDiffResults: | ||
| """Compare two size analyses and return the deltas from ``base`` to ``head``. | ||
|
|
||
| Files are matched by path. A file present in both builds with a different content | ||
| hash is reported as modified; hashes that match are omitted (no size change). | ||
| """ | ||
| base_categories = _category_totals(base) | ||
| head_categories = _category_totals(head) | ||
|
|
||
| category_diffs = [ | ||
| CategoryDiff( | ||
| category=category, | ||
| head_size=head_categories.get(category, 0), | ||
| base_size=base_categories.get(category, 0), | ||
| ) | ||
| for category in base_categories.keys() | head_categories.keys() | ||
| ] | ||
| category_diffs = [c for c in category_diffs if c.size_diff != 0] | ||
| category_diffs.sort(key=lambda c: abs(c.size_diff), reverse=True) | ||
|
|
||
| base_files = _file_sizes(base) | ||
| head_files = _file_sizes(head) | ||
|
|
||
| file_changes: list[FileChange] = [] | ||
| for path in base_files.keys() | head_files.keys(): | ||
| base_file = base_files.get(path) | ||
| head_file = head_files.get(path) | ||
|
|
||
| if base_file is None and head_file is not None: | ||
| file_changes.append(FileChange(path=path, kind=ChangeKind.ADDED, head_size=head_file.size, base_size=0)) | ||
| elif head_file is None and base_file is not None: | ||
| file_changes.append(FileChange(path=path, kind=ChangeKind.REMOVED, head_size=0, base_size=base_file.size)) | ||
| elif base_file is not None and head_file is not None: | ||
| if base_file.hash == head_file.hash and base_file.size == head_file.size: | ||
| continue | ||
| file_changes.append( | ||
| FileChange( | ||
| path=path, | ||
| kind=ChangeKind.MODIFIED, | ||
| head_size=head_file.size, | ||
| base_size=base_file.size, | ||
| ) | ||
| ) | ||
|
|
||
| file_changes.sort(key=lambda f: (abs(f.size_diff), f.path), reverse=True) | ||
|
|
||
| return SizeDiffResults( | ||
| app_name=getattr(getattr(head, "app_info", None), "name", "unknown"), | ||
| base_label=_build_label(base), | ||
| head_label=_build_label(head), | ||
| base_install_size=base.install_size, | ||
| head_install_size=head.install_size, | ||
| base_download_size=base.download_size, | ||
| head_download_size=head.download_size, | ||
| category_diffs=category_diffs, | ||
| file_changes=file_changes, | ||
| ) | ||
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,90 @@ | ||
| """Models for size comparisons between two analyses.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
| from typing import Dict, List | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
|
|
||
| class ChangeKind(str, Enum): | ||
| """How a file changed between two analyses.""" | ||
|
|
||
| ADDED = "added" | ||
| REMOVED = "removed" | ||
| MODIFIED = "modified" | ||
|
|
||
|
|
||
| class FileChange(BaseModel): | ||
| """A single file that was added, removed, or modified between two builds.""" | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| path: str = Field(..., description="Relative path in the bundle") | ||
| kind: ChangeKind = Field(..., description="Whether the file was added, removed, or modified") | ||
| head_size: int = Field(..., ge=0, description="Size in the new (head) build, 0 if removed") | ||
| base_size: int = Field(..., ge=0, description="Size in the old (base) build, 0 if added") | ||
|
|
||
| @property | ||
| def size_diff(self) -> int: | ||
| """Signed size delta (head - base) in bytes.""" | ||
| return self.head_size - self.base_size | ||
|
|
||
|
|
||
| class CategoryDiff(BaseModel): | ||
| """Size delta for a single treemap category.""" | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| category: str = Field(..., description="Treemap category name") | ||
| head_size: int = Field(..., ge=0, description="Category size in the new (head) build") | ||
| base_size: int = Field(..., ge=0, description="Category size in the old (base) build") | ||
|
|
||
| @property | ||
| def size_diff(self) -> int: | ||
| """Signed size delta (head - base) in bytes.""" | ||
| return self.head_size - self.base_size | ||
|
|
||
|
|
||
| class SizeDiffResults(BaseModel): | ||
| """Result of comparing two size analyses (base -> head).""" | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| app_name: str = Field(..., description="App display name (from the head build)") | ||
| base_label: str = Field(..., description="Human-readable label for the base build, e.g. version (build)") | ||
| head_label: str = Field(..., description="Human-readable label for the head build, e.g. version (build)") | ||
|
|
||
| base_install_size: int = Field(..., ge=0, description="Install size of the base build in bytes") | ||
| head_install_size: int = Field(..., ge=0, description="Install size of the head build in bytes") | ||
| base_download_size: int = Field(..., ge=0, description="Download size of the base build in bytes") | ||
| head_download_size: int = Field(..., ge=0, description="Download size of the head build in bytes") | ||
|
|
||
| category_diffs: List[CategoryDiff] = Field( | ||
| default_factory=list, description="Per-category size deltas, largest absolute change first" | ||
| ) | ||
| file_changes: List[FileChange] = Field( | ||
| default_factory=list, description="Per-file changes, largest absolute change first" | ||
| ) | ||
|
|
||
| @property | ||
| def install_size_diff(self) -> int: | ||
| """Signed install size delta (head - base) in bytes.""" | ||
| return self.head_install_size - self.base_install_size | ||
|
|
||
| @property | ||
| def download_size_diff(self) -> int: | ||
| """Signed download size delta (head - base) in bytes.""" | ||
| return self.head_download_size - self.base_download_size | ||
|
|
||
| def to_dict(self) -> Dict[str, object]: | ||
| """Convert to a JSON-serializable dictionary including computed deltas.""" | ||
| data = self.model_dump() | ||
| data["install_size_diff"] = self.install_size_diff | ||
| data["download_size_diff"] = self.download_size_diff | ||
| for category, model in zip(data["category_diffs"], self.category_diffs): | ||
| category["size_diff"] = model.size_diff | ||
| for change, model in zip(data["file_changes"], self.file_changes): | ||
| change["size_diff"] = model.size_diff | ||
| return data |
Oops, something went wrong.
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.
Empty hash skips real changes
Medium Severity
For paths present in both builds,
compute_diffskips reporting whenhashandsizeboth match. Android analysis setshashto an empty string for merged entries (e.g. combined DEX or duplicate paths), so two builds can differ in content while still being treated as unchanged when merged sizes match.Reviewed by Cursor Bugbot for commit bf5cc20. Configure here.