Add fast-path for equality matching

This commit is contained in:
Fabian Reinartz 2017-04-05 14:14:30 +02:00
parent 1579e12011
commit 7de2217011
4 changed files with 17 additions and 6 deletions

View file

@ -7,20 +7,20 @@ import (
) )
func mmap(f *os.File, sz int) ([]byte, error) { func mmap(f *os.File, sz int) ([]byte, error) {
low, high := uint32(size), uint32(size>>32) low, high := uint32(sz), uint32(sz>>32)
h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, low, high, nil) h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, low, high, nil)
if h == 0 { if h == 0 {
return os.NewSyscallError("CreateFileMapping", errno) return nil, os.NewSyscallError("CreateFileMapping", errno)
} }
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz)) addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
if addr == 0 { if addr == 0 {
return os.NewSyscallError("MapViewOfFile", errno) return nil, os.NewSyscallError("MapViewOfFile", errno)
} }
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil { if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
return os.NewSyscallError("CloseHandle", err) return nil, os.NewSyscallError("CloseHandle", err)
} }
return (*[1 << 30]byte)(unsafe.Pointer(addr))[:sz], nil return (*[1 << 30]byte)(unsafe.Pointer(addr))[:sz], nil

View file

@ -688,7 +688,7 @@ func (r *indexReader) Postings(name, value string) (Postings, error) {
off, ok := r.postings[key] off, ok := r.postings[key]
if !ok { if !ok {
return nil, ErrNotFound return emptyPostings, nil
} }
flag, b, err := r.section(off) flag, b, err := r.section(off)

View file

@ -34,6 +34,9 @@ func (m *EqualMatcher) Name() string { return m.name }
// Matches implements Matcher interface. // Matches implements Matcher interface.
func (m *EqualMatcher) Matches(v string) bool { return v == m.value } func (m *EqualMatcher) Matches(v string) bool { return v == m.value }
// Value returns the matched value.
func (m *EqualMatcher) Value() string { return m.value }
// NewEqualMatcher returns a new matcher matching an exact label value. // NewEqualMatcher returns a new matcher matching an exact label value.
func NewEqualMatcher(name, value string) Matcher { func NewEqualMatcher(name, value string) Matcher {
return &EqualMatcher{name: name, value: value} return &EqualMatcher{name: name, value: value}

View file

@ -157,6 +157,15 @@ func (q *blockQuerier) Select(ms ...labels.Matcher) SeriesSet {
} }
func (q *blockQuerier) selectSingle(m labels.Matcher) Postings { func (q *blockQuerier) selectSingle(m labels.Matcher) Postings {
// Fast-path for equal matching.
if em, ok := m.(*labels.EqualMatcher); ok {
it, err := q.index.Postings(em.Name(), em.Value())
if err != nil {
return errPostings{err: err}
}
return it
}
tpls, err := q.index.LabelValues(m.Name()) tpls, err := q.index.LabelValues(m.Name())
if err != nil { if err != nil {
return errPostings{err: err} return errPostings{err: err}
@ -174,7 +183,6 @@ func (q *blockQuerier) selectSingle(m labels.Matcher) Postings {
res = append(res, vals[0]) res = append(res, vals[0])
} }
} }
if len(res) == 0 { if len(res) == 0 {
return emptyPostings return emptyPostings
} }