Better usage of memoryview reduces heap allocation

pull/3/merge
Peter Hinch 2016-11-26 07:56:06 +00:00
rodzic 67ea344993
commit 686d1c92bb
2 zmienionych plików z 5 dodań i 4 usunięć

Wyświetl plik

@ -168,13 +168,14 @@ _index =\
b'\x00\x00\x14\x00\x2e\x00\x4b\x00\x71\x00\x97\x00\xd2\x00\x0a\x01'\
b'\x1b\x01\x35\x01\x4f\x01\x75\x01\x9e\x01\xb2\x01\xcc\x01\xe0\x01'\
_mvfont = memoryview(_font)
# Boilerplate code omitted
def get_ch(ch):
# validate ch, if out of range use '?'
# get offsets into _font and retrieve char width
# Return: memoryview of bitmap, height and width
return memoryview(_font[offset + 2, next_offset]), height, width
return mvfont[offset + 2, next_offset], height, width
```
``height`` and ``width`` are specified in bits (pixels).

Wyświetl plik

@ -348,7 +348,7 @@ STR01 = """# Code generated by font-to-py.py.
version = '0.1'
"""
STR02 = """
STR02 = """_mvfont = memoryview(_font)
def _chr_addr(ordch):
offset = 2 * (ordch - 32)
@ -360,7 +360,7 @@ def get_ch(ch):
offset = _chr_addr(ordch)
width = int.from_bytes(_font[offset:offset + 2], 'little')
next_offs = _chr_addr(ordch +1)
return memoryview(_font[offset + 2:next_offs]), {}, width
return _mvfont[offset + 2:next_offs], {}, width
"""
@ -399,7 +399,7 @@ def write_data(stream, fnt, font_path, monospaced, hmap, reverse):
bw_index = ByteWriter(stream, '_index')
bw_index.odata(index)
bw_index.eot()
stream.write(STR02.format(height, height))
stream.write(STR02.format(height))
# BINARY OUTPUT
# hmap reverse magic bytes