-
-
Notifications
You must be signed in to change notification settings - Fork 267
refactor(storage/postgres): simplify snapshot creation logic in commo… #2576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughRewrote snapshot construction in Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant common.go
Note over common.go: parse snapshot fields\n(xmin, xmax, xip_list)
Caller->>common.go: RequestAddXid(xid, snapshot)
common.go->>common.go: parse xmin, parse xmax, validate xid in [xmin,xmax)
alt invalid xmin/xmax or invalid xip entry
common.go-->>Caller: return original snapshot
else xid already in xip_list
common.go-->>Caller: return original snapshot
else valid and not present
common.go->>common.go: append xid to xips array
common.go->>common.go: sortXips(xips)
common.go->>common.go: rebuild xip_list string
common.go-->>Caller: return updated snapshot
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/storage/postgres/utils/common.go (1)
87-96: Replace bubble sort with standard library sort for better performance.The bubble sort implementation has O(n²) time complexity. While typical
xip_listsizes may be small, usingsort.Slicefrom the standard library would be more idiomatic, maintainable, and efficient (O(n log n)).Apply this diff to use the standard library:
+import ( + "sort" + // ... other imports +) // sortXips sorts a slice of xip values in ascending order func sortXips(xips []uint64) { - for i := 0; i < len(xips)-1; i++ { - for j := i + 1; j < len(xips); j++ { - if xips[i] > xips[j] { - xips[i], xips[j] = xips[j], xips[i] - } - } - } + sort.Slice(xips, func(i, j int) bool { + return xips[i] < xips[j] + }) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
internal/storage/postgres/utils/common.go(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Test with Coverage
- GitHub Check: Scan Container for Vulnerabilities
- GitHub Check: Analyze (go)
- GitHub Check: Test with Coverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
internal/storage/postgres/utils/common.go (2)
94-98: Consider pre-allocating the xipStrs slice.Since the final size of
xipStrsis known upfront (len(xips)), pre-allocating the slice can avoid multiple allocations and improve performance slightly.Apply this diff:
// Rebuild xip_list string - var xipStrs []string + xipStrs := make([]string, 0, len(xips)) for _, xip := range xips { xipStrs = append(xipStrs, fmt.Sprintf("%d", xip)) }
102-111: Prefer standard library sort for better performance.The bubble sort implementation has O(n²) time complexity. While PostgreSQL snapshots typically have small
xip_listsizes, using the standard library'ssort.Slicewould be more efficient and idiomatic.Apply this diff:
+import ( + "sort" + // ... other imports +) // sortXips sorts a slice of xip values in ascending order func sortXips(xips []uint64) { - for i := 0; i < len(xips)-1; i++ { - for j := i + 1; j < len(xips); j++ { - if xips[i] > xips[j] { - xips[i], xips[j] = xips[j], xips[i] - } - } - } + sort.Slice(xips, func(i, j int) bool { + return xips[i] < xips[j] + }) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
internal/storage/postgres/utils/common.go(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Test with Coverage
- GitHub Check: Analyze (go)
- GitHub Check: Scan Container for Vulnerabilities
- GitHub Check: Test with Coverage
🔇 Additional comments (2)
internal/storage/postgres/utils/common.go (2)
48-68: Excellent fix for the xid bounds validation!The implementation now correctly validates that
xidfalls within the valid range[xmin, xmax)before adding it to thexip_list, which addresses the previous review concern. The early return whenxmax == xidis also correct, as no modification is needed in that case.
70-88: Robust xip_list parsing implementation.The parsing logic correctly handles edge cases including empty entries, whitespace trimming, invalid values, and duplicate detection. The defensive approach of returning the original snapshot on any parse error ensures safe fallback behavior.
…n.go
Summary by CodeRabbit