From f402732a333e4fe67494789e9b27d1dfa6f4200d Mon Sep 17 00:00:00 2001 From: Czarna <86512570+Czarna1024@users.noreply.github.com> Date: Fri, 12 May 2023 23:26:37 +0200 Subject: [PATCH] Optimize the vis_heap_chunks by creating VALID_CHARS once This commit optimizes the `bin_ascii` function used by the `vis_heap_chunks` command. That function executed the following line on each call: ``` valid_chars = list(map(ord, set(printable) - set("\t\r\n\x0c\x0b"))) ``` And it could be called thousand times, e.g. 90k on a benchmark. This commit moves the creation of the `valid_chars` list to the global space so it is computed only once. As a result, on a simple benchmark we improved the speed of `vis_heap_chunks` command from 4.6s to 3s. --- pwndbg/commands/heap.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pwndbg/commands/heap.py b/pwndbg/commands/heap.py index 1e2c55212..d0503990a 100644 --- a/pwndbg/commands/heap.py +++ b/pwndbg/commands/heap.py @@ -1,5 +1,6 @@ import argparse import ctypes +from string import printable from typing import Dict from typing import List @@ -973,11 +974,11 @@ def vis_heap_chunks( ) -def bin_ascii(bs): - from string import printable +VALID_CHARS = list(map(ord, set(printable) - set("\t\r\n\x0c\x0b"))) + - valid_chars = list(map(ord, set(printable) - set("\t\r\n\x0c\x0b"))) - return "".join(chr(c) if c in valid_chars else "." for c in bs) +def bin_ascii(bs): + return "".join(chr(c) if c in VALID_CHARS else "." for c in bs) def bin_labels_mapping(collections):