GitLab Well Architected
Cookbook navigation

Example: compare two product lookups

Would scanning a list or building an index be a better way to find a product? Explore how the answer changes as you tell Duo more about your application.

Set up the comparison

In GitLab Duo Chat, describe a Python service that finds a product by SKU. Include its current function:

def find_product(products, sku):
    for product in products:
        if product["sku"] == sku:
            return product
    return None

Then copy the prompt from the previous page and fill its three placeholders:

  • [FEATURE/PROBLEM]: looking up products by SKU in a Python service.
  • [DESCRIBE FIRST APPROACH]: scan the product list for each lookup.
  • [DESCRIBE SECOND APPROACH]: build a dictionary keyed by SKU and reuse it for lookups.

Before choosing either approach, read what Duo assumes about the workload. Building an index takes work before the first lookup, and reusing it means accounting for changes to the data. To judge whether a dictionary would help your service, you'll need to consider both costs alongside the time it could save on each lookup.

Add a missing constraint

Continue the same conversation with this separate follow-up:

Each request receives a fresh list of 20 to 50 products and does one
lookup. We can't reuse an index between requests because prices may
change. If a SKU appears twice, the first entry must win. We haven't
observed a performance problem.

How does that affect your recommendation?

Keeping the scan is a reasonable choice here. Building a dictionary still requires reading the list, and there's only one lookup to benefit from it. Look at how Duo uses the new details to explain its recommendation. They may lead it to reconsider an index or strengthen its case for the scan; either answer should account for the workload you've described.

Check the behavior

Ask Duo for example inputs and expected results, then compare them with the function. This illustrative table lists the expected results you can check against:

Input Lookup Expected result
A MUG priced at 12, then a BAG priced at 25 BAG The BAG product
The same products HAT None
An empty list MUG None
Two MUG entries, priced at 12 then 18 MUG The first MUG, priced at 12

A dictionary comprehension such as {p["sku"]: p for p in products} keeps the last duplicate. If Duo proposes it without addressing that difference, ask how it would preserve the first match.

Change the workload

For a contrasting case, tell Duo that the service loads 100,000 products once and performs many lookups against that unchanged snapshot, with memory available for an index. Ask whether it would choose differently and what it would measure.

With many lookups against the same data, the cost of building an index can be spread across them. The answer should still account for duplicate SKUs and rebuilding the index when the data changes. Any claim about the size of a speedup would need measurements.

Back to the prompt