panjf2000 / ants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
https://ants.andypan.me/
MIT License
12.83k stars 1.36k forks source link

refactor: enforce a few minor optimization in code #302

Closed POABOB closed 12 months ago

POABOB commented 12 months ago

name: Pull request about: Propose changes to the code title: 'make the code cleaner in worker_loop_queue.go and worker_stack.go' labels: '' assignees: ''

1. Are you opening this pull request for bug-fixs, optimizations or new feature?

refactor

2. Please describe how these code changes achieve your intention.

I noticed that the original code uses the if statement to determine if the queue is overflowing in worker_loop_queue.go. I think it can use the modulo operator to make it cleaner. before

wq.head++
if wq.head == wq.size {
    wq.head = 0
}

after

wq.head = (wq.head + 1) % wq.size

And there are two different calculation methods for binary search of the mid variable in worker_loop_queue.go & worker_stack.go. I think l + ((r - l) >> 1) is a better way. worker_loop_queue.go

mid = l + ((r - l) >> 1)

worker_stack.go

mid := int(uint(l+r) >> 1) // avoid overflow when computing mid

Last, I think it can reuse the isEmpty() method to evaluate the condition below.

before

if wq.size == 0 {
    return 0
}

if wq.head == wq.tail {
    if wq.isFull {
        return wq.size
    }
    return 0
}

after

if wq.size == 0 || wq.isEmpty() {
    return 0
}

if wq.head == wq.tail && wq.isFull {
    return wq.size
}

3. Please link to the relevant issues (if any).

No

4. Which documentation changes (if any) need to be made/updated because of this PR?

No

4. Checklist

codecov[bot] commented 12 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (f0b98c3) 94.08% compared to head (9a26314) 94.43%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #302 +/- ## ========================================== + Coverage 94.08% 94.43% +0.35% ========================================== Files 9 9 Lines 744 737 -7 ========================================== - Hits 700 696 -4 + Misses 32 30 -2 + Partials 12 11 -1 ``` | [Flag](https://app.codecov.io/gh/panjf2000/ants/pull/302/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andy+Pan) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/panjf2000/ants/pull/302/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andy+Pan) | `94.43% <100.00%> (+0.35%)` | :arrow_up: | 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=Andy+Pan#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files](https://app.codecov.io/gh/panjf2000/ants/pull/302?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andy+Pan) | Coverage Δ | | |---|---|---| | [worker\_loop\_queue.go](https://app.codecov.io/gh/panjf2000/ants/pull/302?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andy+Pan#diff-d29ya2VyX2xvb3BfcXVldWUuZ28=) | `97.08% <100.00%> (+2.54%)` | :arrow_up: | | [worker\_stack.go](https://app.codecov.io/gh/panjf2000/ants/pull/302?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andy+Pan#diff-d29ya2VyX3N0YWNrLmdv) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

panjf2000 commented 12 months ago

Thanks!