Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

feat: add caching to routing header calculation - #526

Merged
daniel-sanche merged 15 commits into
mainfrom
routing-header-cache
Oct 9, 2023
Merged

feat: add caching to routing header calculation#526
daniel-sanche merged 15 commits into
mainfrom
routing-header-cache

Conversation

@daniel-sanche

Copy link
Copy Markdown
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 teamSeptember 6, 2023 22:46
@product-auto-labelproduct-auto-labelBot added the size: xs Pull request size is extra small. label Sep 6, 2023
@parthea

Copy link
Copy Markdown
Contributor

Converting to draft as presubmits are failing

@parthea
parthea marked this pull request as draft September 19, 2023 16:55
@product-auto-labelproduct-auto-labelBot added size: s Pull request size is small. and removed size: xs Pull request size is extra small. labels Oct 6, 2023
@product-auto-labelproduct-auto-labelBot 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
@partheaparthea self-assigned this Oct 8, 2023

@partheaparthea left a comment

Copy link
Copy Markdown
Contributor

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

Comment threadtests/unit/gapic/test_routing_header.py Outdated
Comment threadtests/unit/gapic/test_routing_header.py Outdated
Comment threadgoogle/api_core/gapic_v1/routing_header.py Outdated
Comment threadgoogle/api_core/gapic_v1/routing_header.py Outdated
@partheaparthea assigned daniel-sanche and unassigned partheaOct 9, 2023
daniel-sancheand others added 7 commits October 9, 2023 12:19
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
@daniel-sanche
daniel-sanche merged commit 6251eab into mainOct 9, 2023
@daniel-sanche
daniel-sanche deleted the routing-header-cache branch October 9, 2023 20:00
@release-pleaserelease-pleaseBot mentioned this pull request Oct 9, 2023
This was referenced May 30, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: mPull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@daniel-sanche@parthea