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

expression: speed up builtinRepeatSig by using MergeNulls #12674

Merged
merged 9 commits into from
Oct 23, 2019

Conversation

js00070
Copy link
Contributor

@js00070 js00070 commented Oct 13, 2019

What problem does this PR solve?

I find a TODO in the code here #12014 (comment)

So I tried to introduce vectorized null-bitmap by using function MergeNulls to speed it up

What is changed and how it works?

I use MergeNulls in builtinInsertSig, and add a rules check in MergeNulls to avoid it be called wrongly

Check List

Tests

  • Unit test

builtinRepeatSig, builtinLeftSig, builtinRightSig, builtinInsertSig, builtinReplaceSig
to speed up
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Oct 13, 2019
@codecov
Copy link

codecov bot commented Oct 13, 2019

Codecov Report

Merging #12674 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #12674   +/-   ##
===========================================
  Coverage   79.9727%   79.9727%           
===========================================
  Files           465        465           
  Lines        107319     107319           
===========================================
  Hits          85826      85826           
  Misses        15029      15029           
  Partials       6464       6464

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.
It seems that MergeNulls is called wrongly in this PR.
Please check the comment of MergeNull.

It will be nice if you can add a check in MergeNulls for the rules that the called need to follow to avoid it be called wrongly again.

@js00070
Copy link
Contributor Author

js00070 commented Oct 14, 2019

Thanks for your contribution.
It seems that MergeNulls is called wrongly in this PR.
Please check the comment of MergeNull.

It will be nice if you can add a check in MergeNulls for the rules that the called need to follow to avoid it be called wrongly again.

Thanks for reviewing my change of code.
I can see data stored in the result column is fixed-length type. Although I haven't figured out the reason why it should be fixed-length type yet, but I will try to add a check in MergeNulls first.

@js00070
Copy link
Contributor Author

js00070 commented Oct 14, 2019

Thanks for your contribution.
It seems that MergeNulls is called wrongly in this PR.
Please check the comment of MergeNull.

It will be nice if you can add a check in MergeNulls for the rules that the called need to follow to avoid it be called wrongly again.

It seems that there is a wrongly called MergeNulls in the master branch, at here

result.MergeNulls(buf, buf1)

result is a int type column, buf and buf1 are string type columns. It seems to violate the requirements of MergeNulls

@XuHuaiyu
Copy link
Contributor

@js00070
MergeNulls ONLY needs the result column to be fixed-length type.
That's because we use AppendXXX to write a new value to a var-length type column, and AppendXXX will also append a null-value into nullBitmap which is wrong.

@js00070
Copy link
Contributor Author

js00070 commented Oct 14, 2019

@js00070
MergeNulls ONLY needs the result column to be fixed-length type.
That's because we use AppendXXX to write a new value to a var-length type column, and AppendXXX will also append a null-value into nullBitmap which is wrong.

Thanks! I totally understand now!

@qw4990 qw4990 removed their request for review October 14, 2019 08:52
@js00070 js00070 requested a review from a team as a code owner October 14, 2019 10:51
@ghost ghost requested review from qw4990 and removed request for a team October 14, 2019 10:51
@zz-jason zz-jason removed the request for review from Reminiscent October 14, 2019 10:52
Copy link
Contributor

@SunRunAway SunRunAway left a comment

Choose a reason for hiding this comment

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

Could you provide a benchmark result.
Try to use https://godoc.org/golang.org/x/perf/cmd/benchstat.

Beside, could you help to remove that TODO you mentioned before?

