Skip to content

Commit 896a6fc

Browse files
committed
Avoid constructing workflow-context payload codec twice
1 parent b904919 commit 896a6fc

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

temporalio/worker/_workflow.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,11 @@ async def _handle_activation(
277277
"Cache already exists for activation with initialize job"
278278
)
279279

280-
data_converter = self._data_converter.with_context(
281-
temporalio.converter.WorkflowSerializationContext(
282-
namespace=self._namespace,
283-
workflow_id=workflow_id,
284-
)
280+
workflow_context = temporalio.converter.WorkflowSerializationContext(
281+
namespace=self._namespace,
282+
workflow_id=workflow_id,
285283
)
284+
data_converter = self._data_converter.with_context(workflow_context)
286285
if self._data_converter.payload_codec:
287286
assert data_converter.payload_codec
288287
if not workflow:
@@ -291,6 +290,8 @@ async def _handle_activation(
291290
payload_codec = _CommandAwarePayloadCodec(
292291
workflow.instance,
293292
context_free_payload_codec=self._data_converter.payload_codec,
293+
workflow_context_payload_codec=data_converter.payload_codec,
294+
workflow_context=workflow_context,
294295
)
295296
await temporalio.bridge.worker.decode_activation(
296297
act,
@@ -367,6 +368,11 @@ async def _handle_activation(
367368
payload_codec = _CommandAwarePayloadCodec(
368369
workflow.instance,
369370
context_free_payload_codec=self._data_converter.payload_codec,
371+
workflow_context_payload_codec=data_converter.payload_codec,
372+
workflow_context=temporalio.converter.WorkflowSerializationContext(
373+
namespace=self._namespace,
374+
workflow_id=workflow.workflow_id,
375+
),
370376
)
371377
try:
372378
await temporalio.bridge.worker.encode_completion(
@@ -733,6 +739,8 @@ class _CommandAwarePayloadCodec(temporalio.converter.PayloadCodec):
733739

734740
instance: WorkflowInstance
735741
context_free_payload_codec: temporalio.converter.PayloadCodec
742+
workflow_context_payload_codec: temporalio.converter.PayloadCodec
743+
workflow_context: temporalio.converter.WorkflowSerializationContext
736744

737745
async def encode(
738746
self,
@@ -756,6 +764,8 @@ def _get_current_command_codec(self) -> temporalio.converter.PayloadCodec:
756764
if context := self.instance.get_serialization_context(
757765
_command_aware_visitor.current_command_info.get(),
758766
):
767+
if context == self.workflow_context:
768+
return self.workflow_context_payload_codec
759769
return self.context_free_payload_codec.with_context(context)
760770

761771
return self.context_free_payload_codec

0 commit comments

Comments
 (0)