Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Efficient serialization of generically-typed structure

former_member186608
Active Participant
0 Likes
545

Hi people,

in my code I am dealing with a structure of a generic type, that means at design time I have no information about its particular components and their actual values. My aim is to serialize this structure into a string as fast as possible. All values of the components shall be separated by a certain delimiter. The kind of delimiter and the drawbacks of using delimiters in general shall not be discussed here.

My approach looks as follows:

The field symbol <ls_untyped_data> points to the generically-typed structure. lv_number_of_fields says how many components the structure has.

I use the approach of targeting to the component by its index, since this is faster according to the ABAP documentation than referring to the component name. If lv_counter equals 1, we are in the first loop cycle and simply add the value of the first component. If lv_Counter is > 1, we additionally add the field_separator.

lv_counter = 0.

DO lv_number_of_fields TIMES.

    ADD 1 TO lv_counter.

    ASSIGN COMPONENT lv_counter OF STRUCTURE <ls_untyped_data> TO <lv_field_value>.

    IF lv_counter > 1.

         mv_serialized_records = mv_serialized_records && lv_field_separator && <lv_field_value>.

    ELSEIF lv_counter = 1.

         mv_serialized_records = mv_serialized_records && <lv_field_value>.

    ENDIF.

ENDDO.

This solution works as expected. However, when we deal with a large number of components (in a particular case I have more than 300) and we deal with a huge amount of structures (that means the entire coding above is called several times for a set of structures), this approach is considerably slow.

I suspect the reason is the dynamic lookup of the component at runtime. However, I am not aware of any other possibiltiy to dynamically inspect the structure.

Any suggestions?

Thanks!

Marco

Hi people,

in my code I am dealing with a structure of a generic type, that means at design time I have no information about its particular components and their actual values. My aim is to serialize this structure into a string as fast as possible. All values of the components shall be separated by a certain delimiter. The kind of delimiter and the drawbacks of using delimiters in general shall not be discussed here.

My approach looks as follows:

The field symbol <ls_untyped_data> points to the generically-typed structure. lv_number_of_fields says how many components the structure has.

I use the approach of targeting to the component by its index, since this is faster according to the ABAP documentation than referring to the component name. If lv_counter equals 1, we are in the first loop cycle and simply add the value of the first component. If lv_Counter is > 1, we additionally add the field_separator.

lv_counter = 0.

DO lv_number_of_fields TIMES.

    ADD 1 TO lv_counter.

    ASSIGN COMPONENT lv_counter OF STRUCTURE <ls_untyped_data> TO <lv_field_value>.

    IF lv_counter > 1.

         mv_serialized_records = mv_serialized_records && lv_field_separator && <lv_field_value>.

    ELSEIF lv_counter = 1.

         mv_serialized_records = mv_serialized_records && <lv_field_value>.

    ENDIF.

ENDDO.

This solution works as expected. However, when we deal with a large number of components (in a particular case I have more than 300) and we deal with a huge amount of structures (that means the entire coding above is called several times for a set of structures), this approach is considerably slow.

I suspect the reason is the dynamic lookup of the component at runtime. However, I am not aware of any other possibiltiy to dynamically inspect the structure.

Any suggestions?

Thanks!

Marco

1 REPLY 1
Read only

Former Member
0 Likes
504

Hi Marco,

As an alternative you could try to run the transformation id on the structure in order to convert the structure to an XML string and then run a Simple or XSLT transformation in order to replace the XML tags with the custom delimiter (or do this second step with a normal search and replace).

The upside of this approach would be that you don't need any dynamic lookup of the input's structure.

I cannot state anything about the performance of this approach but you could give it a try and compare this to the approach you already have.

Regards,

Valentin