In Mytask offline performance improvement, it is necessary to support both two DDIC structure modelled in BP and Task, that is, crmt_odata_attachment_t and crmt_bp_odata_attachment_t. It is unknown which data type will be used since they are determined by runtime according to different urls passed from frontend.
The first approach to support both structure is first trying with my task structure using line ASSIGN lr_ref->* TO CASTING. If this line fails, we can know the structure does not belong to Task model, so we can use BP structure to store result.
In this approach, we have defined two variables with separate field symbol with data type crmt_odata_attachment_t and crmt_bp_odata_attachment_t to hold result.
METHOD dynamic_fill_approach1.
DATA: lr_ref TYPE REF TO data,
ls_task TYPE crmt_odata_attachment,
ls_bp TYPE crmt_bp_odata_attachment,
lt_task TYPE crmt_odata_attachment_t,
lv_is_task TYPE abap_bool VALUE abap_true,
lt_bp TYPE crmt_bp_odata_attachment_t.
FIELD-SYMBOLS: TYPE zcrm_task_attachment_t,
TYPE crmt_odata_attachment_t,
TYPE crmt_bp_odata_attachment_t.
GET REFERENCE OF ct_table INTO lr_ref.
TRY.
ASSIGN lr_ref->* TO CASTING.
CATCH cx_root.
lv_is_task = abap_false.
ENDTRY.
LOOP AT ct_table ASSIGNING FIELD-SYMBOL().
IF lv_is_task = abap_true.
ASSIGN COMPONENT 'ATTACHMENT' OF STRUCTURE TO .
ls_task-name = sy-index.
ls_task-created_by = sy-uname.
ls_task-mime_type = 'text/html'.
ls_task-documentid = ls_task-header_guid = get_guid_by_index( sy-tabix ).
APPEND ls_task TO .
ELSE.
ASSIGN COMPONENT 'ATTACHMENT' OF STRUCTURE TO .
ls_bp-name = sy-index.
ls_bp-created_by = sy-uname.
ls_bp-mime_type = 'text/html'.
ls_bp-documentid = get_guid_by_index( sy-tabix ).
APPEND ls_bp TO .
ENDIF.
ENDLOOP.
ENDMETHOD.
In approach2, we use generic programming style, neither dedicated type for BP nor for Task is defined in the code. Instead, “ANY TABLE” is defined.
METHOD dynamic_fill_approach2.
DATA:lr_attachment TYPE REF TO data,
ls_task TYPE crmt_odata_attachment,
lt_task TYPE crmt_odata_attachment_t.
FIELD-SYMBOLS: TYPE crmt_odata_attachment_t,
TYPE ANY TABLE,
TYPE ANY TABLE.
CREATE DATA lr_attachment LIKE lt_task.
ASSIGN lr_attachment->* TO .
LOOP AT ct_table ASSIGNING FIELD-SYMBOL().
ASSIGN COMPONENT 'ATTACHMENT' OF STRUCTURE TO .
CLEAR: lt_task.
ls_task-name = sy-index.
ls_task-created_by = sy-uname.
ls_task-mime_type = 'text/html'.
ls_task-documentid = ls_task-header_guid = get_guid_by_index( sy-tabix ).
APPEND ls_task TO lt_task.
* Jerry 2016-01-29 19:10PM this is used to simulate that every task has an internal table
* to store multiple attachments
= lt_task.
MOVE-CORRESPONDING TO .
ENDLOOP.
ENDMETHOD.
In ABAP help, it is said we should avoid generic programming unless it is really necessary from security and performance perspective. In this case, I would like to know the performance loss if I use approach2 compared with approach1.
Then I write the following report to get answer:
PARAMETERS: num TYPE int4 OBLIGATORY DEFAULT 10.
DATA(lo_tool) = NEW zcl_crm_attachment_tool( ).
DATA: lt_task1 TYPE zcrm_task_attachment_t,
lt_bp1 TYPE zcrm_bp_attachment_t,
lt_task2 LIKE lt_task1,
lt_bp2 LIKE lt_bp1.
CALL METHOD lo_tool->prefill_attachment_tab
EXPORTING
iv_num = num
CHANGING
ct_bp1 = lt_bp1
ct_bp2 = lt_bp2
ct_task1 = lt_task1
ct_task2 = lt_Task2.
lo_tool->start( ).
lo_tool->dynamic_fill_approach1( CHANGING ct_table = lt_task1 ).
lo_tool->dynamic_fill_approach1( CHANGING ct_table = lt_bp1 ).
lo_tool->stop( 'static assign performance: ').
lo_tool->start( ).
lo_tool->dynamic_fill_approach2( CHANGING ct_table = lt_task2 ).
lo_tool->dynamic_fill_approach2( CHANGING ct_table = lt_bp2 ).
lo_tool->stop( 'dynamic assign performance: ').
CHECK lo_tool->compare_bp( it_bp1 = lt_bp1 it_bp2 = lt_bp2 ).
CHECK lo_tool->compare_task( it_task1 = lt_task1 it_task2 = lt_task2 ).
WRITE: / 'attachment number: ' COLOR COL_POSITIVE,
lines( lt_bp1 ) COLOR COL_GROUP , ' equal' COLOR COL_NEGATIVE.
When handling with 10 tasks: performance difference: 300 microseconds
100 tasks: approach1 is one time faster than approach2
500 tasks:
1000 tasks:
10000 tasks:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":