Skip to content

[backport] perf: Improves a bit the recommend endpoint#2214

Merged
helio-frota merged 1 commit intoguacsec:release/0.4.zfrom
helio-frota:backport-2183-to-release/0.4.z
Mar 4, 2026
Merged

[backport] perf: Improves a bit the recommend endpoint#2214
helio-frota merged 1 commit intoguacsec:release/0.4.zfrom
helio-frota:backport-2183-to-release/0.4.z

Conversation

@helio-frota
Copy link
Contributor

@helio-frota helio-frota commented Jan 22, 2026

Backport of #2183 to 0.4.z branch
Manual backport after cherry-pick failed #2183 (comment)

Summary by Sourcery

Enhancements:

  • Batch-load related organizations and statuses for advisories and reuse them via in-memory maps instead of per-record lookups.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 22, 2026

Reviewer's Guide

Refactors the recommend endpoint advisory/status loading to batch-fetch related organizations and statuses, building lookup maps to avoid per-row queries and adjusting VersionedPurlStatus construction to accept a preloaded status model instead of doing its own database lookup.

Sequence diagram for batched advisory and status loading in recommend endpoint

sequenceDiagram
    actor Client
    participant RecommendEndpoint
    participant VersionedPurlAdvisory
    participant DB

    Client->>RecommendEndpoint: GET /recommend
    RecommendEndpoint->>VersionedPurlAdvisory: build_recommendations(tx)

    VersionedPurlAdvisory->>DB: load_many vulnerability::Entity
    DB-->>VersionedPurlAdvisory: vulns

    VersionedPurlAdvisory->>DB: statuses.load_one(advisory::Entity)
    DB-->>VersionedPurlAdvisory: advisories

    Note over VersionedPurlAdvisory: New: batch load organizations for all advisories
    VersionedPurlAdvisory->>DB: advisory_models.load_one(organization::Entity)
    DB-->>VersionedPurlAdvisory: organizations
    VersionedPurlAdvisory->>VersionedPurlAdvisory: build org_map by advisory.id

    Note over VersionedPurlAdvisory: New: batch load status models for all purl statuses
    VersionedPurlAdvisory->>DB: statuses.load_one(status::Entity)
    DB-->>VersionedPurlAdvisory: status_models
    VersionedPurlAdvisory->>VersionedPurlAdvisory: build status_map by status_id

    loop for each (vuln, advisory, purl_status)
        VersionedPurlAdvisory->>VersionedPurlAdvisory: lookup status_model in status_map
        VersionedPurlAdvisory->>VersionedPurlStatus: from_entity(vuln, status_model, tx)
        VersionedPurlStatus-->>VersionedPurlAdvisory: VersionedPurlStatus

        alt advisory already in results
            VersionedPurlAdvisory->>VersionedPurlAdvisory: push status into existing entry
        else new advisory head
            VersionedPurlAdvisory->>VersionedPurlAdvisory: lookup organization in org_map
            VersionedPurlAdvisory->>VersionedPurlAdvisory: create AdvisoryHead and push result
        end
    end

    VersionedPurlAdvisory-->>RecommendEndpoint: Vec<VersionedPurlAdvisory>
    RecommendEndpoint-->>Client: JSON response
Loading

Class diagram for batched status and organization lookup in VersionedPurl models

classDiagram
    class VersionedPurlAdvisory {
        +Vec~VersionedPurlAdvisory~ from_vulns_and_statuses(vulns, statuses, tx)
        -Vec~advisory::Model~ advisory_models
        -HashMap~Uuid, Option~organization::Model~~ org_map
        -HashMap~Uuid, Option~status::Model~~ status_map
    }

    class VersionedPurlStatus {
        +from_entity(vuln: vulnerability::Model, status_model: Option~status::Model~, tx)
        -String status
        -VulnerabilityHead vulnerability
    }

    class advisory_Model {
        +Uuid id
    }

    class organization_Model {
    }

    class status_Model {
        +Uuid id
        +String slug
    }

    class purl_status_Model {
        +Uuid status_id
    }

    class vulnerability_Model {
    }

    class AdvisoryHead {
        +from_advisory(advisory: advisory::Model, organization: Option~organization::Model~)
        +Uuid uuid
    }

    class VulnerabilityHead {
        +from_vulnerability_entity(vuln: vulnerability::Model)
    }

    VersionedPurlAdvisory --> advisory_Model : uses
    VersionedPurlAdvisory --> organization_Model : batched_lookup
    VersionedPurlAdvisory --> status_Model : batched_lookup
    VersionedPurlAdvisory --> purl_status_Model : iterates
    VersionedPurlAdvisory --> VersionedPurlStatus : aggregates
    VersionedPurlAdvisory --> AdvisoryHead : constructs

    VersionedPurlStatus --> vulnerability_Model : builds_head
    VersionedPurlStatus --> status_Model : optional_preloaded
    VersionedPurlStatus --> VulnerabilityHead : constructs

    advisory_Model --> organization_Model : related
    purl_status_Model --> status_Model : related

    AdvisoryHead --> advisory_Model : derived_from
    AdvisoryHead --> organization_Model : derived_from
    VulnerabilityHead --> vulnerability_Model : derived_from
Loading

File-Level Changes

Change Details Files
Batch-load related organizations for advisories and reuse them via a lookup map to avoid per-advisory queries.
  • Collect non-None advisory models into a Vec before relationship loading
  • Use SeaORM load_one on the advisory models to fetch related organizations in one batch
  • Build a HashMap keyed by advisory ID to map advisories to their optional organization model
  • Replace per-iteration advisory.find_related(organization::Entity).one(tx) with a lookup into the precomputed HashMap
modules/fundamental/src/purl/model/details/versioned_purl.rs
Batch-load status entities and pass them into VersionedPurlStatus without per-call database queries.
  • Use statuses.load_one(status::Entity, tx) to fetch all related status models at once
  • Build a HashMap keyed by status_id to map purl_status entries to their optional status model
  • Change VersionedPurlStatus::from_entity signature to accept an Optionstatus::Model instead of &purl_status::Model
  • Adjust call sites to look up the status model from the HashMap and pass it into from_entity
  • Remove the internal status.find_related(...) query and instead derive the slug directly from the provided status model, defaulting to "unknown" when absent
modules/fundamental/src/purl/model/details/versioned_purl.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@helio-frota helio-frota requested a review from ctron January 22, 2026 18:57
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Now that VersionedPurlStatus::from_entity no longer performs any database lookups, you can drop the tx parameter and likely make it a non-async function to simplify its API and avoid unnecessary async overhead.
  • When building org_map and status_map, consider pre-allocating with HashMap::with_capacity(...) based on the iterator lengths to avoid reallocation overhead for these hot-path lookups.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `VersionedPurlStatus::from_entity` no longer performs any database lookups, you can drop the `tx` parameter and likely make it a non-`async` function to simplify its API and avoid unnecessary async overhead.
- When building `org_map` and `status_map`, consider pre-allocating with `HashMap::with_capacity(...)` based on the iterator lengths to avoid reallocation overhead for these hot-path lookups.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@helio-frota helio-frota changed the title perf: Improves a bit the recommend endpoint [backport] perf: Improves a bit the recommend endpoint Jan 22, 2026
@helio-frota helio-frota enabled auto-merge January 23, 2026 09:08
@helio-frota helio-frota force-pushed the backport-2183-to-release/0.4.z branch from 6bcbc8a to 99d9ca5 Compare February 9, 2026 12:49
@helio-frota helio-frota requested a review from mrizzi February 11, 2026 15:58
Assisted-by: Claude Code
(cherry picked from commit 56ee049)
@helio-frota helio-frota force-pushed the backport-2183-to-release/0.4.z branch from 99d9ca5 to be0079a Compare March 3, 2026 09:28
@helio-frota helio-frota requested a review from ptomanRH March 3, 2026 09:29
@helio-frota helio-frota requested a review from Strum355 March 3, 2026 10:46
@helio-frota helio-frota added this pull request to the merge queue Mar 4, 2026
@helio-frota
Copy link
Contributor Author

@Strum355 thanks!

Merged via the queue into guacsec:release/0.4.z with commit a4c2830 Mar 4, 2026
4 checks passed
@helio-frota helio-frota deleted the backport-2183-to-release/0.4.z branch March 4, 2026 12:39
@github-project-automation github-project-automation bot moved this to Done in Trustify Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants