Avoid String Concatenation Inside Loops (Quadratic Memory Allocation)
WarningPerformance & Database#EC NO_CONCAT_LOOPRemote Enabled
Overview
Avoid string concatenation inside loops.
ATC Finding Message:
Accumulate parts in a table and concatenate once
Rationale
Performing string concatenation (lv_result = lv_result && lv_chunk or CONCATENATE) inside a loop causes quadratic memory allocation ($O(N^2)$). In each iteration, ABAP allocates a new heap memory buffer, copies the entire existing accumulator plus the new chunk, and marks the old buffer for garbage collection. As the iteration count increases, execution time and memory usage skyrocket.
Best Practice: Collect string fragments into an internal table of strings (string_table) and join them in a single native operation using concat_lines_of( it_strings, sep ).
Non-Compliant
abap
" Anti-pattern: Re-allocates and copies full string buffer on every iteration (O(N^2))
DATA lv_csv TYPE string.
LOOP AT lt_records INTO ls_record.
lv_csv = lv_csv && ls_record-id && ',' && ls_record-name && cl_abap_char_utilities=>newline.
ENDLOOP.Compliant
abap
" Clean ABAP: Collect lines and join in a single lightning-fast operation (O(N))
DATA lt_lines TYPE string_table.
LOOP AT lt_records INTO ls_record.
INSERT |{ ls_record-id },{ ls_record-name }| INTO TABLE lt_lines.
ENDLOOP.
DATA(lv_csv) = concat_lines_of( table = lt_lines sep = cl_abap_char_utilities=>newline ).Suppression
To suppress findings for this rule, append the pseudo-comment to the statement:
abap
" Standard pseudo-comment:
... "#EC NO_CONCAT_LOOP
" SAP CI_ prefix format:
... "#EC CI_NO_CONCAT_LOOPRelated Rules
- Do Not Mutate the Internal Table Currently Being Looped (Iterator Invalidation) —
#EC NO_MOD_LOOP_TAB - Avoid Late Access to sy-tabix or sy-index Outside Loops —
#EC NO_LATE_SYS_IDX - Specify TRANSPORTING in Internal Table MODIFY and READ Operations —
#EC PREF_TRANSPORT
Technical Details
- Native ATC Class:
ZCL_SKYLINT_NO_CONCAT_LOOP - Implementation File: zcl_skylint_no_concat_loop.clas.abap