Skip to content

Conversation

@HellAmbro
Copy link
Contributor

This PR optimizes template fetching by retrieving templates directly for a specific tag instead of fetching all releases and filtering afterward.
Additionally, it streamlines the iteration over assets using a prebuilt asset map for faster lookups.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 24, 2025

CodSpeed Performance Report

Merging #6068 will not alter performance

Comparing HellAmbro:improve_templates (1ef3759) with main (5b3aa27)

Summary

✅ 8 untouched

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 24, 2025

Greptile Summary

Optimized template fetching by directly querying GitHub API for specific release tag (/tags/v{tag}) instead of fetching all releases and iterating. Improved asset lookup performance by building an asset_map dictionary upfront for O(1) lookups instead of repeated linear searches through assets. Made code more defensive by using .get("templates", []) to handle missing keys gracefully.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • Pure optimization with no behavioral changes - reduces API calls from fetching all releases to fetching one specific release, improves asset lookup from O(n*m) to O(m) via prebuilt map, adds defensive error handling, and maintains functional equivalence with original code
  • No files require special attention

Important Files Changed

Filename Overview
reflex/utils/templates.py Optimized template fetching to query specific release by tag instead of fetching all releases, improved asset lookup performance with prebuilt map

Sequence Diagram

sequenceDiagram
    participant Client as fetch_app_templates
    participant API as GitHub API
    participant Assets as Release Assets

    Note over Client,API: OLD: Fetch all releases
    Client->>API: GET /repos/reflex-dev/templates/releases
    API-->>Client: All releases [...]
    Client->>Client: Iterate releases to find tag match
    
    Note over Client,Assets: OLD: Linear search for each template
    loop For each template
        Client->>Assets: Search assets array for template.zip
        Assets-->>Client: Return URL or None
    end

    Note over Client,API: NEW: Direct tag query (optimized)
    Client->>API: GET /releases/tags/v{tag}
    alt Release exists
        API-->>Client: Specific release data
        Client->>Client: Build asset_map dictionary (O(1) lookups)
        Client->>API: GET templates.json from asset_map
        API-->>Client: templates.json content
        loop For each template
            Client->>Client: Lookup code_url in asset_map (O(1))
        end
    else Release not found
        API-->>Client: 404 Not Found
        Client->>Client: Return empty dict
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Additional Comments (1)

  1. reflex/utils/templates.py, line 284-287 (link)

    logic: Cannot assign to code_url field because Template is a frozen dataclass (line 15). This will raise dataclasses.FrozenInstanceError at runtime.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@HellAmbro HellAmbro marked this pull request as draft December 24, 2025 23:50
@HellAmbro HellAmbro marked this pull request as ready for review December 25, 2025 00:08
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 25, 2025

Greptile found no issues!

From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant