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: Do not save long-lived unsafe.Pointer in hash join v2 #54085

Conversation

windtalker
Copy link
Contributor

@windtalker windtalker commented Jun 18, 2024

What problem does this PR solve?

Issue Number: ref #53127

Problem Summary:
In #53208, it creates a lots of long-lived unsafe.Pointer during hash join's build, a simple performance test shows lots of unsafe.Pointer will significantly increases gc time:
gc with a lots of unsafe.Pointer

[gc_test]# cat pointer_gc.go 
package main

import "runtime"
import "time"
import "fmt"
import "unsafe"

func main() {
        a := make([]byte, 1e9*8)
        b := make([]unsafe.Pointer, 1e9)
        for i := 0; i < 1e9; i++ {
                b[i] = unsafe.Pointer(&a[i*8])
        }
        for i := 0; i < 1e9; i++ {
                *(*int64)(b[i]) = int64(i)
        }
        for i := 0; i < 10; i++ {
                start := time.Now()
                runtime.GC()
                fmt.Printf("GC took %s\n", time.Since(start))
        }

        for i := 0; i < 1e9; i++ {
                if *(*int64)(b[i]) != int64(i) {
                        panic("test failed")
                }
        }

        runtime.KeepAlive(a)
        runtime.KeepAlive(b)
}
[gc_test]# ./pointer_gc 
GC took 442.535362ms
GC took 421.290733ms
GC took 442.405494ms
GC took 449.971723ms
GC took 426.559535ms
GC took 444.889418ms
GC took 468.164119ms
GC took 461.121191ms
GC took 458.241395ms
GC took 479.782243ms

gc with a lots of uintptr

[gc_test]# cat uintptr_gc.go 
package main

import "runtime"
import "time"
import "fmt"
import "unsafe"

func main() {
        a := make([]byte, 1e9*8)
        b := make([]uintptr, 1e9)
        for i := 0; i < 1e9; i++ {
                *(*unsafe.Pointer)((unsafe.Pointer)(&b[i])) = unsafe.Pointer(&a[i*8])
        }
        for i := 0; i < 1e9; i++ {
                *(*int64)((unsafe.Pointer)(&b[i])) = int64(i)
        }

        for i := 0; i < 10; i++ {
                start := time.Now()
                runtime.GC()
                fmt.Printf("GC took %s\n", time.Since(start))
        }

        for i := 0; i < 1e9; i++ {
                if *(*int64)((unsafe.Pointer)(&b[i])) != int64(i) {
                        panic("test failed")
                }
        }

        runtime.KeepAlive(a)
        runtime.KeepAlive(b)
}
[gc_test]# ./uintptr_gc 
GC took 1.818511ms
GC took 836.665µs
GC took 830.171µs
GC took 913.262µs
GC took 925.983µs
GC took 849.608µs
GC took 950.502µs
GC took 862.005µs
GC took 892.863µs
GC took 848.197µs

So this pr remove all the long-lived unsafe.Pointer during hash join v2. For the variable that used to be unsafe.Pointer, this pr use uintptr instead.

But go runtime actually forbit to convert uintptr to unsafe.Pointer directly:

https://pkg.go.dev/unsafe
Note that both conversions must appear in the same expression, with only the intervening arithmetic between them:

// INVALID: uintptr cannot be stored in variable
// before conversion back to Pointer.
u := uintptr(p)
p = unsafe.Pointer(u + offset)

this pr use the follow hack to read/write unsafe.Pointer from/to uintptr

*(*unsafe.Pointer)(unsafe.Pointer(&uintptr)) = unsafePointer
unsafePointer := *(*unsafe.Pointer)(unsafe.Pointer(&uintptr))

What changed and how does it work?

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.

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 do-not-merge/invalid-title release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 18, 2024
@windtalker windtalker changed the title Do not save long-lived unsafe.Pointer in hash join v2 executor: Do not save long-lived unsafe.Pointer in hash join v2 Jun 18, 2024
Copy link

tiprow bot commented Jun 18, 2024

Hi @windtalker. 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.

Copy link

codecov bot commented Jun 18, 2024

Codecov Report

Attention: Patch coverage is 98.07692% with 1 line in your changes missing coverage. Please review.

Project coverage is 56.3787%. Comparing base (b04931e) to head (e83583f).
Report is 11 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #54085         +/-   ##
=================================================
- Coverage   72.8368%   56.3787%   -16.4582%     
=================================================
  Files          1533       1678        +145     
  Lines        436025     620750     +184725     
=================================================
+ Hits         317587     349971      +32384     
- Misses        98798     246771     +147973     
- Partials      19640      24008       +4368     
Flag Coverage Δ
integration 38.7106% <9.6153%> (?)
unit 71.9562% <98.0769%> (+0.1315%) ⬆️

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

Components Coverage Δ
dumpling 52.9656% <ø> (ø)
parser ∅ <ø> (∅)
br 56.8358% <ø> (+10.7531%) ⬆️

@windtalker windtalker force-pushed the hash_join_v2_use_uintptr_to_save_unsafe_pointer branch 2 times, most recently from 29f60a5 to 76f214f Compare June 20, 2024 08:16
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 20, 2024
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jun 26, 2024
windtalker and others added 7 commits July 3, 2024 10:09
Signed-off-by: xufei <[email protected]>
Signed-off-by: xufei <[email protected]>
Signed-off-by: xufei <[email protected]>
Signed-off-by: xufei <[email protected]>
Signed-off-by: xufei <[email protected]>
@windtalker windtalker force-pushed the hash_join_v2_use_uintptr_to_save_unsafe_pointer branch from 76f214f to e83583f Compare July 3, 2024 02:11
Copy link

ti-chi-bot bot commented Jul 3, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wshwsh12, XuHuaiyu

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 lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 3, 2024
Copy link

ti-chi-bot bot commented Jul 3, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-06-26 06:36:21.592468073 +0000 UTC m=+787908.077956900: ☑️ agreed by wshwsh12.
  • 2024-07-03 06:54:02.865730757 +0000 UTC m=+1393769.351219591: ☑️ agreed by XuHuaiyu.

@windtalker
Copy link
Contributor Author

/test check-dev2

Copy link

tiprow bot commented Jul 3, 2024

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

In response to this:

/test check-dev2

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.

@windtalker
Copy link
Contributor Author

/test check-dev2

Copy link

tiprow bot commented Jul 3, 2024

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

In response to this:

/test check-dev2

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.

@windtalker
Copy link
Contributor Author

/test unit-test

@windtalker
Copy link
Contributor Author

/test check-dev2

Copy link

tiprow bot commented Jul 3, 2024

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

In response to this:

/test unit-test

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

tiprow bot commented Jul 3, 2024

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

In response to this:

/test check-dev2

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.

@windtalker
Copy link
Contributor Author

/test unit-test

@windtalker
Copy link
Contributor Author

/test check-dev2

Copy link

tiprow bot commented Jul 3, 2024

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

In response to this:

/test unit-test

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

tiprow bot commented Jul 3, 2024

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

In response to this:

/test check-dev2

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.

@ti-chi-bot ti-chi-bot bot merged commit 7192413 into pingcap:master Jul 3, 2024
23 checks passed
@windtalker windtalker deleted the hash_join_v2_use_uintptr_to_save_unsafe_pointer branch July 22, 2024 02:06
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/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.

3 participants