Skip to content

Commit

Permalink
minor optimizations (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp authored Jan 7, 2024
1 parent 1bdfd58 commit 3b5f62b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 62 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
},
"license": "MIT",
"dependencies": {
"@abaplint/cli": "^2.105.6",
"@abaplint/runtime": "^2.7.138",
"@abaplint/cli": "^2.105.8",
"@abaplint/runtime": "^2.7.140",
"@abaplint/database-sqlite": "^2.7.119",
"@abaplint/transpiler-cli": "^2.7.138"
"@abaplint/transpiler-cli": "^2.7.140"
}
}
23 changes: 12 additions & 11 deletions src/json/#ui2#cl_json.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ CLASS /ui2/cl_json IMPLEMENTATION.
DATA lo_type TYPE REF TO cl_abap_typedescr.
DATA lo_struct TYPE REF TO cl_abap_structdescr.
DATA lt_components TYPE cl_abap_structdescr=>component_table.
DATA ls_component LIKE LINE OF lt_components.
DATA ref TYPE REF TO data.
DATA lv_index TYPE i.

FIELD-SYMBOLS <ls_component> LIKE LINE OF lt_components.
FIELD-SYMBOLS <any> TYPE any.
FIELD-SYMBOLS <tab> TYPE ANY TABLE.

Expand Down Expand Up @@ -132,18 +132,18 @@ CLASS /ui2/cl_json IMPLEMENTATION.
lo_struct ?= lo_type.
lt_components = lo_struct->get_components( ).
r_json = '{'.
LOOP AT lt_components INTO ls_component.
ASSIGN COMPONENT ls_component-name OF STRUCTURE data TO <any>.
LOOP AT lt_components ASSIGNING <ls_component>.
ASSIGN COMPONENT <ls_component>-name OF STRUCTURE data TO <any>.
ASSERT sy-subrc = 0.
IF compress = abap_true AND <any> IS INITIAL.
CONTINUE.
ENDIF.
IF pretty_name = pretty_mode-camel_case.
r_json = r_json && |"{ to_mixed( to_lower( ls_component-name ) ) }":|.
r_json = r_json && |"{ to_mixed( to_lower( <ls_component>-name ) ) }":|.
ELSEIF pretty_name = pretty_mode-low_case.
r_json = r_json && |"{ to_lower( ls_component-name ) }":|.
r_json = r_json && |"{ to_lower( <ls_component>-name ) }":|.
ELSE.
r_json = r_json && |"{ ls_component-name }":|.
r_json = r_json && |"{ <ls_component>-name }":|.
ENDIF.
r_json = r_json && serialize(
data = <any>
Expand Down Expand Up @@ -214,6 +214,7 @@ CLASS /ui2/cl_json IMPLEMENTATION.
DATA lv_member LIKE LINE OF lt_members.

FIELD-SYMBOLS <any> TYPE any.
FIELD-SYMBOLS <ls_component> LIKE LINE OF lt_components.

prefix = mo_parsed->find_ignore_case( prefix ).

Expand Down Expand Up @@ -272,21 +273,21 @@ CLASS /ui2/cl_json IMPLEMENTATION.
WHEN cl_abap_typedescr=>kind_struct.
lo_struct ?= io_type.
lt_components = lo_struct->get_components( ).
LOOP AT lt_components INTO ls_component.
ASSIGN COMPONENT ls_component-name OF STRUCTURE data TO <any>.
LOOP AT lt_components ASSIGNING <ls_component>.
ASSIGN COMPONENT <ls_component>-name OF STRUCTURE data TO <any>.
ASSERT sy-subrc = 0.
CASE pretty_name.
WHEN pretty_mode-camel_case.
lv_name = to_mixed( to_lower( ls_component-name ) ).
lv_name = to_mixed( to_lower( <ls_component>-name ) ).
WHEN OTHERS.
lv_name = to_lower( ls_component-name ).
lv_name = to_lower( <ls_component>-name ).
ENDCASE.
" WRITE '@KERNEL console.dir("structure: " + lv_name.get());'.
_deserialize(
EXPORTING
prefix = prefix && '/' && lv_name
pretty_name = pretty_name
io_type = ls_component-type
io_type = <ls_component>-type
CHANGING
data = <any> ).
ENDLOOP.
Expand Down
24 changes: 14 additions & 10 deletions src/kernel/kernel_create_data_handle.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,35 @@ CLASS kernel_create_data_handle IMPLEMENTATION.
METHOD struct.
DATA lo_struct TYPE REF TO cl_abap_structdescr.
DATA lt_components TYPE cl_abap_structdescr=>component_table.
DATA ls_component LIKE LINE OF lt_components.
DATA field TYPE REF TO data.
DATA lv_name TYPE string.

FIELD-SYMBOLS <ls_component> LIKE LINE OF lt_components.

lo_struct ?= handle.
lt_components = lo_struct->get_components( ).
WRITE '@KERNEL let obj = {};'.
LOOP AT lt_components INTO ls_component.
LOOP AT lt_components ASSIGNING <ls_component>.
* WRITE '@KERNEL console.dir(ls_component.get().name);'.
call(
EXPORTING
handle = lo_struct->get_component_type( ls_component-name )
handle = lo_struct->get_component_type( <ls_component>-name )
CHANGING
dref = field ).
WRITE '@KERNEL obj[ls_component.get().name.get().toLowerCase()] = field.getPointer();'.
lv_name = to_lower( <ls_component>-name ).
WRITE '@KERNEL obj[lv_name.get()] = field.getPointer();'.
ENDLOOP.
WRITE '@KERNEL dref.assign(new abap.types.Structure(obj));'.
ENDMETHOD.

METHOD table.
DATA lo_table TYPE REF TO cl_abap_tabledescr.
DATA lt_keys TYPE abap_table_keydescr_tab.
DATA ls_key LIKE LINE OF lt_keys.
DATA lv_component TYPE string.
DATA field TYPE REF TO data.

FIELD-SYMBOLS <ls_key> LIKE LINE OF lt_keys.

lo_table ?= handle.

call(
Expand All @@ -113,16 +117,16 @@ CLASS kernel_create_data_handle IMPLEMENTATION.

* todo, handle secondary keys,
lt_keys = lo_table->get_keys( ).
LOOP AT lt_keys INTO ls_key WHERE is_primary = abap_true.
IF ls_key-access_kind = cl_abap_tabledescr=>tablekind_sorted.
LOOP AT lt_keys ASSIGNING <ls_key> WHERE is_primary = abap_true.
IF <ls_key>-access_kind = cl_abap_tabledescr=>tablekind_sorted.
WRITE '@KERNEL options.primaryKey.type = "SORTED";'.
ELSEIF ls_key-access_kind = cl_abap_tabledescr=>tablekind_hashed.
ELSEIF <ls_key>-access_kind = cl_abap_tabledescr=>tablekind_hashed.
WRITE '@KERNEL options.primaryKey.type = "HASHED";'.
ENDIF.
IF ls_key-is_unique = abap_true.
IF <ls_key>-is_unique = abap_true.
WRITE '@KERNEL options.primaryKey.isUnique = true;'.
ENDIF.
LOOP AT ls_key-components INTO lv_component.
LOOP AT <ls_key>-components INTO lv_component.
WRITE '@KERNEL options.primaryKey.keyFields.push(lv_component.get().toLowerCase());'.
ENDLOOP.
ENDLOOP.
Expand Down
51 changes: 14 additions & 37 deletions src/rtti/cl_abap_structdescr.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ CLASS cl_abap_structdescr DEFINITION PUBLIC INHERITING FROM cl_abap_complexdescr
PRIVATE SECTION.
METHODS update_components.

TYPES: BEGIN OF ty_refs,
name TYPE string,
ref TYPE REF TO cl_abap_datadescr,
suffix TYPE string,
as_include TYPE abap_bool,
END OF ty_refs.
DATA mt_refs TYPE STANDARD TABLE OF ty_refs WITH DEFAULT KEY.
DATA mt_refs TYPE component_table.
ENDCLASS.

CLASS cl_abap_structdescr IMPLEMENTATION.
Expand Down Expand Up @@ -107,7 +101,7 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
LOOP AT p_components INTO ls_component.
CLEAR ls_ref.
ls_ref-name = ls_component-name.
ls_ref-ref = ls_component-type.
ls_ref-type = ls_component-type.
APPEND ls_ref TO ref->mt_refs.
ENDLOOP.
ref->update_components( ).
Expand All @@ -127,7 +121,7 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
ls_view-name = ls_component-name.
READ TABLE mt_refs WITH KEY name = ls_component-name INTO ls_ref.
IF sy-subrc = 0.
ls_view-type = ls_ref-ref.
ls_view-type = ls_ref-type.
ENDIF.
IF ls_ref-as_include = abap_true.
CONTINUE.
Expand Down Expand Up @@ -193,15 +187,13 @@ CLASS cl_abap_structdescr IMPLEMENTATION.
ASSIGN COMPONENT lv_name OF STRUCTURE data TO <fs>.
lo_datadescr ?= cl_abap_typedescr=>describe_by_data( <fs> ).
ls_ref-name = lv_name.
ls_ref-ref = lo_datadescr.
ls_ref-type = lo_datadescr.

CLEAR lv_as_include.
WRITE '@KERNEL if (INPUT.data?.getAsInclude) {'.
WRITE '@KERNEL lv_as_include.set(INPUT.data?.getAsInclude()?.[name.toLowerCase()] ? "X" : " ");'.
WRITE '@KERNEL }'.
ls_ref-as_include = lv_as_include.

CLEAR lv_suffix.
WRITE '@KERNEL if (INPUT.data?.getSuffix) {'.
WRITE '@KERNEL lv_as_include.set(INPUT.data?.getSuffix()?.[name.toLowerCase()] || "");'.
WRITE '@KERNEL }'.
Expand All @@ -215,44 +207,29 @@ CLASS cl_abap_structdescr IMPLEMENTATION.

METHOD update_components.
DATA ls_component LIKE LINE OF components.
DATA ls_ref LIKE LINE OF mt_refs.
FIELD-SYMBOLS <ls_ref> LIKE LINE OF mt_refs.

CLEAR components.
LOOP AT mt_refs INTO ls_ref.
CLEAR ls_component.
ls_component-name = ls_ref-name.
ls_component-type_kind = ls_ref-ref->type_kind.
ls_component-length = ls_ref-ref->length.
ls_component-decimals = ls_ref-ref->decimals.
LOOP AT mt_refs ASSIGNING <ls_ref>.
ls_component-name = <ls_ref>-name.
ls_component-type_kind = <ls_ref>-type->type_kind.
ls_component-length = <ls_ref>-type->length.
ls_component-decimals = <ls_ref>-type->decimals.
APPEND ls_component TO components.
ENDLOOP.
ENDMETHOD.

METHOD get_components.
DATA ls_component LIKE LINE OF components.
DATA ret LIKE LINE OF rt_components.
DATA ls_ref LIKE LINE OF mt_refs.

LOOP AT components INTO ls_component.
CLEAR ret.
ret-name = ls_component-name.
READ TABLE mt_refs INTO ls_ref WITH KEY name = ls_component-name.
IF sy-subrc = 0.
ret-type = ls_ref-ref.
ENDIF.
" as_include type abap_bool,
" suffix type string,
APPEND ret TO rt_components.
ENDLOOP.
rt_components = mt_refs.
ENDMETHOD.

METHOD get_component_type.
DATA line LIKE LINE OF mt_refs.
READ TABLE mt_refs INTO line WITH KEY name = p_name.
FIELD-SYMBOLS <line> LIKE LINE OF mt_refs.
READ TABLE mt_refs ASSIGNING <line> WITH KEY name = p_name.
IF sy-subrc <> 0.
RAISE component_not_found.
ELSE.
p_descr_ref = line-ref.
p_descr_ref = <line>-type.
ENDIF.
ENDMETHOD.

Expand Down
2 changes: 1 addition & 1 deletion test/performance/cl_ui2_deserialize1.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CLASS cl_ui2_deserialize1 IMPLEMENTATION.
DATA ls_data TYPE STANDARD TABLE OF ty_data WITH DEFAULT KEY.

lv_json = lv_json && '['.
DO 200 TIMES.
DO 600 TIMES.
lv_json = lv_json && '{"abap": 2, "foo": "sdfs", "another": ""},'.
ENDDO.
lv_json = substring(
Expand Down

0 comments on commit 3b5f62b

Please sign in to comment.