Skip to content

refactor: enforce a few minor optimization in code - #302

Merged
panjf2000 merged 10 commits into
panjf2000:devfrom
POABOB:dev
Oct 18, 2023
Merged

panjf2000 merged 10 commits into
panjf2000:devfrom
POABOB:dev

Conversation

@POABOB

@POABOB POABOB commented Oct 18, 2023

Copy link
Copy Markdown
Contributor

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

  • I have squashed all insignificant commits.
  • I have commented my code for explaining package types, values, functions, and non-obvious lines.
  • I have written unit tests and verified that all tests passes (if needed).
  • I have documented feature info on the README (only when this PR is adding a new feature).
  • (optional) I am willing to help maintain this change if there are issues with it later.

@codecov

codecov Bot commented Oct 18, 2023 •

Copy link
Copy Markdown

Codecov Report

All modified lines are covered by tests ✅

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

Additional details and impacted files
@@            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 Coverage Δ
unittests 94.43% <100.00%> (+0.35%) ⬆️

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

Files Coverage Δ
worker_loop_queue.go 97.08% <100.00%> (+2.54%) ⬆️
worker_stack.go 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread worker_stack.go Resolved
@panjf2000 panjf2000 changed the title refactor: make the code cleaner & unify the calculation of mid in binary search. refactor: enforce a few minor optimization in code Oct 18, 2023
@panjf2000 panjf2000 added enhancement New feature or request pending merged This PR has been reviewed and approved waiting for response waiting for the response from commenter pending development Requested PR owner to improve code and waiting for the result optimization labels Oct 18, 2023

@panjf2000 panjf2000 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM

@panjf2000
panjf2000 merged commit 27685ba into panjf2000:dev Oct 18, 2023
@panjf2000

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request optimization pending development Requested PR owner to improve code and waiting for the result pending merged This PR has been reviewed and approved waiting for response waiting for the response from commenter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址