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

executor: remove *MVMap from agg executor #7541

Merged
merged 6 commits into from
Sep 11, 2018
Merged

Conversation

XuHuaiyu
Copy link
Contributor

@XuHuaiyu XuHuaiyu commented Aug 29, 2018

What problem does this PR solve?

Fix #7450

  • CPU profile to be uploaded.

What is changed and how it works?

Change groupMap of HashAggExec and HashAggFinalWorker from *MVMap to
stringSet, thus we can avoid some cost of runtime.sliceToString,
runtime.byteSliceToString, and the access to MVMap.

Check List

Tests
Exist tests

  • No code

Code changes

  • Has exported function/method change

Side effects

Related changes

@@ -19,7 +19,7 @@ import (

type decimalSet map[types.MyDecimal]struct{}
type float64Set map[float64]struct{}
type stringSet map[string]struct{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this file to util package?

"github.com/spaolacci/murmur3"
"golang.org/x/net/context"
"sort"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to the first block.

@shenli
Copy link
Member

shenli commented Aug 29, 2018

Any benchmark result?

for _, item := range w.groupByItems {
v, err := item.Eval(row)
v, isNull, err := item.EvalString(ctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can not simply call EvalString here.

var err error
w.groupKey, err = codec.EncodeValue(sc, w.groupKey[:0], w.groupValDatums...)
return w.groupKey, errors.Trace(err)
return groupKey, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reuse a []byte to reduce the string object allocation?

}
partialResults := e.getPartialResults(groupKey)

sort.Strings(e.sortedGroupKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this sort is needed?

if item.GetType().Tp == mysql.TypeNewDecimal {
v.SetLength(0)
}
v, isNull, err := item.EvalString(ctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's not safe to call EvalString?

@XuHuaiyu
Copy link
Contributor Author

Some bugs remained, working on this

@XuHuaiyu
Copy link
Contributor Author

/run-all-tests

@XuHuaiyu
Copy link
Contributor Author

XuHuaiyu commented Sep 3, 2018

/run-all-tests

@XuHuaiyu
Copy link
Contributor Author

XuHuaiyu commented Sep 3, 2018

/run-common-test tidb-test=pr/614
/run-integration-common-test tidb-test=pr/614

@shenli
Copy link
Member

shenli commented Sep 3, 2018

@XuHuaiyu Please post the benchmark result here.

@coocood
Copy link
Member

coocood commented Sep 3, 2018

The stringSet has many pointers, GC overhead would be much higher.

@XuHuaiyu
Copy link
Contributor Author

XuHuaiyu commented Sep 4, 2018

  1. There is no obvious improvement for T1 mentioned in Tune the perfomance of hash aggregate operator #7450 (comment):
master this PR
43.0901493788s 42.8431748867s
old.txt new.txt

Optimize points:

  • 3.77% runtime.stringToByteSliceof CPU cost introduced by HashAggPartialWorker.shuffleIntermData
    This cannot be avoid since we need to cast string as byte slice to calculate the hash value.
  • 0.75% runtime.sliceToString of CPU cost introduced by baseHashAggWorker.getPartialResult
  • 8.86% MVMap.Get and MVMap.Put of CPU cost introduced by access to MVMap

The performance is not improved obviously because that the access to StringSet which is used to replace MVMap seems cost as much as the access to MVMap.
But it would be more readable after using StringSet instead of MVMap.

  1. For T2 mentioned in: Tune the perfomance of hash aggregate operator #7450 (comment)

After using a string slice to store the group keys and traverse it instead of the MVMap when fetching the final result, we got about 33.47% CPU performance improvement.

master this PR
463.533461905s 308.472225189s
old.txt new.txt

Optimize points:

  • 4.5% runtime.makeslice introduced by the definition of vals in HashAggExec.getGropuKey
  • 17.5% MVMap.Get and MVMap.Put introduced by the access to MVMap

@XuHuaiyu
Copy link
Contributor Author

XuHuaiyu commented Sep 4, 2018

Seen from the profile picture, it seems the GC overhead is not much more than the master branch. @coocood

@@ -680,35 +675,34 @@ func (e *HashAggExec) execute(ctx context.Context) (err error) {
}
}

func (e *HashAggExec) getGroupKey(row chunk.Row) ([]byte, error) {
vals := make([]types.Datum, 0, len(e.GroupByItems))
func (e *HashAggExec) getGroupKey(row chunk.Row) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be further optimized. Maybe in the next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can change the way to calculate the []byte according to the group by items.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method maybe better return []byte directly and make to string lazy happen, and then modify StringSet add a Exists override method that accept []byte as argument, then use this small trick golang/go@8072f46#diff-590553aade2ea7ef319660a8151c5eb9R581

I feel @coocood want to make StringSet be map[[n]byte]struct if we have some "magic design" make groupKey be fixed length? 🐱

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe hack.String or hack.Slice is what you want.

@zz-jason
Copy link
Member

zz-jason commented Sep 4, 2018

@XuHuaiyu 308.472225189s is too slow, what is the corresponding query?

@zz-jason zz-jason added type/enhancement The issue or PR belongs to an enhancement. and removed type/performance labels Sep 5, 2018
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason
Copy link
Member

/run-all-tests

@zz-jason zz-jason added status/LGT1 Indicates that a PR has LGTM 1. status/all tests passed labels Sep 10, 2018
@XuHuaiyu
Copy link
Contributor Author

/run-common-test tidb-test=pr/614
/run-integration-common-test tidb-test=pr/614

1 similar comment
@lysu
Copy link
Contributor

lysu commented Sep 11, 2018

/run-common-test tidb-test=pr/614
/run-integration-common-test tidb-test=pr/614

@lysu
Copy link
Contributor

lysu commented Sep 11, 2018

/run-common-test tidb-test=pr/614

Copy link
Contributor

@lysu lysu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@XuHuaiyu XuHuaiyu added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 11, 2018
@zz-jason zz-jason merged commit 27047a0 into pingcap:master Sep 11, 2018
@XuHuaiyu XuHuaiyu deleted the tune_agg branch September 12, 2018 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants