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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

windtalker
Copy link
Contributor

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. And go runtime actually forbit to convert uintptr to unsafe.Pointer directly, 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

windtalker and others added 2 commits June 18, 2024 13:19
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufei@pingcap.com>
Copy link

ti-chi-bot bot commented Jun 18, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from windtalker, ensuring that each of them provides their approval before proceeding. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label 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.

Signed-off-by: xufei <xufei@pingcap.com>
Signed-off-by: xufei <xufei@pingcap.com>
Copy link

codecov bot commented Jun 18, 2024

Codecov Report

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

Project coverage is 56.0568%. Comparing base (2cea994) to head (071abe5).
Report is 10 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #54085         +/-   ##
=================================================
- Coverage   74.5294%   56.0568%   -18.4727%     
=================================================
  Files          1516       1636        +120     
  Lines        360821     606559     +245738     
=================================================
+ Hits         268918     340018      +71100     
- Misses        72393     243245     +170852     
- Partials      19510      23296       +3786     
Flag Coverage Δ
integration 37.1945% <7.8431%> (?)
unit 71.7836% <98.0392%> (-1.6256%) ⬇️

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

Components Coverage Δ
dumpling 52.9656% <ø> (-2.2339%) ⬇️
parser ∅ <ø> (∅)
br 52.2898% <ø> (+8.3175%) ⬆️

Signed-off-by: xufei <xufei@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant