From 7c97dd78ed3aa28edb9626125653a057655cb7c6 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 12 Jul 2024 14:05:36 +0300 Subject: [PATCH] A bit optimizing array assignment codegen --- tpl/bhl_bind_macro.twig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tpl/bhl_bind_macro.twig b/tpl/bhl_bind_macro.twig index 8d0e6d4..4b1eb4d 100644 --- a/tpl/bhl_bind_macro.twig +++ b/tpl/bhl_bind_macro.twig @@ -407,15 +407,16 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- endif -%} {%- if type is instanceof('\\mtgArrType') -%} - var _arr_type = (ArrayTypeSymbol){{value}}.type; - int _native_before_count = 0; + var _src_arr_type = (ArrayTypeSymbol){{value}}.type; + int _dst_before_count = 0; + int _src_arr_count = _src_arr_type.ArrCount({{value}}); - {%- if type == 'any[]'-%} + {%~ if type == 'any[]'-%} {{native}} = ValList.New(frm.vm); {%~ else ~%} try { - _native_before_count = {{native}}.Count; + _dst_before_count = {{native}}.Count; } catch(NullReferenceException) { @@ -423,17 +424,18 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack } {%~ endif -%} - for(int i=0;i<_arr_type.ArrCount({{value}});++i) + + for(int i=0;i<_src_arr_count;++i) { - var _arr_item = _arr_type.ArrGetAt({{value}}, i); + var _arr_item = _src_arr_type.ArrGetAt({{value}}, i); {#TODO: get rid of replace hack below#} {{('var ' ~ _self.val2native(type.value, '_arr_item', 'tmp'))|replace({'tmp = tmp' : '', 'var var' : 'var'})}}; - if(i < _native_before_count) + if(i < _dst_before_count) {{native}}[i] = tmp; else {{native}}.Add(tmp); } - while({{native}}.Count > _arr_type.ArrCount({{value}})) + while({{native}}.Count > _src_arr_count) {{native}}.RemoveAt({{native}}.Count-1); {%- endif -%}