Skip to content

Conversation

@WarpWing
Copy link
Contributor

No description provided.

@WarpWing WarpWing changed the title Add docker-compose configuration for testing Added docker compose file for Mongo + Redis and Optimized helper.go Dec 30, 2025
})
for i, part := range parts {
parts[i] = cases.Title(language.English).String(part)
if !strings.ContainsAny(s, "_-") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the previous Casebuilder above and used strings.Builder instead. Using Builder here to avoid allocations from Join.

if strings.EqualFold(r, rarity) {
return i
}
if idx, ok := rarityMap[strings.ToLower(rarity)]; ok {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using lookup here instead of loop

return re.ReplaceAllStringFunc(template, func(match string) string {
name := strings.Trim(match, "{}")
return variableRegex.ReplaceAllStringFunc(template, func(match string) string {
name := match[1 : len(match)-1] // Faster than strings.Trim
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See first comment about pre compiling regex.


func Filter[T any](slice []T, predicate func(T) bool) []T {
var result []T
result := make([]T, 0, len(slice)/2) // Estimate half will match
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pre allocating result slice here, no need to start with nil and append.

}
}

sort.Slice(slice, func(i, j int) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using quicksort over bubblesort here because n log n on top 😈

return 1
}

func GetHexColor(color string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fmt.sscaf is slow, strconv is faster here.

Copy link
Contributor Author

@WarpWing WarpWing left a comment

Choose a reason for hiding this comment

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

images (2).jpeg

@DuckySoLucky DuckySoLucky requested a review from Copilot December 31, 2025 16:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds Docker Compose configuration for MongoDB and Redis services and implements significant performance optimizations in helper.go, including pre-compiled regexes, buffer pooling, improved string building, and replacing bubble sort implementations with Go's built-in sort package.

Key changes:

  • Added Docker Compose file with MongoDB and Redis service configurations including health checks
  • Optimized helper functions with better algorithms (replacing O(n²) bubble sorts with O(n log n) sorts)
  • Improved memory efficiency through string builders, buffer pooling, and pre-allocated maps

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
docker-compose.yml Adds containerized MongoDB and Redis services with health checks and persistent volumes
src/utility/helper.go Performance optimizations including regex pre-compilation, buffer pooling, efficient sorting, and improved string handling
NotEnoughUpdates-REPO Updates subproject commit reference

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


func Filter[T any](slice []T, predicate func(T) bool) []T {
var result []T
result := make([]T, 0, len(slice)/2) // Estimate half will match
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

The capacity pre-allocation assumes half the items will match, which may waste memory if the actual match rate is much lower, or cause reallocations if it's higher. Consider using len(slice) as the capacity to avoid reallocations in worst-case scenarios, or use 0 to let Go's append grow naturally for typical cases.

Suggested change
result := make([]T, 0, len(slice)/2) // Estimate half will match
result := make([]T, 0, len(slice)) // Preallocate for worst-case: all elements match

Copilot uses AI. Check for mistakes.
return re.ReplaceAllStringFunc(template, func(match string) string {
name := strings.Trim(match, "{}")
return variableRegex.ReplaceAllStringFunc(template, func(match string) string {
name := match[1 : len(match)-1] // Faster than strings.Trim
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

While the inline comment explains the optimization, it would be clearer to note that this assumes the regex match format is always {name}. Consider adding a brief explanation of why this is safe given the regex pattern.

Suggested change
name := match[1 : len(match)-1] // Faster than strings.Trim
name := match[1 : len(match)-1] // Assumes match is "{name}" as enforced by variableRegex (`\{(\w+)\}`); faster than strings.Trim

Copilot uses AI. Check for mistakes.
Comment on lines +320 to +321
if intValue := int(value); intValue > 0 {
return "+" + strconv.Itoa(intValue)
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

Converting float64 to int by direct cast truncates rather than rounds, which can produce incorrect results for values like 9.9 (becomes 9 instead of 10). Consider using math.Round(value) before the int conversion to ensure proper rounding behavior.

Copilot uses AI. Check for mistakes.
}

return fmt.Sprintf("%.0f", math.Abs(value))
return strconv.Itoa(int(math.Abs(value)))
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

Similar to the previous issue, direct casting from float64 to int truncates instead of rounding. Use int(math.Round(math.Abs(value))) to ensure values like -5.9 round to 6 instead of truncating to 5.

Suggested change
return strconv.Itoa(int(math.Abs(value)))
return strconv.Itoa(int(math.Round(math.Abs(value))))

Copilot uses AI. Check for mistakes.
if httpErr != nil {
return
}
resp.Body.Close()
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

The response body is closed without checking for errors or using defer. If the function returns early before this line, the body won't be closed, causing a resource leak. Use defer resp.Body.Close() immediately after checking the HTTP error to ensure the body is always closed.

Suggested change
resp.Body.Close()
defer resp.Body.Close()

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants