Skip to content

Simplify ConcurrentPool #907

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

Merged
merged 1 commit into from
Apr 11, 2022
Merged

Conversation

stIncMale
Copy link
Member

This PR mostly replaces ReentrantReadWriteLock in ConcurrentPool.StateAndPermits.lock with ReentrantLock, and simplifies StateAndPermits.throwIfClosedOrPaused.

Originally throwIfClosedOrPaused was called by threads that do not necessary held an exclusive lock, so the method had to make sure it cannot be executed concurrently with the code that modifies the StateAndPermits.paused field (we don't have the same care about the closed field because unlike paused, it is flipped at most once). However, in order to allow throwIfClosedOrPaused to be executed concurrently with itself, it acquired a shared lock.

Then I noticed that each time throwIfClosedOrPaused is called by a thread that does not hold an exclusive lock, the thread then calls another method of StateAndPermits that calls throwIfClosedOrPaused again. This observation allowed to simplify the code, because many original throwIfClosedOrPaused invocations were unnecessary.

@stIncMale stIncMale requested review from rozza and jyemin April 1, 2022 00:15
@stIncMale stIncMale self-assigned this Apr 1, 2022
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

It looks neat my simpler, I have a couple of questions:

Then I noticed that each time throwIfClosedOrPaused is called by a thread that does not hold an exclusive lock, the thread then calls another method of StateAndPermits that calls throwIfClosedOrPaused again. This observation allowed to simplify the code, because many original throwIfClosedOrPaused invocations were unnecessary.

I'm not sure I fully understand this scenario - can you elaborate.

Does using a single lock rather than a read / write lock have any performance impact.

@stIncMale
Copy link
Member Author

stIncMale commented Apr 1, 2022

Does using a single lock rather than a read / write lock have any performance impact.

I guess one may say that it generally does have a negative effect. However, in our case we simply had the method throwIfClosedOrPaused called in multiple places without holding the write lock, and then (in the program order) we called it again while holding the write lock. Clearly, one of such calls that were done one after the other was redundant, and I removed the one that was done without holding the write lock. This reduced the amount of work the pool does, which means that theoretically it improved its performance, though I highly doubt the change is noticeable given the rest of the work. The actual benefit of the change is that the code became simpler by loosing useless pieces.

Ross, does the above clarifies the change for you?

Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

Thanks for the clarifications - LGTM!

@stIncMale stIncMale merged commit af39665 into mongodb:master Apr 11, 2022
@stIncMale stIncMale deleted the simplifyConcurrentPool branch April 11, 2022 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants