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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add caching to routing header calculation #526

Merged
merged 15 commits into from
Oct 9, 2023

Conversation

daniel-sanche
Copy link
Contributor

_to_grpc_metadata is used to convert a tuple of string metadata into the proper GCP routing format. It is part of the hot-path of rpcs with routing info, and it adds a noticable cost to each rpc. But typically, the same values will be used for each rpc

This PR adds a functools.lru_cache wrapper, to memoize the most recent paramaters used

import timeit
import functools
from google.api_core.gapic_v1.routing_header import to_grpc_metadata

metadata = (("table_name", "projects/p/tables/my-table"),)
result = timeit.timeit(lambda: to_grpc_metadata(metadata))
print(f"orig time: {result:0.2f}")

cached_to_metadata = functools.lru_cache(to_grpc_metadata)
result = timeit.timeit(lambda: cached_to_metadata(metadata))
print(f"cached time: {result:0.2f}")
orig time: 1.19
cached time: 0.07

@daniel-sanche daniel-sanche requested review from a team as code owners September 6, 2023 22:46
@product-auto-label product-auto-label bot added the size: xs Pull request size is extra small. label Sep 6, 2023
@parthea
Copy link
Collaborator

parthea commented Sep 19, 2023

Converting to draft as presubmits are failing

@parthea parthea marked this pull request as draft September 19, 2023 16:55
@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: xs Pull request size is extra small. labels Oct 6, 2023
@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Oct 6, 2023
@daniel-sanche daniel-sanche marked this pull request as ready for review October 6, 2023 23:00
@parthea parthea self-assigned this Oct 8, 2023
Copy link
Collaborator

@parthea parthea left a comment

Choose a reason for hiding this comment

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

Added minor observations, otherwise LGTM

@parthea parthea assigned daniel-sanche and unassigned parthea Oct 9, 2023
@daniel-sanche daniel-sanche merged commit 6251eab into main Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants