[backport] perf: Improves a bit the recommend endpoint#2214
Merged
helio-frota merged 1 commit intoguacsec:release/0.4.zfrom Mar 4, 2026
Merged
Conversation
Contributor
Reviewer's GuideRefactors 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 endpointsequenceDiagram
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
Class diagram for batched status and organization lookup in VersionedPurl modelsclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Now that
VersionedPurlStatus::from_entityno longer performs any database lookups, you can drop thetxparameter and likely make it a non-asyncfunction to simplify its API and avoid unnecessary async overhead. - When building
org_mapandstatus_map, consider pre-allocating withHashMap::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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
6bcbc8a to
99d9ca5
Compare
Assisted-by: Claude Code (cherry picked from commit 56ee049)
99d9ca5 to
be0079a
Compare
ptomanRH
approved these changes
Mar 3, 2026
Strum355
approved these changes
Mar 4, 2026
Contributor
Author
|
@Strum355 thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #2183 to 0.4.z branch
Manual backport after cherry-pick failed #2183 (comment)
Summary by Sourcery
Enhancements: