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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Numerous small performance and correctness issues #211

Merged
merged 2 commits into from Aug 16, 2021

Conversation

dpcollins-google
Copy link
Collaborator

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 馃

@dpcollins-google dpcollins-google requested review from a team as code owners August 15, 2021 13:52
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 15, 2021
@product-auto-label product-auto-label bot added the api: pubsublite Issues related to the googleapis/python-pubsublite API. label Aug 15, 2021
_Key = TypeVar("_Key")
_Client = TypeVar("_Client")


class ClientMultiplexer(Generic[_Key, _Client]):
_OpenedClientFactory = Callable[[], _Client]
_OpenedClientFactory = Callable[[_Key], _Client]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There was a lot of "self" time in get_or_create callers from constructing the factory to pass in. This is in the publish hot path, hence this change

_closer: _ClientCloser
_lock: asyncio.Lock
_live_clients: Dict[_Key, _Client]
_live_clients: Dict[_Key, Awaitable[_Client]]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There was a lot of time in acquiring the lock in the publish hotpath- this change makes the lock not necessary

@@ -40,22 +38,16 @@ class MultiplexedSubscriberClient(SubscriberClientInterface):
_executor: ThreadPoolExecutor
_underlying_factory: AsyncSubscriberFactory

_multiplexer: ClientMultiplexer[SubscriptionPath, StreamingPullFuture]
_lock: Lock
_live_clients: Set[StreamingPullFuture]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previously, the subscriber enforced that there was only one open subscription stream per-subscription per-client, but there's actually no need for this.

return item.response_future

def should_flush(self) -> bool:
return self._tester.test(item.request for item in self._requests)
def size(self) -> BatchSize:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most of the CPU in the publish path was in should_flush calls, which had to construct an iterable and iterate through every time.

element_count=1, byte_count=PubSubMessage.pb(request).ByteSize()
)

def _should_flush(self) -> bool:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if you'll note on the left... batching settings were previously ignored

@dpcollins-google dpcollins-google added the automerge Merge the pull request once unit tests and other checks pass. label Aug 16, 2021
@gcf-merge-on-green gcf-merge-on-green bot merged commit 358a1d8 into master Aug 16, 2021
@gcf-merge-on-green gcf-merge-on-green bot deleted the optimize-python branch August 16, 2021 14:16
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsublite Issues related to the googleapis/python-pubsublite API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants