-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leak and avoid lock when dstore disabled (#21)
- Loading branch information
1 parent
1bed777
commit 71a3707
Showing
4 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dstore | ||
|
||
func deduplicateKeys(keys []string) []string { | ||
dedup := make(map[string]struct{}, len(keys)) | ||
|
||
for _, k := range keys { | ||
if _, ok := dedup[k]; ok { | ||
continue | ||
} else { | ||
dedup[k] = struct{}{} | ||
} | ||
} | ||
|
||
dedupKs := make([]string, len(dedup)) | ||
i := 0 | ||
for k := range dedup { | ||
dedupKs[i] = k | ||
i++ | ||
} | ||
return dedupKs | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dstore | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestDeduplicateKeys(t *testing.T) { | ||
test := []string{"a", "b", "c", "d", "a"} | ||
dtk := deduplicateKeys(test) | ||
if (len(dtk) != 4) { | ||
t.Errorf("string slice should be deduplicated: %s", dtk) | ||
} | ||
test2 := []string{"a", "n"} | ||
dtk2 := deduplicateKeys(test2) | ||
if (len(dtk2) != 2) { | ||
t.Errorf("string slice %s has no duplications", test2) | ||
} | ||
t.Logf("after dedup: %s | %s", dtk, dtk2) | ||
} | ||
|
||
func BenchmarkDeduplicateKeys(b *testing.B) { | ||
test := []string{ | ||
"/frodo_feed/title_vecs/3055:4601087161", | ||
"/frodo_feed/title_vecs/3055:4601087162", | ||
"/frodo_feed/title_vecs/3055:4601087161", | ||
"/frodo_feed/title_vecs/3055:4601087165", | ||
"/frodo_feed/title_vecs/3055:4601087161", | ||
} | ||
|
||
for j := 0; j < 200; j++ { | ||
test = append(test, fmt.Sprintf("/frodo_feed/title_vecs/3055:460108716%d", j)) | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
deduplicateKeys(test) | ||
} | ||
} |