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: refactor cache logic for builtinRegexpSubstrFuncSig #49584

Merged
merged 2 commits into from
Dec 20, 2023

Conversation

lcwangchao
Copy link
Collaborator

@lcwangchao lcwangchao commented Dec 19, 2023

What problem does this PR solve?

Issue Number: close #49583

Problem Summary:

See #49583 and #49464

What changed and how does it work?

  1. Add CtxID to StatementContext to return a unique ID for statement context.
  2. Introduce builtinFuncCache to provide a cache mechanism for built function. It uses context ID to separate cache between different statements so that we can still cache some immediate states even if plan cache is enabled now.
  3. Introduce regexpNewBaseFuncSig as a new base for regex related functions. We'll use regexpNewBaseFuncSig to replace the old regexpBaseFuncSig in the next several PRs.
  4. refactor builtinRegexpSubstrFuncSig to use regexpNewBaseFuncSig to manage the cache, so that:
  • It's implement can decouple with plan cache
  • It can still cache the parsed *regexp.Regexp when plan cache is on and regex pattern is read from a variable

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

We did a benchmark test and the performance of regexp_substr increased:

func BenchmarkRegexpSubStr(b *testing.B) {
	store := testkit.CreateMockStore(b)
	tk := testkit.NewTestKit(b, store)
	tk.MustExec("use test")
	tk.MustExec("create table t(a varchar(256))")
	for i := 0; i < b.N; i++ {
		tk.MustExec(fmt.Sprintf("insert into t values('pingcaper%d@pingcap.com')", i))
	}
	tk.MustExec(`set @regexp='^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$'`)
	tk.MustExec("prepare s from 'select regexp_substr(a, ?) from t'")

	b.ResetTimer()
	tk.MustQuery("execute s using @regexp")
	b.StopTimer()
}

Master:

go test -bench='^BenchmarkRegexpSubStr$' -run='^$' -benchtime=1000000x --tags=intest
...
 1000000	      1804 ns/op
PASS
ok  	github.com/pingcap/tidb/pkg/expression/integration_test	64.611s

This PR:

go test -bench='^BenchmarkRegexpSubStr$' -run='^$' -benchtime=1000000x --tags=intest
...
 1000000	       333.3 ns/op
PASS
ok  	github.com/pingcap/tidb/pkg/expression/integration_test	64.772s

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

improve the performance of function `regexp_substr` in some cases when plan cache enabled

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 19, 2023
@lcwangchao lcwangchao force-pushed the regexp_replace branch 2 times, most recently from 83c572d to 7e6ed39 Compare December 19, 2023 07:55
Copy link

codecov bot commented Dec 19, 2023

Codecov Report

Merging #49584 (0879ea0) into master (fb9d220) will increase coverage by 0.5730%.
Report is 26 commits behind head on master.
The diff coverage is 63.8888%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #49584        +/-   ##
================================================
+ Coverage   71.0039%   71.5769%   +0.5730%     
================================================
  Files          1368       1423        +55     
  Lines        399378     420521     +21143     
================================================
+ Hits         283574     300996     +17422     
- Misses        96012     100609      +4597     
+ Partials      19792      18916       -876     
Flag Coverage Δ
integration 43.8174% <63.8888%> (?)
unit 71.0041% <ø> (+0.0002%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 53.9663% <ø> (ø)
parser ∅ <ø> (∅)
br 47.2731% <ø> (-5.6218%) ⬇️

@lcwangchao
Copy link
Collaborator Author

/retest

@lcwangchao
Copy link
Collaborator Author

/retest

func (c *builtinFuncCache[T]) getOrInitCache(ctx EvalContext, constructCache func() (T, error)) (T, error) {
intest.Assert(constructCache != nil)
ctxID := ctx.GetSessionVars().StmtCtx.CtxID()
if item, ok := c.getCache(ctxID); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we double check the cache outside of the lock protection?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To achieve a better performance, ensure a concurrent call of getOrInitCache will not block each other.

Copy link
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 20, 2023
Copy link
Contributor

@xzhangxian1008 xzhangxian1008 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

ti-chi-bot bot commented Dec 20, 2023

@xzhangxian1008: adding LGTM is restricted to approvers and reviewers in OWNERS files.

In response to this:

LGTM

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

ti-chi-bot bot commented Dec 20, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: XuHuaiyu, xzhangxian1008, YangKeao

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added approved lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 20, 2023
Copy link

ti-chi-bot bot commented Dec 20, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-12-20 09:20:53.99525382 +0000 UTC m=+1039145.032480749: ☑️ agreed by YangKeao.
  • 2023-12-20 10:09:08.225141316 +0000 UTC m=+1042039.262368243: ☑️ agreed by XuHuaiyu.

@ti-chi-bot ti-chi-bot bot merged commit 97acf71 into pingcap:master Dec 20, 2023
@lcwangchao lcwangchao deleted the regexp_replace branch December 20, 2023 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor cache implement for function regexp_substr
4 participants