-
Notifications
You must be signed in to change notification settings - Fork 369
refactor: replace map-to-sorted-slice boilerplate with slices.Sorted(maps.Keys(...)) #28502
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,7 +33,10 @@ | |||||||||
|
|
||||||||||
| package workflow | ||||||||||
|
|
||||||||||
| import "sort" | ||||||||||
| import ( | ||||||||||
| "maps" | ||||||||||
| "slices" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // excludeMapKeys creates a new map excluding the specified keys | ||||||||||
| func excludeMapKeys(original map[string]any, excludeKeys ...string) map[string]any { | ||||||||||
|
|
@@ -54,10 +57,5 @@ func excludeMapKeys(original map[string]any, excludeKeys ...string) map[string]a | |||||||||
| // sortedMapKeys returns the keys of a map[string]string in sorted order. | ||||||||||
| // Used to produce deterministic output when writing environment variables. | ||||||||||
| func sortedMapKeys(m map[string]string) []string { | ||||||||||
| keys := make([]string, 0, len(m)) | ||||||||||
| for k := range m { | ||||||||||
| keys = append(keys, k) | ||||||||||
| } | ||||||||||
| sort.Strings(keys) | ||||||||||
| return keys | ||||||||||
| return slices.Sorted(maps.Keys(m)) | ||||||||||
|
||||||||||
| return slices.Sorted(maps.Keys(m)) | |
| keys := maps.Keys(m) | |
| slices.Sort(keys) | |
| return keys |
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.
slices.Sorted(maps.Keys(domainMap)) clones and sorts a fresh slice, but maps.Keys already allocates a fresh slice; this introduces an extra allocation vs the previous in-place sort.Strings on the collected keys. If domain lists can be large or computed often, consider sorting in place (maps.Keys + slices.Sort) to avoid the additional allocation.