@@ -648,6 +648,14 @@ func (c *Column) CopyReconstruct(sel []int, dst *Column) *Column {
// The caller should ensure that all these columns have the same
// length, and data stored in the result column is fixed-length type.
func (c *Column) MergeNulls(cols ...*Column) {
if len(c.offsets) != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if len(c.offsets) != 0 {
if !c.isFixed() {

@js00070
Copy link
Contributor Author

js00070 commented Oct 18, 2019

Could you provide a benchmark result.
Try to use https://godoc.org/golang.org/x/perf/cmd/benchstat.

Beside, could you help to remove that TODO you mentioned before?

I run the bench test and use benchstat to compare the performance between using MergeNulls and not using MergeNulls, the old.txt is the benchtest result without using MergeNulls, and the new.txt is the benchtest result with using MergeNulls.

here is my benchtest cmd:

go test -v -benchmem -bench=BenchmarkVectorizedBuiltinArithmeticFunc -count=20 -run=BenchmarkVectorizedBuiltinArithmeticFunc | tee old.txt

go test -v -benchmem -bench=BenchmarkVectorizedBuiltinArithmeticFunc -count=20 -run=BenchmarkVectorizedBuiltinArithmeticFunc | tee new.txt

here is my benchstat result:

$ benchstat old.txt new.txt
name                                                                            old time/op    new time/op    delta
VectorizedBuiltinArithmeticFunc/builtinArithmeticMinusRealSig-VecBuiltinFunc-4    4.30µs ± 3%    3.96µs ± 2%  -7.72%  (p=0.000 n=18+19)

name                                                                            old alloc/op   new alloc/op   delta
VectorizedBuiltinArithmeticFunc/builtinArithmeticMinusRealSig-VecBuiltinFunc-4     0.00B          0.00B         ~     (all equal)

name                                                                            old allocs/op  new allocs/op  delta
VectorizedBuiltinArithmeticFunc/builtinArithmeticMinusRealSig-VecBuiltinFunc-4      0.00           0.00         ~     (all equal)

here is my bench test code with no MergeNulls:

func (b *builtinArithmeticMinusRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
	if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil {
		return err
	}
	n := input.NumRows()
	buf, err := b.bufAllocator.get(types.ETReal, n)
	if err != nil {
		return err
	}
	defer b.bufAllocator.put(buf)
	if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil {
		return err
	}

	// result.MergeNulls(buf)
	x := result.Float64s()
	y := buf.Float64s()
	for i := 0; i < n; i++ {
		if result.IsNull(i) || buf.IsNull(i) {
			continue
		}
		if (x[i] > 0 && -y[i] > math.MaxFloat64-x[i]) || (x[i] < 0 && -y[i] < -math.MaxFloat64-x[i]) {
			return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String()))
		}
		x[i] = x[i] - y[i]
	}
	return nil
}

Copy link
Contributor

@SunRunAway SunRunAway left a comment

Choose a reason for hiding this comment

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

LGTM.

@SunRunAway SunRunAway added the status/LGT1 Indicates that a PR has LGTM 1. label Oct 21, 2019
@zyxbest
Copy link
Contributor

zyxbest commented Oct 21, 2019

/build

@zyxbest
Copy link
Contributor

zyxbest commented Oct 21, 2019

/run-all-tests

@XuHuaiyu XuHuaiyu changed the title expression: introduce vectorized null-bitmap by using mergeNulls in some vectorized string functions to speed up expression: speed up builtinRepeatSig by using MergeNulls Oct 23, 2019
Copy link
Contributor

@XuHuaiyu XuHuaiyu 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/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Oct 23, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Oct 23, 2019

/run-all-tests

@sre-bot sre-bot merged commit cb1e7cf into pingcap:master Oct 23, 2019
lfkdsk added a commit to JustProject/tidb that referenced this pull request Oct 26, 2019
…ect/tidb into feature-add-udf-support

* 'feature-add-udf-support' of https://github.com/JustProject/tidb: (26 commits)
  *: fix bug that the kill command doesn't work when the killed session is waiting for the pessimistic lock (pingcap#12852)
  executor: fix the projection upon the indexLookUp in indexLookUpJoin can't get result. (pingcap#12889)
  planner, executor: support create view on union (pingcap#12595)
  planner/cascades: introduce TransformationID in cascades planner (pingcap#12879)
  executor: fix data race in test (pingcap#12910)
  executor: reuse chunk row for insert on duplicate update (pingcap#12847)
  ddl: speed up tests (pingcap#12888)
  executor: speed up test (pingcap#12896)
  expression: implement vectorized evaluation for `builtinSecondSig` (pingcap#12886)
  expression: implement vectorized evaluation for `builtinJSONObjectSig` (pingcap#12663)
  expression: speed up builtinRepeatSig by using MergeNulls (pingcap#12674)
  expression: speed up unit tests under the expression package (pingcap#12887)
  store,kv: snapshot doesn't cache the non-exists kv entries lead to poor 'insert ignore' performance (pingcap#12872)
  executor: fix data race in `GetDirtyTable()` (pingcap#12767)
  domain: increase TTL to reduce the occurrence of reporting min startTS errors (pingcap#12578)
  executor: split test for speed up (pingcap#12881)
  executor: fix inconsistent of grants privileges with MySQL when executing `grant all on ...` (pingcap#12330)
  expression: implement vectorized evaluation for `builtinJSONUnquoteSig` (pingcap#12841)
  tune grpc connection count between tidb and tikv (pingcap#12884)
  Makefile: change test parallel to 8 (pingcap#12885)
  ...
@js00070 js00070 deleted the mergeNulls branch November 7, 2019 11:06
XiaTianliang pushed a commit to XiaTianliang/tidb that referenced this pull request Dec 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression contribution This PR is from a community contributor. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants