Home > Back-end >  Optimize byte array escaping performance python
Optimize byte array escaping performance python

Time:10-15

I need to perform custom escaping over a byte array in python. However, during escaping python converts bytes to integers, making performance optimization very difficult. How can I speed up my escaping function?

ESCAPE_DICT={
    0x00: [0x5C,0x7A], # null        -> \z 0x5c 0x7a
    0x22: [0x5C,0x71], # "           -> \q 0x5c 0x71
    0x3B: [0x5C,0x73], # ;           -> \s 0x5c 0x73
    0x5C: [0x5C,0x5C], # \           -> \\ 0x5c 0x5c
    0x0A: [0x5C,0x6E], # line-feed   -> \n 0x5c 0x6e
    0x0C: [0x5C,0x66], # form-feed   -> \f 0x5c 0x66
    0x0D: [0x5C,0x63], # carr-return -> \c 0x5c 0x63
}

def escape(string: bytes):
    str_len=string.__len__()
    escaped_list=[]
    for i in range(0,str_len):
        curr_byte=string[i]
        escape = ESCAPE_DICT.get(curr_byte)
        if escape is None:
            # Don't escape current byte
            escaped_list.append(curr_byte)
        else:
            # Escape current byte
            escaped_list.extend(escape)
    return bytes(escaped_array)

CodePudding user response:

import re

ESCAPE_DICT = {
    b'\x00': rb'\z',  # null
    b'"': rb'\q',
    b';': rb'\s',
    b'\\': rb'\\',
    b'\n': rb'\n',  # linefeed
    b'\f': rb'\f',  # formfeed
    b'\r': rb'\c',  # carriage return
}
ESCAPE_CLASS = '['   ''.join(r'\x'   e.hex() for e in ESCAPE_DICT)   ']'
ESCAPE_REGEX = re.compile(ESCAPE_CLASS.encode())


def escape(string: bytes) -> bytes:
    return re.sub(ESCAPE_REGEX, lambda m: ESCAPE_DICT[m.group(0)], string)


x = b'"abc\ndef\rpqr\x00stu\\xyz"'
y = escape(x)

from pprint import pprint
pprint(ESCAPE_CLASS)
pprint(ESCAPE_REGEX)
pprint(x)
pprint(y)
# =>
# '[\\x00\\x22\\x3b\\x5c\\x0a\\x0c\\x0d]'
# re.compile(b'[\\x00\\x22\\x3b\\x5c\\x0a\\x0c\\x0d]')
# b'"abc\ndef\rpqr\x00stu\\xyz"'
# b'\\qabc\\ndef\\cpqr\\zstu\\\\xyz\\q'

You can read the rb prefix as “raw bytes”.

Your escapes are a bit strange, though. E.g., the carriage return is normally \r, not \c, and \s normally stands for generic whitespace.

  • Related