Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize tagset cursor's key #3676

Merged
merged 2 commits into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ There are breaking changes in this release. Please see the *Features* section be
- [#3646](https://github.com/influxdb/influxdb/pull/3646): Fix nil FieldCodec panic.
- [#3672](https://github.com/influxdb/influxdb/pull/3672): Reduce in-memory index by 20%-30%
- [#3673](https://github.com/influxdb/influxdb/pull/3673): Improve query performance by removing unnecessary tagset sorting.
- [#3676](https://github.com/influxdb/influxdb/pull/3676): Improve query performance by memomizing mapper output keys.

## v0.9.2 [2015-07-24]

Expand Down
5 changes: 3 additions & 2 deletions tsdb/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ func (e *Executor) executeRaw(out chan *influxql.Row) {
// Add up to the index to the values
if chunkedOutput == nil {
chunkedOutput = &MapperOutput{
Name: m.bufferedChunk.Name,
Tags: m.bufferedChunk.Tags,
Name: m.bufferedChunk.Name,
Tags: m.bufferedChunk.Tags,
cursorKey: m.bufferedChunk.key(),
}
chunkedOutput.Values = m.bufferedChunk.Values[:ind]
} else {
Expand Down
36 changes: 23 additions & 13 deletions tsdb/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ func (a MapperValues) Less(i, j int) bool { return a[i].Time < a[j].Time }
func (a MapperValues) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

type MapperOutput struct {
Name string `json:"name,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Fields []string `json:"fields,omitempty"` // Field names of returned data.
Values []*MapperValue `json:"values,omitempty"` // For aggregates contains a single value at [0]
Name string `json:"name,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Fields []string `json:"fields,omitempty"` // Field names of returned data.
Values []*MapperValue `json:"values,omitempty"` // For aggregates contains a single value at [0]
cursorKey string // Tagset-based key for the source cursor. Cached for performance reasons.
}

func (mo *MapperOutput) key() string {
return formMeasurementTagSetKey(mo.Name, mo.Tags)
return mo.cursorKey
}

// LocalMapper is for retrieving data for a query, from a given shard.
Expand Down Expand Up @@ -287,9 +288,10 @@ func (lm *LocalMapper) nextChunkRaw() (*MapperOutput, error) {

if output == nil {
output = &MapperOutput{
Name: cursor.measurement,
Tags: cursor.tags,
Fields: lm.selectFields,
Name: cursor.measurement,
Tags: cursor.tags,
Fields: lm.selectFields,
cursorKey: cursor.key(),
}
}
value := &MapperValue{Time: k, Value: v, Tags: t}
Expand Down Expand Up @@ -325,10 +327,11 @@ func (lm *LocalMapper) nextChunkAgg() (*MapperOutput, error) {
// for a single tagset.
if output == nil {
output = &MapperOutput{
Name: tsc.measurement,
Tags: tsc.tags,
Fields: lm.selectFields,
Values: make([]*MapperValue, 1),
Name: tsc.measurement,
Tags: tsc.tags,
Fields: lm.selectFields,
Values: make([]*MapperValue, 1),
cursorKey: tsc.key(),
}
// Aggregate values only use the first entry in the Values field. Set the time
// to the start of the interval.
Expand Down Expand Up @@ -643,6 +646,10 @@ type tagSetCursor struct {
// Performance profiling shows that this lookahead needs to be part
// of the tagSetCursor type and not part of the the cursors type.
pointHeap *pointHeap

// Memomize the cursor's tagset-based key. Profiling shows that calculating this
// is significant CPU cost, and it only needs to be done once.
memokey string
}

// tagSetCursors represents a sortable slice of tagSetCursors.
Expand Down Expand Up @@ -675,7 +682,10 @@ func newTagSetCursor(m string, t map[string]string, c []*seriesCursor, d *FieldC
}

func (tsc *tagSetCursor) key() string {
return formMeasurementTagSetKey(tsc.measurement, tsc.tags)
if tsc.memokey == "" {
tsc.memokey = formMeasurementTagSetKey(tsc.measurement, tsc.tags)
}
return tsc.memokey
}

// Next returns the next matching series-key, timestamp byte slice and meta tags for the tagset. Filtering
Expand Down