Proper way to resize image akin to da_push? And there's another question.

Right now, I am attempting to optimize my code by reducing the number of resize.

So, I made this code in the math parser:

			change_arr_size(index,count)=(
				temp_height=h(##index);
				temp_value=da_size(##index);
				new_dar_size=da_size(##index)+count;
				if(new_dar_size>=temp_height,
					while(temp_height<=new_dar_size,
						temp_height<<=2;
					);
					resize(##index,1,temp_height,1,coordinates_and_iterations_array_size,0);
					i[##index,h(##index)-1]=temp_value;
				);
			);

That allows me to reduce the number of resize. Is this proper? Another question, does da_push resize images unnecessarily even though the image that represents the dynamic array is greater than da_size()? Does this mean I have to use copy instead?

Right now, I made a different version of rep_rrd, and I’m so confused as to why my old code is faster.

Like, why is my latest subdivision method is slower than the older one?

			array_subdivision(arg_coordinates)=(
				arg_coordinates[iteration_index]!=$maximum_iteration?(
					starting_coordinate=v(0,decrement_dimensions);
					end_coordinate=starting_coordinate+dimensions;
					part_coordinates=arg_coordinates[starting_coordinate,2,dimensions];
					max_split_possible=int(calculate_distance(part_coordinates)/step_size);
					max_split_possible-1?(
						arg_coordinates[iteration_index]++;
						split_count=v(2,min(max_split_possible,max_split_count));
						end_point=1+part_coordinates[1]-(split_count-1)*step_size;
						change_arr_size(modifiable_array_ind,split_count);
						change_arr_size(unmodifiable_array_ind,split_count);
						repeat(split_count-1,
							left_coordinate=v(arg_coordinates[starting_coordinate]+step_size,end_point);
							arg_coordinates[end_coordinate]=left_coordinate-1;
							if(calc_probability()
							,da_push(#modifiable_array_ind,arg_coordinates);
							,da_push(#unmodifiable_array_ind,arg_coordinates);
							);
							arg_coordinates[starting_coordinate]=left_coordinate;
							end_point+=step_size;
						);
						arg_coordinates[end_coordinate]=part_coordinates[1];
						if(calc_probability()
						,da_push(#modifiable_array_ind,arg_coordinates);
						,da_push(#unmodifiable_array_ind,arg_coordinates);
						);
					):(
						change_arr_size(unmodifiable_array_ind,1);
						da_push(#unmodifiable_array_ind,arg_coordinates);
					);
				):(
					change_arr_size(unmodifiable_array_ind,1);
					da_push(#unmodifiable_array_ind,arg_coordinates);
				);
			);

Old one:

	sub_column(info_vector,point)=(
		ri=v(1,max_split,1,0)+1;
		mri=ri-1;
		sx=info_vector[0]+test_boundary;
		ex=info_vector[1]-test_boundary;
		diff=ex-sx;
		bound_check=diff>test_boundary;
		skip=0;
		da_remove(#-2,point);
		if(bound_check,
			ds=diff/mri;
			if(mri>1&&ds<=test_boundary,
				while(mri>1&&ds<=test_boundary,
					--ri;
					--mri;
					ds=diff/mri;
				);
			);
			if(test_boundary||diff?(ds>test_boundary):1,
				py=[info_vector[2],info_vector[3]];
				iter=info_vector[4]+1;
				p=info_vector[0];
				repeat(ri,ri_ind,
					rb=odd<1?u(0,1)>odd;
					ri_ind==mri?(
						if(p>info_vector[1],
							p=info_vector[1];
							if(!(info_vector[3]-info_vector[2]),rb=1;);
						);
						(rb||(iter==max_iter))?(
							da_push(#-1,[p,info_vector[1],py,iter]);
						):(
							da_push(#-2,[p,info_vector[1],py,iter]);
						);
					):
					!ri_ind?(
						mx=sx+ds;
						np=min(max(sx,round(u(p,mx))),ex);
						vp=[p,np];
						(rb||(iter==max_iter))?(
							da_push(#-1,[vp,py,iter]);
						):(
							da_push(#-2,[vp,py,iter]);
						);
						p=np+1;
						if(p>=ex,skip=1;);
					):(
						if(skip,continue(););
						mx+=ds;
						np=min(max(p+internal_space,round(u(p,mx))),ex);
						vp=[min(p,ex),np];
						(rb||(iter==max_iter))?(
							da_push(#-1,[vp,py,iter]);
						):(
							da_push(#-2,[vp,py,iter]);
						);
						p=np+1;
					);
				);
			,da_push(#-1,info_vector);
			);
		,da_push(#-1,info_vector);
		);
	);

Not sure I get the context of using this function, so I can’t say.
Basically, since da exist, you rarely have to manage images with dynamic size by yourself.

No, it should not. As long as the size of the dynamic array is enough to store new elements, the da is not resized.
Note that the very last element of the da stores the number of da elements, so if you have N elt in your da, its size must be at least N+1.

Well, as the number of new elements is known, once I add them, I resize the dynamic array. But, I found a better solution. I decided to resize dynamic array image to height of 1<<16 at the very beginning.

Here, this is the code
#@cli rep_rsdb: eq. to 'rep_random_rectangular_division'
+rep_rsdb: +random_subdivision_of_dimension_boundaries $*
#@cli rep_random_subdivision_of_dimension_boundaries: split>1,_additional_thickness>=0,_max_iter>=2,_probability[%]>0,_loop_limit>1,0<=_border[%]>=100%,_seed,_number_of_dimensions,spatial_dimension_1,spatial_dimension_2,...
#@cli : Generate random division of rectangle as in random number of division and varying thickness within rectangle.
#@cli : _additional_thickness refers to the excess pixel thickness.
#@cli : _max_iter limits the number of iterations per rectangle.
#@cli : _probability determines the probability that a rectangle will be permitted to be utilized for further iteration.
#@cli : _loop_limit limits the number of time the process of subdivision of all quadrilaterals is done.
#@cli : _seed generates the output based on defined parameter.
#@cli : Default values: '_gen_thick=0','_max_iter=5','_probability[%]=95%','_loop_limit=5000','_border=0%','_seed=n/a'
+rep_random_subdivision_of_dimension_boundaries:
skip ${2=0},${3=5},${4=95%},${5=5000},${6=2},${7=1},${8=}

# +rep_random_subdivision_of_dimension_boundaries 10,0,1000,1,1000000,0,0,,1000,1000 1000,1000 eval.. polygon(#-1,4,i0,i1,i2,i1,i2,i3,i0,i3,1,y);I;

start_img_ind:=$!
split,additional_thickness,maximum_iteration,probability,loop_limit,border_size,force_multithreading,seed=${1-8}
if $_cpus==1 force_multithreading=0 fi
dimensions:=$#-8

check "$split>1 &&
       $additional_thickness>=0 &&
       $maximum_iteration>=2 &&
       inrange($probability,0,1,1,1) &&
       (ispercentage("$border_size")?inrange("$border_size",0,1,1,1):isint("$border_size",0)) &&
       $loop_limit>1 &&
       isbool($force_multithreading)"

if ispercentage($border_size)
	border_size:=int((additional_thickness)*$border_size)
fi

check "$border_size<(1+$additional_thickness)"

coordinates_and_iterations_array_size={($dimensions<<1)+1}
1,1,1,$coordinates_and_iterations_array_size,"
	const dimensions=$dimensions;
	const additional_thickness=$additional_thickness;
	const border_size=$border_size;
	const double_border_size=border_size<<1;
	const minimum_dimension_requirement=additional_thickness+double_border_size+1;
	list_of_abs_dimensions=abs([${9--1}]);
	coordinates=vector(#s,0);
	repeat(dimensions,p,
		current_dimension=list_of_abs_dimensions[p];
		if(!isfinite(current_dimension)||!isint(current_dimension,minimum_dimension_requirement),run('error inv_inp'););
		copy(coordinates[p],[0,current_dimension-1],2,dimensions,1);
	);
	set('list_of_dimensions',v2s(list_of_abs_dimensions));
	[coordinates,0];"

(1) a[-2,-1] y # Convert last image to dynamic array image
1,1,1,100%
1,1,1,1,$loop_limit

last_da_test_img_ind=-1
repeat 1+$force_multithreading {
	iter,processing_mode=$>,${arg0\ $>,>,:}
	eval[-1] $processing_mode"begin_t(
			const dimensions=$dimensions;
			const decrement_dimensions=dimensions-1;
			const iteration_index=dimensions<<1;
			const step_size=$additional_thickness+1;
			const max_split_count=$split;
			const probability=$probability;
			const coordinates_and_iterations_array_size=$coordinates_and_iterations_array_size;
			const start_array_size=1<<16;
			current_selection=vector(#coordinates_and_iterations_array_size);

			if($force_multithreading&&!$iter
			,post_mt=1;condition()=da_size(#modifiable_array_ind)<$_cpus&&loop_limit--;
			,post_mt=0;condition()=da_size(#modifiable_array_ind)&&loop_limit--;
			);

			probability==1?(
				calc_probability()=1;
			):(
				const negate_probability=1-probability;
				calc_probability()=iteration_level?(u>negate_probability):1;
			);

			calculate_distance(arg_part_coordinates)=arg_part_coordinates[1]-arg_part_coordinates[0]+1;

			array_subdivision(arg_coordinates)=(
				arg_coordinates[iteration_index]!=$maximum_iteration?(
					starting_coordinate=v(0,decrement_dimensions);
					end_coordinate=starting_coordinate+dimensions;
					part_coordinates=arg_coordinates[starting_coordinate,2,dimensions];
					max_split_possible=int(calculate_distance(part_coordinates)/step_size);
					max_split_possible-1?(
						arg_coordinates[iteration_index]++;
						split_count=v(2,min(max_split_possible,max_split_count));
						end_point=1+part_coordinates[1]-(split_count-1)*step_size;
						repeat(split_count-1,
							left_coordinate=v(arg_coordinates[starting_coordinate]+step_size,end_point);
							arg_coordinates[end_coordinate]=left_coordinate-1;
							if(calc_probability()
							,da_push(#modifiable_array_ind,arg_coordinates);
							,da_push(#unmodifiable_array_ind,arg_coordinates);
							);
							arg_coordinates[starting_coordinate]=left_coordinate;
							end_point+=step_size;
						);
						arg_coordinates[end_coordinate]=part_coordinates[1];
						if(calc_probability()
						,da_push(#modifiable_array_ind,arg_coordinates);
						,da_push(#unmodifiable_array_ind,arg_coordinates);
						);
					):(
						da_push(#unmodifiable_array_ind,arg_coordinates);
					);
				):(
					da_push(#unmodifiable_array_ind,arg_coordinates);
				);
			);

		);
		modifiable_array_ind=$start_img_ind+(x<<1);
		unmodifiable_array_ind=modifiable_array_ind+1;
		loop_limit=i;
		if(!post_mt,
			elm_count_mod=da_size(#modifiable_array_ind);
			elm_count_unm=da_size(#unmodifiable_array_ind);
			resize(#modifiable_array_ind,1,start_array_size,1,coordinates_and_iterations_array_size,0);
			resize(#unmodifiable_array_ind,1,start_array_size,1,coordinates_and_iterations_array_size,0);
			i[#modifiable_array_ind,start_array_size-1]=elm_count_mod;
			i[#unmodifiable_array_ind,start_array_size-1]=elm_count_unm;
		);
		while(condition(),
			current_selection_of_modifiable_array=v(0,da_size(#modifiable_array_ind)-1);
			copy(current_selection[0],i[#modifiable_array_ind,current_selection_of_modifiable_array],coordinates_and_iterations_array_size,1,h(#modifiable_array_ind));
			da_remove(#modifiable_array_ind,current_selection_of_modifiable_array);
			array_subdivision(current_selection);
		);
		final_da_size=da_size(#modifiable_array_ind);
		da_freeze(#modifiable_array_ind);
		da_freeze(#unmodifiable_array_ind);
		end(
			post_mt&&loop_limit&&final_da_size?(
				set('new_loop_limit',loop_limit);
				set('continue_loop_in_multithreaded_mode',1);
			):(
				set('continue_loop_in_multithreaded_mode',0);
			);
		);"
	if $continue_loop_in_multithreaded_mode
		rm. {min($new_loop_limit,$_cpus)},1,1,1,{int($new_loop_limit/w)}
		s[$start_img_ind] y,$_cpus
		foreach[$start_img_ind--3] { ({h}) a[-2,-1] y 1,1,1,100% }
	else
		rm.
		break
	fi
}
a[$start_img_ind--1] y shuffle.

As you can see, if(!post_mt...); first resizes a dynamic array image into 1<<16, this reduces the number of resize, and thus my execution time is more in line with what I expect from this code.

I feel that I can do better by doing what I did earlier, and use multiple copy() to mimic da_push multiple times at once, but nah, I’ll skip that.

That’s why my earlier code didn’t really change the execution time that much, so I ditched that solution.