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

table: Update delta from column slice, instead of map #53670

Merged
merged 9 commits into from
Jun 1, 2024

Conversation

ekexium
Copy link
Contributor

@ekexium ekexium commented May 30, 2024

What problem does this PR solve?

Issue Number: close #53660

Problem Summary:

AddRecord/UpdateRecord/RemoveRecord is in the hot path for writing. Iterating over a map when they call UpdateDeltaForTable is slow.

What changed and how does it work?

Replace the map with a slice.

Before:

ROUTINE ======================== github.com/pingcap/tidb/pkg/sessionctx/variable.(*TransactionContext).UpdateDeltaForTable in /Users/eke/code/tidb/pkg/sessionctx/variable/session.go
      30ms      420ms (flat, cum)  2.04% of Total
         .          .    309:func (tc *TransactionContext) UpdateDeltaForTable(physicalTableID int64, delta int64, count int64, colSize map[int64]int64) {
         .          .    310:	tc.tdmLock.Lock()
         .          .    311:	defer tc.tdmLock.Unlock()
         .          .    312:	if tc.TableDeltaMap == nil {
         .          .    313:		tc.TableDeltaMap = make(map[int64]TableDelta)
         .          .    314:	}
         .          .    315:	item := tc.TableDeltaMap[physicalTableID]
         .          .    316:	if item.ColSize == nil && colSize != nil {
         .          .    317:		item.ColSize = make(map[int64]int64, len(colSize))
         .          .    318:	}
         .          .    319:	item.Delta += delta
         .          .    320:	item.Count += count
         .          .    321:	item.TableID = physicalTableID
      10ms      350ms    322:	for key, val := range colSize {
         .       20ms    323:		item.ColSize[key] += val
         .          .    324:	}
         .       30ms    325:	tc.TableDeltaMap[physicalTableID] = item
      20ms       20ms    326:}
         .          .    327:
         .          .    328:// GetKeyInPessimisticLockCache gets a key in pessimistic lock cache.
         .          .    329:func (tc *TransactionContext) GetKeyInPessimisticLockCache(key kv.Key) (val []byte, ok bool) {
         .          .    330:	if tc.pessimisticLockCache == nil && tc.CurrentStmtPessimisticLockCache == nil {
         .          .    331:		return nil, false

This PR:

ROUTINE ======================== github.com/pingcap/tidb/pkg/sessionctx/variable.(*TransactionContext).UpdateDeltaForTableFromColSlice in /Users/eke/code/tidb/pkg/sessionctx/variable/session.go
      10ms       30ms (flat, cum)  0.13% of Total
         .          .    334:func (tc *TransactionContext) UpdateDeltaForTableFromColSlice(
         .          .    335:	physicalTableID int64, delta int64,
         .          .    336:	count int64, colSizes []ColSize,
         .          .    337:) {
         .          .    338:	tc.tdmLock.Lock()
         .          .    339:	defer tc.tdmLock.Unlock()
         .          .    340:	if tc.TableDeltaMap == nil {
         .          .    341:		tc.TableDeltaMap = make(map[int64]TableDelta)
         .          .    342:	}
         .       10ms    343:	item := tc.TableDeltaMap[physicalTableID]
         .          .    344:	if item.ColSize == nil && colSizes != nil {
         .          .    345:		item.ColSize = make(map[int64]int64, len(colSizes))
         .          .    346:	}
         .          .    347:	item.Delta += delta
         .          .    348:	item.Count += count
         .          .    349:	item.TableID = physicalTableID
         .          .    350:	for _, s := range colSizes {
         .       10ms    351:		item.ColSize[s.ColId] += s.Size
         .          .    352:	}
         .          .    353:	tc.TableDeltaMap[physicalTableID] = item
      10ms       10ms    354:}
         .          .    355:
         .          .    356:// GetKeyInPessimisticLockCache gets a key in pessimistic lock cache.
         .          .    357:func (tc *TransactionContext) GetKeyInPessimisticLockCache(key kv.Key) (val []byte, ok bool) {
         .          .    358:	if tc.pessimisticLockCache == nil && tc.CurrentStmtPessimisticLockCache == nil {
         .          .    359:		return nil, false

Check List

Tests

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

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.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 30, 2024
Copy link

tiprow bot commented May 30, 2024

Hi @ekexium. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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-sigs/prow repository.

Signed-off-by: ekexium <eke@fastmail.com>
@ekexium ekexium force-pushed the update-delta-from-col-slice branch from d49d9e4 to 0c599f0 Compare May 30, 2024 03:46
Copy link

codecov bot commented May 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.7496%. Comparing base (04c66ee) to head (74c936a).
Report is 12 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #53670        +/-   ##
================================================
+ Coverage   74.4884%   74.7496%   +0.2612%     
================================================
  Files          1506       1506                
  Lines        357618     433103     +75485     
================================================
+ Hits         266384     323743     +57359     
- Misses        71857      89496     +17639     
- Partials      19377      19864       +487     
Flag Coverage Δ
integration 49.6159% <100.0000%> (?)
unit 71.6483% <100.0000%> (-1.7190%) ⬇️

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

Components Coverage Δ
dumpling 53.9957% <ø> (-2.3014%) ⬇️
parser ∅ <ø> (∅)
br 50.3524% <ø> (+6.7889%) ⬆️

@ekexium
Copy link
Contributor Author

ekexium commented May 30, 2024

/retest

@ekexium ekexium requested review from you06 and cfzjywxk May 30, 2024 04:14
Copy link

tiprow bot commented May 30, 2024

@ekexium: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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-sigs/prow repository.

go.mod Outdated Show resolved Hide resolved
Signed-off-by: ekexium <eke@fastmail.com>
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 30, 2024
go.mod Outdated Show resolved Hide resolved
pkg/sessionctx/variable/session.go Outdated Show resolved Hide resolved
@@ -670,7 +670,7 @@ func (t *TableCommon) UpdateRecord(ctx context.Context, sctx table.MutateContext
return err
}
}
colSize := make(map[int64]int64, len(t.Cols()))
colSize := make([]variable.ColSize, len(t.Cols()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch, we could check if there similar implemetations in other performance critical paths.

Signed-off-by: ekexium <eke@fastmail.com>
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 30, 2024
Signed-off-by: ekexium <eke@fastmail.com>
Signed-off-by: ekexium <eke@fastmail.com>
Signed-off-by: ekexium <eke@fastmail.com>
@ekexium
Copy link
Contributor Author

ekexium commented May 31, 2024

/retest

Copy link

tiprow bot commented May 31, 2024

@ekexium: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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-sigs/prow repository.

Copy link
Contributor

@cfzjywxk cfzjywxk 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 May 31, 2024
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 31, 2024
Copy link

ti-chi-bot bot commented May 31, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-05-31 01:52:03.524371615 +0000 UTC m=+3000477.281507189: ☑️ agreed by cfzjywxk.
  • 2024-05-31 03:08:57.270659714 +0000 UTC m=+3005091.027795288: ☑️ agreed by zyguan.

@ekexium
Copy link
Contributor Author

ekexium commented May 31, 2024

@yudongusa PTAL

@cfzjywxk
Copy link
Contributor

@ekexium
Need approvement from variable file owners.

Copy link

@yudongusa yudongusa 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 Jun 1, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, lcwangchao, you06, yudongusa, zyguan

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 the approved label Jun 1, 2024
@ti-chi-bot ti-chi-bot bot merged commit 8483dc5 into pingcap:master Jun 1, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TransactionContext.UpdateDeltaForTable is inefficient
7 participants