Skip to content

Commit

Permalink
pynuttx: optimize memory leak check by skipping previously checked po…
Browse files Browse the repository at this point in the history
…inters

Reduce leak analysis time from 12s to 7.8s, achieving a 35% performance improvement that tested on an internal project.

1. Use memoryview to avoid creating int objects, reducing overhead
2. Store checked pointers in a dictionary for efficient lookups, improving performance.
3. While dictionary operations have some cost, the overall speedup is significant.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
  • Loading branch information
XuNeo authored and acassis committed Feb 5, 2025
1 parent b906384 commit 04bd00f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tools/pynuttx/nxgdb/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,34 @@ def collect_leaks(

longsize = utils.get_long_type().sizeof

checked_ptr = {}

def pointers(node: memdump.MMNodeDump) -> Generator[int, None, None]:
# Return all possible pointers stored in this node
size = node.nodesize - node.overhead
memory = node.read_memory()
prev_mem = None
while size > 0:
size -= longsize
ptr = int.from_bytes(memory[size : size + longsize], "little")
mem = memory[size : size + longsize]
if mem == prev_mem:
continue

prev_mem = mem
ptr = int.from_bytes(mem, "little")
if ptr in checked_ptr:
continue

checked_ptr[ptr] = True

if any(region["start"] <= ptr < region["end"] for region in regions):
yield ptr

print("Leak analyzing...", flush=True, end="")
t = time.time()
for good in good_nodes:
if not sorted_addr: # All nodes are checked
break
for ptr in pointers(good):
if not (idx := bisect.bisect_right(sorted_addr, ptr)):
continue
Expand Down

0 comments on commit 04bd00f

Please sign in to comment.