Skip to content

Proposal: Performance and Type-Safety Improvements in DocumentAdapters.from() Field Conversion Logic #3178

@hyeonjae

Description

@hyeonjae

I would like to propose an enhancement to the field conversion logic within DocumentAdapters.from(Hit<?> hit, JsonpMapper jsonpMapper) to improve both performance and type safety.

Currently, the method uses the following approach:

return new EntityAsMap().fromJson(sb.toString());

This builds a JSON string using a StringBuilder and then parses it back into an object. This pattern can lead to unnecessary performance overhead and increased GC pressure, especially when processing large numbers of fields. Additionally, the conversion may result in suboptimal type handling, as all values are initially treated as JSON strings.

Suggested Improvements

  1. Performance Optimization

    • By utilizing JsonData.to(Object.class), each field can be converted directly to its corresponding Java type (List, Map, String, Number, etc.), eliminating the need for intermediate stringification and parsing.
    • This approach is more efficient, especially for documents with many fields.
  2. Enhanced Type Safety

    • The proposed change ensures that values retain their native Java types, reducing the likelihood of type errors in later processing.
    • Unlike the current implementation, which may store values as JSON strings, the improved logic preserves the structure and types of arrays, objects, numbers, and strings.
  3. Sample Code

Map<String, Object> fieldMap = new LinkedHashMap<>();
hit.fields().forEach((key, jsonData) -> {
    fieldMap.put(key, jsonData.to(Object.class));
});
EntityAsMap hitFieldsAsMap = new EntityAsMap();
hitFieldsAsMap.putAll(fieldMap);

I have conducted initial tests comparing both approaches with respect to result correctness and performance. If you need more details or sample benchmarks, I am happy to provide them.

Please let me know if there are any concerns or considerations to be aware of during review.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions