⚡ Bolt: [performance improvement] Memoize Printify variants API call#45
⚡ Bolt: [performance improvement] Memoize Printify variants API call#45merg357 wants to merge 1 commit into
Conversation
Replaced repetitive identical requests to Printify blueprint variants across creation loops by wrapping the function with `functools.lru_cache`. Mutations of responses are prevented by changing return types from lists to tuples, which natively serialize to JSON. Added documentation comments. Co-authored-by: merg357 <221854052+merg357@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR improves bulk Printify product-creation performance by memoizing repeated blueprint/provider variant lookups, avoiding N+1 network calls to the Printify variants.json endpoint.
Changes:
- Added
@functools.lru_cache(maxsize=None)toget_variantsin multiple scripts to reuse prior variant fetch results. - Changed
get_variantsto return immutable tuples (and updated related type hints/signatures) to prevent cached-value mutation. - Added a short internal note in
.jules/bolt.mddocumenting the “cache mutable vs immutable” lesson.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| printify_manager.py | Memoizes variant lookup and returns tuples to avoid repeated API calls during CLI-driven bulk creation. |
| merch_factory.py | Memoizes variant lookup and updates product creation signature to accept tuple variant IDs. |
| create_merch.py | Memoizes variant lookup and updates typing to use Tuple[int, ...] for variant IDs. |
| .jules/bolt.md | Documents the rationale for caching immutable sequences with lru_cache. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def get_variants(blueprint_id: int, provider_id: int) -> list: | ||
| # Why: Prevents repeated network calls to fetch blueprint variants for the same blueprint/provider in iterative product creation loops (N+1 queries). | ||
| # Impact: Significant reduction in API calls and network latency during bulk operations. | ||
| # Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved. |
| # Impact: Significant reduction in API calls and network latency during bulk operations. | ||
| # Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved. | ||
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: |
| def get_variants(blueprint_id: int, provider_id: int) -> list: | ||
| # Why: Prevents repeated network calls to fetch blueprint variants for the same blueprint/provider in iterative product creation loops (N+1 queries). | ||
| # Impact: Significant reduction in API calls and network latency during bulk operations. | ||
| # Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved. |
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) | ||
| resp.raise_for_status() | ||
| return [v["id"] for v in resp.json().get("variants", [])[:4]] | ||
| return tuple([v["id"] for v in resp.json().get("variants", [])[:4]]) |
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) | ||
| resp.raise_for_status() | ||
| return [v["id"] for v in resp.json().get("variants", [])[:4]] | ||
| return tuple([v["id"] for v in resp.json().get("variants", [])[:4]]) | ||
|
|
||
| def create_product(title: str, blueprint_id: int, provider_id: int, | ||
| variant_ids: list, image_id: str) -> dict: | ||
| variant_ids: tuple, image_id: str) -> dict: | ||
| url = f"{BASE_URL}/shops/{SHOP_ID}/products.json" |
| def get_variants(blueprint_id: int, print_provider_id: int = 29) -> Tuple[int, ...]: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{print_provider_id}/variants.json" | ||
| resp = requests.get(url, headers=get_headers()) | ||
| resp.raise_for_status() |
💡 What: Applied Python's
@functools.lru_cache(maxsize=None)toget_variantsincreate_merch.py,merch_factory.py, andprintify_manager.py. The return type ofget_variantswas updated from a list to an immutable tuple.🎯 Why: During bulk product creation, the applications repeatedly queried Printify to get variants for the same blueprints and providers over and over (N+1 queries). By memoizing this result, redundant requests are bypassed.
📊 Impact: Significant reduction in API calls and network latency during bulk operations.
🔬 Measurement: Review logging outputs during bulk creation to observe lack of latency on subsequent variant fetches.
PR created automatically by Jules for task 12171247976811877567 started by @merg357