From b153f222a5fdd87e68b90d2ee972074a356b1d56 Mon Sep 17 00:00:00 2001 From: Nuno Duque Nunes Date: Fri, 29 May 2026 00:27:43 +0000 Subject: [PATCH] perf: batch resolve accept dest IPs in activity (4x speedup) - json_helper: batch_resolve_dest() resolves all dest IPs in one Python call - json.sh: json::batch_resolve_dest wrapper - activity: _DEST_RESOLVE_CACHE pre-populated before render loop - _render_peer_accept_dests: cache lookup instead of resolve::dest per row - activity: 1.8s -> 0.48s --- commands/activity.command.sh | 63 ++++++++++++------ core/json.sh | 1 + core/json_helper.py | 55 +++++++++++++++ .../__pycache__/accept_events.cpython-311.pyc | Bin 13201 -> 13201 bytes 4 files changed, 99 insertions(+), 20 deletions(-) diff --git a/commands/activity.command.sh b/commands/activity.command.sh index a0c3380..554ea0a 100644 --- a/commands/activity.command.sh +++ b/commands/activity.command.sh @@ -188,27 +188,50 @@ function cmd::activity::run() { # ── Accept dest inline renderer ── _render_peer_accept_dests() { - local peer_name="$1" - local keys="${_ACCEPT_DEST_KEYS[$peer_name]:-}" - [[ -z "$keys" ]] && return 0 - for d_key in $keys; do - local dest_stats="${_ACCEPT_DEST[$d_key]:-}" - [[ -z "$dest_stats" ]] && continue - local d_bytes_orig d_bytes_reply d_count - IFS='|' read -r d_bytes_orig d_bytes_reply d_count <<< "$dest_stats" - local rest_key="${d_key#${peer_name}:}" - local d_ip="${rest_key%%:*}" - local pp="${rest_key#*:}" - local d_port="${pp%%:*}" - local d_proto="${pp##*:}" - local dest_display - dest_display=$(resolve::dest "$d_ip" "$d_port" "$d_proto" 2>/dev/null \ - || echo "${d_ip}:${d_port}/${d_proto}") - ui::activity::accept_dest_row \ - "$dest_display" "$d_bytes_orig" "$d_bytes_reply" \ - "$d_count" "$drops_col" "$w_count" + local peer_name="$1" + local keys="${_ACCEPT_DEST_KEYS[$peer_name]:-}" + [[ -z "$keys" ]] && return 0 + for d_key in $keys; do + local dest_stats="${_ACCEPT_DEST[$d_key]:-}" + [[ -z "$dest_stats" ]] && continue + local d_bytes_orig d_bytes_reply d_count + IFS='|' read -r d_bytes_orig d_bytes_reply d_count <<< "$dest_stats" + local rest_key="${d_key#${peer_name}:}" + local d_ip="${rest_key%%:*}" + local pp="${rest_key#*:}" + local d_port="${pp%%:*}" + local d_proto="${pp##*:}" + local spec="${d_ip}:${d_port}:${d_proto}" + local dest_display="${_DEST_RESOLVE_CACHE[$spec]:-${d_ip}:${d_port}/${d_proto}}" + ui::activity::accept_dest_row \ + "$dest_display" "$d_bytes_orig" "$d_bytes_reply" \ + "$d_count" "$drops_col" "$w_count" + done + } + + declare -gA _DEST_RESOLVE_CACHE=() + local -a _dest_specs=() + for _dk in "${!_ACCEPT_DEST[@]}"; do + # key format: peer:ip:port:proto — strip peer prefix + local _rest="${_dk#*:}" + local _dip="${_rest%%:*}" + local _pp="${_rest#*:}" + local _dport="${_pp%%:*}" + local _dproto="${_pp##*:}" + local _spec="${_dip}:${_dport}:${_dproto}" + # Deduplicate + local _found=false + for _s in "${_dest_specs[@]:-}"; do + [[ "$_s" == "$_spec" ]] && _found=true && break done - } + $_found || _dest_specs+=("$_spec") + done + + if [[ ${#_dest_specs[@]} -gt 0 ]]; then + while IFS='|' read -r _spec _display; do + [[ -n "$_spec" ]] && _DEST_RESOLVE_CACHE["$_spec"]="$_display" + done < <(json::batch_resolve_dest "${_dest_specs[@]}" 2>/dev/null) + fi local first_peer=true skip_peer=false current_name="" local -a rendered_peers=() diff --git a/core/json.sh b/core/json.sh index c730605..bdad396 100644 --- a/core/json.sh +++ b/core/json.sh @@ -157,6 +157,7 @@ function json::endpoint_cache_get() { python3 "$JSON_HELPER" endpoint_cach # Accept Events function json::accept_events() { python3 "$JSON_HELPER" accept_events "$@" 3 else '', args[4] if len(args) > 4 else '', args[5] if len(args) > 5 else '0'), + 'batch_resolve_dest': lambda args: batch_resolve_dest(args[0], args[1], *args[2:]), } # ── Main ───────────────────────────────────────────────────────────────────── diff --git a/core/lib/__pycache__/accept_events.cpython-311.pyc b/core/lib/__pycache__/accept_events.cpython-311.pyc index 12b234c6a185de6db9294a5ff5cc4e6d8befc5f8..fcd4f3dc94af570c088a2cd1dfbd16b412194432 100644 GIT binary patch delta 19 ZcmbQ3J~5qZIWI340}y<