Skip to content

Commit e1081d4

Browse files
authored
Merge pull request #1825 from codeflash-ai/codeflash/optimize-pr1199-2026-03-13T00.56.31
⚡️ Speed up method `OptimizeRequest.to_payload` by 33% in PR #1199 (`omni-java`)
2 parents 9022f9e + 588a3a9 commit e1081d4

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

codeflash/api/schemas.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ import annotations
1414

15+
import platform
1516
from dataclasses import dataclass, field
1617
from enum import Enum
1718
from typing import Any
@@ -122,10 +123,16 @@ class OptimizeRequest:
122123

123124
def to_payload(self) -> dict[str, Any]:
124125
"""Convert to API payload dict, maintaining backward compatibility."""
126+
# Cache frequently accessed attribute
127+
lang = self.language_info
128+
129+
# Build payload in one shot using local references to minimize attribute lookups.
130+
# Add language version (canonical for all languages)
131+
# Backward compat: backend still expects python_version
125132
payload = {
126133
"source_code": self.source_code,
127134
"trace_id": self.trace_id,
128-
"language": self.language_info.name,
135+
"language": lang.name,
129136
"dependency_code": self.dependency_code,
130137
"is_async": self.is_async,
131138
"n_candidates": self.n_candidates,
@@ -135,21 +142,13 @@ def to_payload(self) -> dict[str, Any]:
135142
"repo_name": self.repo_name,
136143
"current_username": self.current_username,
137144
"is_numerical_code": self.is_numerical_code,
145+
"language_version": lang.version,
146+
"python_version": (lang.version if lang.name == "python" else platform.python_version()),
138147
}
139148

140-
# Add language version (canonical for all languages)
141-
payload["language_version"] = self.language_info.version
142-
143-
# Backward compat: backend still expects python_version
144-
import platform
145-
146-
payload["python_version"] = (
147-
self.language_info.version if self.language_info.name == "python" else platform.python_version()
148-
)
149-
150149
# Module system for JS/TS
151-
if self.language_info.module_system != ModuleSystem.UNKNOWN:
152-
payload["module_system"] = self.language_info.module_system.value
150+
if lang.module_system != ModuleSystem.UNKNOWN:
151+
payload["module_system"] = lang.module_system.value
153152

154153
return payload
155154

0 commit comments

Comments
 (0)