Skip to content

Commit 50f58bf

Browse files
chmouelpipelines-as-code[bot]
authored andcommitted
Optimize removeDuplicates function
- Use struct{} instead of bool in the map to reduce memory usage. - Combine declaration and initialization of the result slice. - Initialize result slice with a capacity equal to the input slice length to reduce allocations. These changes improve memory efficiency and make the code more concise. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent de6c90f commit 50f58bf

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkg/changedfiles/changedfiles.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ type ChangedFiles struct {
88
Renamed []string
99
}
1010

11+
// removeDuplicates removes duplicates from a slice of strings.
1112
func removeDuplicates(s []string) []string {
12-
holdit := make(map[string]bool)
13-
var result []string
13+
holdit := make(map[string]struct{})
14+
result := make([]string, 0, len(s))
1415
for _, str := range s {
1516
if _, ok := holdit[str]; !ok {
16-
holdit[str] = true
17+
holdit[str] = struct{}{}
1718
result = append(result, str)
1819
}
1920
}

0 commit comments

Comments
 (0)