summaryrefslogtreecommitdiffstats
path: root/src/merger_test.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
commit37dc273148df0893053bf5cda0582a23f5c2b2d2 (patch)
treed90f5e96fa97de429265461268b1a6db2703cb4c /src/merger_test.go
parentf7f01d109eb05c7eae82c243b6b6d5c5951ee707 (diff)
Micro-optimizations
- Make structs smaller - Introduce Result struct and use it to represent matched items instead of reusing Item struct for that purpose - Avoid unnecessary memory allocation - Avoid growing slice from the initial capacity - Code cleanup
Diffstat (limited to 'src/merger_test.go')
-rw-r--r--src/merger_test.go26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/merger_test.go b/src/merger_test.go
index f62f9759..d50470f6 100644
--- a/src/merger_test.go
+++ b/src/merger_test.go
@@ -15,7 +15,7 @@ func assert(t *testing.T, cond bool, msg ...string) {
}
}
-func randItem() *Item {
+func randResult() *Result {
str := fmt.Sprintf("%d", rand.Uint32())
offsets := make([]Offset, rand.Int()%3)
for idx := range offsets {
@@ -23,10 +23,10 @@ func randItem() *Item {
eidx := sidx + int32(rand.Uint32()%20)
offsets[idx] = Offset{sidx, eidx}
}
- return &Item{
- text: util.RunesToChars([]rune(str)),
- rank: buildEmptyRank(rand.Int31()),
- offsets: offsets}
+ return &Result{
+ item: &Item{text: util.RunesToChars([]rune(str))},
+ offsets: offsets,
+ rank: rank{index: rand.Int31()}}
}
func TestEmptyMerger(t *testing.T) {
@@ -36,23 +36,23 @@ func TestEmptyMerger(t *testing.T) {
assert(t, len(EmptyMerger.merged) == 0, "Invalid merged list")
}
-func buildLists(partiallySorted bool) ([][]*Item, []*Item) {
+func buildLists(partiallySorted bool) ([][]*Result, []*Result) {
numLists := 4
- lists := make([][]*Item, numLists)
+ lists := make([][]*Result, numLists)
cnt := 0
for i := 0; i < numLists; i++ {
- numItems := rand.Int() % 20
- cnt += numItems
- lists[i] = make([]*Item, numItems)
- for j := 0; j < numItems; j++ {
- item := randItem()
+ numResults := rand.Int() % 20
+ cnt += numResults
+ lists[i] = make([]*Result, numResults)
+ for j := 0; j < numResults; j++ {
+ item := randResult()
lists[i][j] = item
}
if partiallySorted {
sort.Sort(ByRelevance(lists[i]))
}
}
- items := []*Item{}
+ items := []*Result{}
for _, list := range lists {
items = append(items, list...)
}