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.
pull/1710/head
Czarna 3 years ago committed by GitHub
parent e77c6f5c2e
commit f402732a33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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):

Loading…
Cancel
Save