Browse Source

optimize FacetsBuilder with cached fields & avoid some allocs

Steve Yen 2 years ago
parent
commit
2a8237e8cc
4 changed files with 11 additions and 9 deletions
  1. 1 1
      index/index.go
  2. 1 1
      index/store/kvstore.go
  3. 2 2
      registry/store.go
  4. 7 5
      search/facets_builder.go

+ 1 - 1
index/index.go

@@ -87,7 +87,7 @@ type FieldTerms map[string][]string
 
 // FieldsNotYetCached returns a list of fields not yet cached out of a larger list of fields
 func (f FieldTerms) FieldsNotYetCached(fields []string) []string {
-	var rv []string
+	rv := make([]string, 0, len(fields))
 	for _, field := range fields {
 		if _, ok := f[field]; !ok {
 			rv = append(rv, field)

+ 1 - 1
index/store/kvstore.go

@@ -16,7 +16,7 @@ package store
 
 import "encoding/json"
 
-// KVStore is an abstraction for working with KV stores.  Note that 
+// KVStore is an abstraction for working with KV stores.  Note that
 // in order to be used with the bleve.registry, it must also implement
 // a constructor function of the registry.KVStoreConstructor type.
 type KVStore interface {

+ 2 - 2
registry/store.go

@@ -28,8 +28,8 @@ func RegisterKVStore(name string, constructor KVStoreConstructor) {
 	stores[name] = constructor
 }
 
-// KVStoreConstructor is used to build a KVStore of a specific type when 
-// specificied by the index configuration. In addition to meeting the 
+// KVStoreConstructor is used to build a KVStore of a specific type when
+// specificied by the index configuration. In addition to meeting the
 // store.KVStore interface, KVStores must also support this constructor.
 // Note that currently the values of config must
 // be able to be marshaled and unmarshaled using the encoding/json library (used

+ 7 - 5
search/facets_builder.go

@@ -29,6 +29,7 @@ type FacetBuilder interface {
 type FacetsBuilder struct {
 	indexReader index.IndexReader
 	facets      map[string]FacetBuilder
+	fields      []string
 }
 
 func NewFacetsBuilder(indexReader index.IndexReader) *FacetsBuilder {
@@ -43,14 +44,15 @@ func (fb *FacetsBuilder) Add(name string, facetBuilder FacetBuilder) {
 }
 
 func (fb *FacetsBuilder) Update(docMatch *DocumentMatch) error {
-	var fields []string
-	for _, facetBuilder := range fb.facets {
-		fields = append(fields, facetBuilder.Field())
+	if fb.fields == nil {
+		for _, facetBuilder := range fb.facets {
+			fb.fields = append(fb.fields, facetBuilder.Field())
+		}
 	}
 
-	if len(fields) > 0 {
+	if len(fb.fields) > 0 {
 		// find out which fields haven't been loaded yet
-		fieldsToLoad := docMatch.CachedFieldTerms.FieldsNotYetCached(fields)
+		fieldsToLoad := docMatch.CachedFieldTerms.FieldsNotYetCached(fb.fields)
 		// look them up
 		fieldTerms, err := fb.indexReader.DocumentFieldTerms(docMatch.IndexInternalID, fieldsToLoad)
 		if err != nil {