Skip to content

Commit f4c77eb

Browse files
committed
Refactor string size collection functions for improved readability and efficiency
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent f752577 commit f4c77eb

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

src/ebpf_c_codegen.ml

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ let emit_line ctx line =
200200
let emit_blank_line ctx =
201201
ctx.output_lines <- ctx.output_lines @ [""]
202202

203+
let concat = List.concat
204+
let concat_map f l = List.concat (List.map f l)
205+
let concat_map_opt f = function
206+
| Some l -> concat_map f l
207+
| None -> []
208+
203209
let increase_indent ctx = ctx.indent_level <- ctx.indent_level + 1
204210

205211
let decrease_indent ctx = ctx.indent_level <- ctx.indent_level - 1
@@ -356,13 +362,9 @@ let rec collect_string_sizes_from_instr ir_instr =
356362
in
357363
var_type_sizes @ init_sizes
358364
| IRCall (_, args, ret_opt) ->
359-
let args_sizes = List.fold_left (fun acc arg ->
360-
acc @ (collect_string_sizes_from_value arg)) [] args in
361-
let ret_sizes = match ret_opt with
362-
| Some ret_val -> collect_string_sizes_from_value ret_val
363-
| None -> []
364-
in
365-
args_sizes @ ret_sizes
365+
let args_sizes = concat_map collect_string_sizes_from_value args in
366+
let ret_sizes = match ret_opt with Some ret_val -> collect_string_sizes_from_value ret_val | None -> [] in
367+
args_sizes @ ret_sizes
366368
| IRMapLoad (map_val, key_val, dest_val, _) ->
367369
(collect_string_sizes_from_value map_val) @
368370
(collect_string_sizes_from_value key_val) @
@@ -392,24 +394,17 @@ let rec collect_string_sizes_from_instr ir_instr =
392394
collect_string_sizes_from_value cond_val
393395
| IRIf (cond_val, then_instrs, else_instrs_opt) ->
394396
let cond_sizes = collect_string_sizes_from_value cond_val in
395-
let then_sizes = List.fold_left (fun acc instr ->
396-
acc @ (collect_string_sizes_from_instr instr)) [] then_instrs in
397-
let else_sizes = match else_instrs_opt with
398-
| Some else_instrs -> List.fold_left (fun acc instr ->
399-
acc @ (collect_string_sizes_from_instr instr)) [] else_instrs
400-
| None -> []
401-
in
397+
let then_sizes = concat_map collect_string_sizes_from_instr then_instrs in
398+
let else_sizes = concat_map_opt collect_string_sizes_from_instr else_instrs_opt in
402399
cond_sizes @ then_sizes @ else_sizes
403400
| IRIfElseChain (conditions_and_bodies, final_else) ->
404-
let cond_sizes = List.fold_left (fun acc (cond_val, then_instrs) ->
405-
let cond_sizes = collect_string_sizes_from_value cond_val in
406-
let then_sizes = List.fold_left (fun acc instr ->
407-
acc @ (collect_string_sizes_from_instr instr)) [] then_instrs in
408-
acc @ cond_sizes @ then_sizes
409-
) [] conditions_and_bodies in
401+
let cond_sizes = concat_map (fun (cond_val, then_instrs) ->
402+
let cond_sz = collect_string_sizes_from_value cond_val in
403+
let then_sz = concat_map collect_string_sizes_from_instr then_instrs in
404+
cond_sz @ then_sz
405+
) conditions_and_bodies in
410406
let else_sizes = match final_else with
411-
| Some else_instrs -> List.fold_left (fun acc instr ->
412-
acc @ (collect_string_sizes_from_instr instr)) [] else_instrs
407+
| Some else_instrs -> concat_map collect_string_sizes_from_instr else_instrs
413408
| None -> []
414409
in
415410
cond_sizes @ else_sizes
@@ -440,8 +435,7 @@ let rec collect_string_sizes_from_instr ir_instr =
440435
(collect_string_sizes_from_value end_val) @
441436
(collect_string_sizes_from_value counter_val) @
442437
(collect_string_sizes_from_value ctx_val) @
443-
(List.fold_left (fun acc instr ->
444-
acc @ (collect_string_sizes_from_instr instr)) [] body_instructions)
438+
(concat_map collect_string_sizes_from_instr body_instructions)
445439
| IRBreak -> []
446440
| IRContinue -> []
447441
| IRCondReturn (cond_val, ret_if_true, ret_if_false) ->
@@ -456,16 +450,13 @@ let rec collect_string_sizes_from_instr ir_instr =
456450
in
457451
cond_sizes @ true_sizes @ false_sizes
458452
| IRTry (try_instructions, _catch_clauses) ->
459-
List.fold_left (fun acc instr ->
460-
acc @ (collect_string_sizes_from_instr instr)) [] try_instructions
453+
concat_map collect_string_sizes_from_instr try_instructions
461454
| IRThrow _error_code ->
462455
[] (* Throw statements don't contain values to collect *)
463456
| IRDefer defer_instructions ->
464-
List.fold_left (fun acc instr ->
465-
acc @ (collect_string_sizes_from_instr instr)) [] defer_instructions
457+
concat_map collect_string_sizes_from_instr defer_instructions
466458
| IRTailCall (_, args, _) ->
467-
List.fold_left (fun acc arg ->
468-
acc @ (collect_string_sizes_from_value arg)) [] args
459+
concat_map collect_string_sizes_from_value args
469460
| IRStructOpsRegister (instance_val, struct_ops_val) ->
470461
(collect_string_sizes_from_value instance_val) @ (collect_string_sizes_from_value struct_ops_val)
471462
| IRObjectNew (dest_val, _) ->
@@ -478,23 +469,13 @@ let rec collect_string_sizes_from_instr ir_instr =
478469
collect_string_sizes_from_value ringbuf_val
479470

480471
let collect_string_sizes_from_function ir_func =
481-
List.fold_left (fun acc block ->
482-
List.fold_left (fun acc instr ->
483-
acc @ (collect_string_sizes_from_instr instr)
484-
) acc block.instructions
485-
) [] ir_func.basic_blocks
472+
concat_map (fun block -> concat_map collect_string_sizes_from_instr block.instructions) ir_func.basic_blocks
486473

487474
let collect_string_sizes_from_multi_program ir_multi_prog =
488-
let program_sizes = List.fold_left (fun acc ir_prog ->
489-
let entry_sizes = collect_string_sizes_from_function ir_prog.entry_function in
490-
acc @ entry_sizes
491-
) [] ir_multi_prog.programs in
492-
475+
let program_sizes = concat_map (fun ir_prog -> collect_string_sizes_from_function ir_prog.entry_function) ir_multi_prog.programs in
476+
493477
(* Also collect from kernel functions *)
494-
let kernel_func_sizes = List.fold_left (fun acc ir_func ->
495-
let func_sizes = collect_string_sizes_from_function ir_func in
496-
acc @ func_sizes
497-
) [] ir_multi_prog.kernel_functions in
478+
let kernel_func_sizes = concat_map (fun ir_func -> collect_string_sizes_from_function ir_func) ir_multi_prog.kernel_functions in
498479

499480
(* NOTE: We used to collect string sizes from all userspace structs here, but this was incorrect.
500481
Only structs that are actually used by eBPF programs should be considered.

0 commit comments

Comments
 (0)