rizin/test/unit
NOT XVilka 80f14bf6fc
librz/util/vector: minor RzVector/RzPVector performance optimizations (#6467)
* util/vector: hoist quicksort scratch buffers out of the recursion

vector_quick_sort allocated its two element-sized scratch buffers (t and
pivot) with malloc/free on every recursive call. For a vector of n elements
the sort makes O(n) recursive calls, i.e. O(n) malloc/free pairs purely for
scratch space, and each call could also fail half-way through the sort.

Split the function into a small entry point that allocates the two buffers
once and a recursive worker that receives them as scratch. The buffers are
reused across the whole recursion (each partition step finishes using them
before recursing, and the recursion is sequential, so sharing one pair is
safe). Small elements -- the common case, including every RzPVector-backed
sort -- use stack buffers and allocate nothing at all; only elements larger
than 256 bytes fall back to a single heap allocation for the whole sort.

The element movement and rand()-based pivot selection are unchanged, so the
result is identical for any input (verified byte-for-byte against the previous
implementation for ascending and descending orders over many random arrays).

* util/vector: evaluate the comparator once per element in the quicksort

The partition loop tested the element against the pivot with two separate
calls to the comparator:

    if ((cmp(VEC_INDEX(a, i), pivot, user) < 0 && !reverse) ||
        (cmp(VEC_INDEX(a, i), pivot, user) > 0 && reverse)) {

Because cmp is an opaque function pointer the compiler cannot common up the
two calls, so depending on the result and the reverse flag the comparator was
invoked up to twice per element. Compute the result once into a local and test
that:

    int c = cmp(VEC_INDEX(a, i), pivot, user);
    if ((c < 0 && !reverse) || (c > 0 && reverse)) {

This halves comparator calls in the worst case and is a clear win whenever the
comparator is non-trivial (the common case for struct elements). Measured on a
shared host: ~12-14% faster for int sorting and ~30% faster with a moderately
expensive comparator. The ordering is unchanged (verified byte-for-byte).

* util/vector: simplify rz_pvector_remove_data index computation

The index of the located slot was computed as

    size_t index = (el - (void **)vec->v.a) * sizeof(void **) / vec->v.elem_size;

For an RzPVector the element size is always sizeof(void *), so the
`* sizeof(void **) / vec->v.elem_size` factor is identically 1 and the pointer
difference `el - (void **)vec->v.a` already yields the index directly. Drop the
redundant scaling, which removes a multiply and a divide and makes the intent
clear. Behaviour is unchanged.

* test/unit: add RzVector sort and rz_pvector_remove_data regression tests

The existing sort tests only sort 4-5 small elements and there was no test for
rz_pvector_remove_data. Add coverage for the code paths exercised by the sort
changes and the remove_data cleanup:

  - test_vector_sort_large       sort 2000 heavily-duplicated ut32 values
                                 ascending and descending, verifying the result
                                 is ordered and a permutation of the input (vs a
                                 reference qsort). Drives the recursion deeply
                                 and the shared scratch buffers.
  - test_vector_sort_large_elem  sort 400 elements of 304 bytes each, taking the
                                 heap-allocated scratch fallback, and check the
                                 full payload (not just the key) stays consistent
                                 through all the element moves.
  - test_pvector_remove_data     remove interior, first and last elements by
                                 value while preserving order, and confirm
                                 removing an absent value is a no-op.

All pass on both the previous and the optimized implementation (the sort and
remove_data changes are behaviour-preserving).

* test/bench: benchmark rz_vector_sort and rz_pvector_sort

bench_vector.c benchmarked only remove_at and swap. Add sort benchmarks so the
suite covers the functions touched by the sort optimizations and can be run
against the old and new librz for before/after numbers:

  - rz_vector_sort over 4k ut64 with a cheap comparator
  - rz_vector_sort over 4k ut64 with a deliberately expensive comparator
    (shows the effect of evaluating the comparator once per element)
  - rz_pvector_sort over 4k pointers (reference; pvector sort is unchanged)

Each iteration refills the buffer from an unsorted master copy via a single
memcpy before sorting; that overhead is identical across builds so the measured
delta reflects the sort.

---------

Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2026-06-10 00:22:26 +08:00
..
auxiliary meson: Fix duplicate checks for -Wno-unused-result (#6053) 2026-03-19 20:46:00 +08:00
cpp Replace rz_core_cmd0 calls in tui/panels.c and port cmd_help commands to RzShell as cmd_math (#3421) 2023-03-30 18:36:48 +08:00
.gitignore
meson.build librz/arch: do not split ANSI escapes when truncating colored operands (#6430) 2026-05-30 16:06:07 +08:00
minunit.h Add new implementation of RzConfig (#5820) 2026-05-03 15:34:48 +08:00
mock_io.inl Change len parameter type to size_t in RzIO API (#4012) 2023-12-03 19:28:16 +08:00
rz_arch_buffer.h Add some SPDX headers (#1469) 2021-08-17 18:27:03 +02:00
rz_test_cmd_test Change -, --, = argument and = command meaning (#930) 2021-04-21 16:20:24 +08:00
test_addr_interval.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_agraph.c Fix RZ_IPI usage on public headers & fix memory leak in the ROP code. (#6171) 2026-04-08 13:37:17 +08:00
test_analysis_block.c Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_analysis_block_invars.inl Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_analysis_cc.c Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_analysis_class_graph.c refactor: add hashtable-based RzGraph implementation (#6152) 2026-04-08 04:01:40 +08:00
test_analysis_coverage.c Fix: add meta size in code count (#5054) 2025-04-02 10:54:52 +08:00
test_analysis_function.c librz/analysis: add new API to get storage information of function return (#6208) 2026-04-20 23:14:49 +08:00
test_analysis_hints.c analysis: add enum immediate hints with ahie command (#6052) 2026-03-21 15:52:21 +08:00
test_analysis_meta.c Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_analysis_op.c Fix implementation of rz_core_analysis_[bytes/op_chunk_iter] (#6145) 2026-04-04 15:32:47 +08:00
test_analysis_var.c Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_analysis_xrefs.c Remove Global/Static variables in path.c to make it thread safe (#5148) 2025-06-13 21:34:33 +08:00
test_annotated_code.c Convert %ll format specifiers to PFMT64 (#6267) 2026-04-22 05:57:04 +08:00
test_base16.c librz/util: base64, base85, base36, base32, base16 support and refactor (#5216) (#5271) 2025-08-17 15:26:25 +08:00
test_base32.c librz/util: base64, base85, base36, base32, base16 support and refactor (#5216) (#5271) 2025-08-17 15:26:25 +08:00
test_base36.c librz/util: base64, base85, base36, base32, base16 support and refactor (#5216) (#5271) 2025-08-17 15:26:25 +08:00
test_base64.c test: some base64 unit tests to demonstrate bugs 2023-10-13 09:42:50 +08:00
test_base85.c librz/util: base64, base85, base36, base32, base16 support and refactor (#5216) (#5271) 2025-08-17 15:26:25 +08:00
test_big.c Rename RNum to the RzNum (#1539) 2021-08-29 21:06:29 +02:00
test_bin_lines.c Add new Line Data Structure 2021-03-30 13:07:05 +08:00
test_bin_mach0.c Fix dyldcache reading tests with optimization (Fix #3459) (#5035) 2025-03-25 08:12:09 +01:00
test_bits.c Enhance performance of rz_bv_copy_nbits (#5541) 2025-11-23 21:52:23 +08:00
test_bitvector.c Add inplace variants for rz_bv_(un)signed_cast() (#6164) 2026-04-06 17:09:05 +02:00
test_buf.c Prevent RzBuffer's Oxff_priv from overriding io.0xff (#6371) 2026-05-27 06:18:45 +08:00
test_cmd.c Implement colors for tables and move RzListInfo out of table.c (#5729) 2026-01-05 21:34:41 +08:00
test_compare.c Port c commands to newshell (#1854) 2021-11-25 17:55:08 +05:30
test_config.c Add RzConfigValidator for validating (on set) owned variables (#6356) 2026-05-16 16:12:13 +02:00
test_config.h.in test: allow to run unit tests without installation 2022-05-10 16:57:44 +02:00
test_cons.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_bar.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_canvas.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_grep.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_histogram.c librz/cons/histogram: revamp visual horizontal histogram (#6365, #4431) (#6440) 2026-06-07 14:00:31 +08:00
test_cons_html.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_hud.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_line.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_pager.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_pal.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_pipe.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_cons_rgb.c test: increase coverage for RzCons and its components (#5735) 2026-01-07 12:27:45 +08:00
test_contrbtree.c Improve cmd_help.yaml and fix random number generator (#3452) 2023-04-07 20:22:01 +08:00
test_core_addr.c Implement unified API for describing addresses (name+offset format) (#5571) 2025-12-10 19:11:31 +08:00
test_core_analysis_stats.c Refactor rz_core_analysis_get_stats() and fix edge cases (#2645) 2022-05-30 04:59:47 +00:00
test_core_bin.c refactor: output legacy integer mode to use RzOutputMode (#5815) 2026-03-24 06:12:43 +08:00
test_core_cmd.c fix: resolve endless loop (#6049) 2026-03-18 20:52:14 +08:00
test_core_plugin.c support context-aware core plugins (#6404) 2026-05-28 20:37:01 +08:00
test_core_seek.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_core_task.c rz_core_task_join: Return fine-grained error values (#4505) 2024-05-22 12:03:35 +08:00
test_crypto.c Fix plugin removal and clean code (#3277) 2023-01-16 13:32:46 +01:00
test_debruijn.c Finish conversion of w commands to rzshell (#2412) 2022-03-16 11:20:29 +00:00
test_debug.c Remove rz_io_read_at and use only rz_io_read_at_mapped (#5777) 2026-01-11 22:50:50 +08:00
test_debug_session.c Rename rz_list_get() to rz_list_val() (#5896) 2026-02-09 22:47:57 +08:00
test_diff.c Implement special compare for bytes and always compare mem-aligned. 2026-06-01 01:45:59 +08:00
test_ebcdic.c EBCDIC character support (#1643) 2021-11-22 09:48:57 +08:00
test_encodings.c Enc: add unicode case mappings (#5956) 2026-03-01 15:06:28 +00:00
test_endian.c Convert %ll format specifiers to PFMT64 (#6267) 2026-04-22 05:57:04 +08:00
test_event.c Fix few clang-cl warnings 2021-09-04 12:06:07 +02:00
test_file.c Introduce rz_file_dos_basename() API 2022-03-29 09:27:51 +08:00
test_flags.c Fix aav.aav.* naming #2205 (#6079) 2026-03-29 17:49:04 +08:00
test_flirt.c Fix multiple memory leaks (#5636) 2025-12-18 14:23:48 +00:00
test_float.c librz/util/float: fix cast of negative zero to integer. (#5291) 2025-08-06 00:06:43 +08:00
test_glob.c Add support to rz_str_glob and add tests (#18420) 2021-03-16 16:57:32 +08:00
test_graph.c Graph - API changes to enum (#6349) 2026-05-22 13:45:18 +00:00
test_graph_new.c Graph - (tiny) performance improvements (#6382) 2026-05-31 22:00:25 +00:00
test_graph_new_cfg.c Graph - API changes to enum (#6349) 2026-05-22 13:45:18 +00:00
test_hash.c Add χ² , index of coincidence, min-entropy, serial correlation statistical indicators (#6466) 2026-06-08 00:20:15 +08:00
test_hex.c String/Hex-Search 1/9: Add hexadecimal number helpers, doxygen and type annotations. 2025-02-22 04:12:49 +08:00
test_ht.c Add rz_set_u_take(). (#6137) 2026-04-04 12:04:25 +00:00
test_id_storage.c Fix some memleaks in unit tests (#1869) 2021-10-22 09:33:20 +08:00
test_idpool.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_idstorage.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_il_definitions.c Fix leaks of RzILMem buffers and enabling RzILMem to take buffer ownership. (#5739) 2026-02-10 15:38:51 +00:00
test_il_helpers.c Update contact information of Rot127. (#5522) 2025-11-10 17:15:15 +00:00
test_il_reg.c Enable RzIL VM to halt on exceptions. (#5305) 2025-08-25 12:43:01 +00:00
test_il_validate.c RzIL: Hash Pure variable names to make them more suitable as hash table keys. (#5971) 2026-02-27 11:51:35 +00:00
test_il_vm.c Fix leaks of RzILMem buffers and enabling RzILMem to take buffer ownership. (#5739) 2026-02-10 15:38:51 +00:00
test_inflate_deflate.c Fix: multiple memory release issues (#5616) 2025-12-13 22:14:50 +08:00
test_intervaltree.c struct timezone is obsolete/deprecated (#4672) 2024-10-16 17:13:17 +08:00
test_io.c Move get_boundary functions without module dependencies to RzIO. (#5508) 2026-03-25 14:34:07 +00:00
test_io_ihex.c Remove rz_io_read_at and use only rz_io_read_at_mapped (#5777) 2026-01-11 22:50:50 +08:00
test_itv.c librz/util: implement a bounded interval. (#4977) 2025-03-09 15:24:07 +08:00
test_json.c RzUtil add new API 2023-08-25 23:32:05 +08:00
test_leb128.c test: add unit test for read_*_leb128() functions (#5931) 2026-02-20 04:24:59 +08:00
test_list.c Revert "Implement Pool Node allocation for RzList (#6203)" (#6313) 2026-05-04 22:45:00 +08:00
test_log.c Add getter for the current log level. (#5807) 2026-01-23 18:52:45 +00:00
test_lzma.c Fix: multiple memory release issues (#5616) 2025-12-13 22:14:50 +08:00
test_marks.c Add: Mark API (#5313) 2025-09-01 22:06:01 +00:00
test_math.c Delete overflow test because FreeBSD times out on it. (#6412) 2026-05-28 16:03:38 +00:00
test_mem.c Fix unit tests on 32-bit platforms (#6384) 2026-05-19 11:44:32 +08:00
test_ovf.c Fix #1506 - Fix unsigned integer overflow in RzIO cache skyline (#1512) 2021-08-25 16:36:20 +08:00
test_parse.c librz/arch: do not split ANSI escapes when truncating colored operands (#6430) 2026-05-30 16:06:07 +08:00
test_pf.c Rewrite the pf parser and rework the grammar (#6410) 2026-05-29 03:58:58 +08:00
test_pj.c Rename pj.h to rz_pj.h (#1783) 2021-10-01 21:35:39 +08:00
test_rbtree.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_reg.c RzReg: Associate roles with RzRegItem instead of name strings (#6291) 2026-04-30 12:03:51 +02:00
test_regex.c Improve performance of string search. (#5262) 2025-09-11 22:29:20 +08:00
test_rop.c librz/core: implement ROP/JOP/COP gadget cache (#6328) 2026-05-24 21:48:42 +08:00
test_rop_constraint.c librz/core: implement ROP/JOP/COP gadget cache (#6328) 2026-05-24 21:48:42 +08:00
test_run.c Add RzRun tests 2021-08-23 18:45:22 +08:00
test_rz_test.c Revert #4506 (#4519) 2024-05-27 21:50:58 +08:00
test_sdb.h DWARF improve DWARF5 support and refactor 2023-08-25 23:32:05 +08:00
test_sdb_array.c Fix: asn and asr command #3885 (#5477) 2025-11-04 23:38:16 +08:00
test_sdb_diff.c refactor: SwissTable implementation for ht (#5860) 2026-03-02 12:31:35 +08:00
test_sdb_sdb.c refactor: SwissTable implementation for ht (#5860) 2026-03-02 12:31:35 +08:00
test_sdb_util.c Move SDB into RzUtil 2022-07-19 08:45:20 +02:00
test_serialize_analysis.c librz/arch: fix some memory leaks (#6240) 2026-04-17 00:09:52 +08:00
test_serialize_config.c Add new implementation of RzConfig (#5820) 2026-05-03 15:34:48 +08:00
test_serialize_debug.c sdb: remove unused cas and expire features (#4487) 2024-05-12 18:27:54 +08:00
test_serialize_flag.c Rename rz_list_get() to rz_list_val() (#5896) 2026-02-09 22:47:57 +08:00
test_serialize_mark.c Add: Mark API (#5313) 2025-09-01 22:06:01 +00:00
test_serialize_seek.c sdb: remove unused cas and expire features (#4487) 2024-05-12 18:27:54 +08:00
test_serialize_spaces.c sdb: remove unused cas and expire features (#4487) 2024-05-12 18:27:54 +08:00
test_serialize_types.c librz/type: support C bitfield members in structs and unions (#1240, #314) (#6439) 2026-06-01 05:03:31 +08:00
test_skiplist.c Add user pointer to RzListComparator (#4204) 2024-02-11 13:28:33 +08:00
test_skyline.c Fix #1506 - Fix unsigned integer overflow in RzIO cache skyline (#1512) 2021-08-25 16:36:20 +08:00
test_socket.c Rewrite and fix non-libuv tcp server (Rt command) 2022-08-06 13:20:52 +02:00
test_spaces.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_sparse.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_stack.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_str.c Add rz_str_replace_regex for regex based string replacement (#6151) 2026-04-06 11:52:52 +00:00
test_str_search.c util: extend string search with user-defined printable characters (#6161) 2026-05-01 22:47:14 +08:00
test_strbuf.c RzStrBuf: remove weakref related code 2023-08-15 09:00:22 +08:00
test_subprocess.c Add *pty API (#3221) 2022-12-29 20:38:39 +08:00
test_syscall.c Fix: asn and asr command #3885 (#5477) 2025-11-04 23:38:16 +08:00
test_table.c librz/util/table: fix shared query filtering (#6166) 2026-04-08 11:48:04 +08:00
test_task.c SPDX Copyright text for all files based on history 2021-03-05 19:39:15 +08:00
test_threads.c Fix deadlock in rz_th_queue_close_when_empty() (#6383) 2026-05-25 09:25:29 +02:00
test_tokens.c Move RzAnalysis as private and force usage of C API (#6123) 2026-04-06 16:07:53 +00:00
test_tree.c Rename rz_list_get() to rz_list_val() (#5896) 2026-02-09 22:47:57 +08:00
test_type.c librz/arch: render flag-enum operands as an OR of members (#2344) (#6450) 2026-06-02 04:22:25 +08:00
test_uleb128.c Fix many pointer/int conversion warnings on arm32 (#2246) 2022-01-25 11:41:17 +01:00
test_unum.c librz/util/num: handling minimum st64 in rz_num_abs (#4952) 2025-03-02 22:44:09 +08:00
test_util.c shell: new arg type RZ_CMD_ARG_TYPE_FOLDER (#5514) 2025-12-10 19:08:38 +08:00
test_vector.c librz/util/vector: minor RzVector/RzPVector performance optimizations (#6467) 2026-06-10 00:22:26 +08:00
test_yank.c Fix endianness issues on s390x (#5940) 2026-02-19 23:13:18 +08:00