tikv / pd

Placement driver for TiKV
Apache License 2.0
1.03k stars 714 forks source link

rule: optimize `Clone()` method #8299

Closed okJiang closed 2 weeks ago

okJiang commented 2 weeks ago

What problem does this PR solve?

Issue Number: Close https://github.com/tikv/pd/issues/8298

What is changed and how does it work?

Check List

Tests

func BenchmarkCloneWithJSON(b *testing.B) {
    r := &Rule{
        GroupID:  "g1",
        ID:       "r1",
        StartKey: []byte("start"),
        EndKey:   []byte("end"),
    }
    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        var clone Rule
        json.Unmarshal([]byte(r.String()), &clone)
        clone.StartKey = append(r.StartKey[:0:0], r.StartKey...)
        clone.EndKey = append(r.EndKey[:0:0], r.EndKey...)
    }
}

func BenchmarkCloneWithAssignment(b *testing.B) {
    r := &Rule{
        GroupID:  "g1",
        ID:       "r1",
        StartKey: []byte("start"),
        EndKey:   []byte("end"),
    }
    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        clone := *r
        clone.StartKey = append(clone.StartKey[:0:0], r.StartKey...)
        clone.EndKey = append(clone.EndKey[:0:0], r.EndKey...)
    }
}
goos: darwin
goarch: arm64
pkg: github.com/tikv/pd/pkg/schedule/placement
BenchmarkCloneWithJSON-8              735308          1961 ns/op         784 B/op         12 allocs/op
BenchmarkCloneWithAssignment-8      30597870            34.23 ns/op       16 B/op          2 allocs/op
PASS

Code changes

Side effects

Related changes

Release note

None.
ti-chi-bot[bot] commented 2 weeks ago

[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 assign andremouche for approval. 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: - **[OWNERS](https://github.com/tikv/pd/blob/master/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
codecov[bot] commented 2 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 77.35%. Comparing base (463aee9) to head (f320b19). Report is 1 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #8299 +/- ## ========================================== - Coverage 77.39% 77.35% -0.04% ========================================== Files 470 470 Lines 61387 61391 +4 ========================================== - Hits 47508 47491 -17 - Misses 10315 10338 +23 + Partials 3564 3562 -2 ``` | [Flag](https://app.codecov.io/gh/tikv/pd/pull/8299/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/tikv/pd/pull/8299/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | `77.35% <100.00%> (-0.04%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#carryforward-flags-in-the-pull-request-comment) to find out more.