improve python performance - #73
jacopocinaark wants to merge 4 commits into
Conversation
ref: #22400
There was a problem hiding this comment.
🟡 Changes recommended
Preserve jsons key encoding to avoid breaking non-primitive and enum key deserialization.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Improves Python performance by optimizing Artesian JSON dictionary serialization.
Changes:
- Adds fast paths for primitive and datetime values.
- Simplifies dictionary iteration and deserialization handling.
File summaries
| File | Summary |
|---|---|
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py |
Critical (3 votes): str(k) changes established jsons encoding for non-primitive keys, breaking enum round trips. Retain the jsons.dump fallback for this branch. |
Review details
Suppressed comments (1)
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:70
- This fast path also catches subclasses of the scalar types. An
IntEnumis anint, so returning it here bypasses the configureduse_enum_name=Trueserializer and emits its numeric value instead of its enum name. Restrict the shortcut to exact scalar types (or explicitly exclude Enum) so subclasses still go throughjsons.
elif isinstance(v, (str, int, float, bool)) or v is None:
v_out = v
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| elif isinstance(k, (str, int, float, bool)) or k is None: | ||
| k_out = k | ||
| else: | ||
| k_out = str(k) |
There was a problem hiding this comment.
🔵 Needs a closer look
Restore jsons.dump for non-primitive keys to preserve configured serializers.
Review details
Suppressed comments (1)
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:64
- This replaces the existing
jsons.dump(key, key_transformer=None, **kwargs)fallback withstr(k), so non-primitive keys no longer use the configured serializers. For example, anEnumkey is emitted asPurpose.BIDinstead of the enum-name behavior configured byuse_enum_name=True, and custom key serializers are skipped. Keep thejsons.dumpfallback for this branch; only the primitive/datetime fast paths should bypass it.
k_out = str(k)
- Files reviewed: 1/1 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Fast paths bypass configured jsons serialization for enum and non-primitive mapping keys.
Review details
Suppressed comments (2)
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:63
- Non-primitive mapping keys used to go through
jsons.dump, butstr(k)bypasses registered serializers and the active options. For example, anEnumkey now becomes"MyEnum.Member"instead of the configured enum name ("Member"), changing the emittedKeyand potentially breaking the API payload. Keep the jsons fallback for this branch.
else:
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:68
- This primitive fast path also matches
IntEnummembers. Returning one directly bypasses jsons'use_enum_name=True, so when the result is passed to requests' JSON encoder it is emitted as its numeric value instead of its enum name. Check enum types before this branch or keep enum values on the jsons serialization path.
v_out = dt_ser(v, **kwargs)
- Files reviewed: 2/2 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Moderate serializer issues remain unresolved.
Review details
Suppressed comments (2)
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:64
- This changes the non-primitive-key behavior from
jsons.dump(key, key_transformer=None, **kwargs)tostr(k). That bypasses registered serializers and breaks the mapping serializer/deserializer contract for supported key types such as custom objects or dates, because their serialized structure is replaced by a Python repr/string and cannot be loaded back into the declared key type. Keep the primitive/datetime fast paths, but retain thejsons.dumpfallback for other keys.
k_out = str(k)
src/Artesian/_ClientsExecutor/ArtesianJsonSerializer.py:69
- The primitive fast path also matches
IntEnumandstr-basedEnumsubclasses, returning the enum object instead of lettingjsons.dumphonor the configureduse_enum_name=True. This makesartesianJsonSerializereturn an underlying integer/string (or a non-normalized enum) for those values; restrict this shortcut to exact built-in primitive types or handle enums before it.
elif isinstance(v, (str, int, float, bool)) or v is None:
- Files reviewed: 2/2 changed files
- Comments generated: 0 new
- Review effort level: Lite
improve python performance
ref: #22400