| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from collections import Counter |
| 4 |
|
| 5 |
from rpython.rlib.rarithmetic import r_longlong, r_int32, r_uint32, intmask |
| 6 |
from rpython.rlib.unicodedata.supportcode import signed_ord |
| 7 |
from rpython.rtyper.lltypesystem.rffi import r_ushort, r_short |
| 8 |
|
| 9 |
WORDSIZE = 8 |
| 10 |
|
| 11 |
def get_size_unsignedness(data): |
| 12 |
|
| 13 |
|
| 14 |
maxdata = max(data) |
| 15 |
mindata = min(data) |
| 16 |
if mindata < 0: |
| 17 |
assert -2 ** 63 - 1 <= mindata and maxdata < 2 ** 63 |
| 18 |
assert maxdata < 2 ** 64 |
| 19 |
for size in [1, 2, 4, 8]: |
| 20 |
bits = size * 8 - 1 |
| 21 |
if -2 ** bits <= mindata and maxdata < 2 ** bits: |
| 22 |
return size, False |
| 23 |
else: |
| 24 |
if not maxdata < 2 ** 64: |
| 25 |
import pdb; pdb.set_trace() |
| 26 |
for size in [1, 2, 4, 8]: |
| 27 |
bits = size * 8 |
| 28 |
if maxdata < 2 ** bits: |
| 29 |
return size, True |
| 30 |
assert 0, "unreachable" |
| 31 |
|
| 32 |
def getsize_unsigned(data): |
| 33 |
size, unsigned = get_size_unsignedness(data) |
| 34 |
assert unsigned |
| 35 |
return size |
| 36 |
|
| 37 |
def get_char_list_offset(cl, allchars, index): |
| 38 |
key = tuple(cl) |
| 39 |
if key in index: |
| 40 |
return index[key] |
| 41 |
newoffset = len(allchars) |
| 42 |
allchars.extend(cl) |
| 43 |
index[key] = newoffset |
| 44 |
|
| 45 |
for size in range(1, len(key)): |
| 46 |
for start in range(0, len(key) - size + 1): |
| 47 |
subkey = key[start: start+size] |
| 48 |
index[subkey] = newoffset + start |
| 49 |
return newoffset |
| 50 |
|
| 51 |
class CodeWriter(object): |
| 52 |
def __init__(self, outfile): |
| 53 |
self.outfile = outfile |
| 54 |
|
| 55 |
self.size_estimates = Counter() |
| 56 |
self.seen_strings = set() |
| 57 |
|
| 58 |
def print_stats(self): |
| 59 |
def kib(size): |
| 60 |
return round(size/1024., 2) |
| 61 |
print("estimated output sizes [KiB]") |
| 62 |
print("TOTAL", kib(sum(value for key, value in self.size_estimates.iteritems() if key != "unknown" and key != "code"))) |
| 63 |
for category, size in self.size_estimates.most_common(): |
| 64 |
if category == "code": |
| 65 |
continue |
| 66 |
print(category, kib(size)) |
| 67 |
|
| 68 |
print("python code [source KiB]", kib(self.size_estimates['code'])) |
| 69 |
|
| 70 |
def print_listlike(self, name, lst, category=None): |
| 71 |
if not lst: |
| 72 |
self.print_code("def %s(index): raise KeyError" % (name, )) |
| 73 |
return |
| 74 |
|
| 75 |
length = len(lst) |
| 76 |
first = lst[0] |
| 77 |
start = 0 |
| 78 |
for start in range(1, length): |
| 79 |
if lst[start] != first: |
| 80 |
break |
| 81 |
else: |
| 82 |
|
| 83 |
self.print_code(""" |
| 84 |
def %s(index): |
| 85 |
if 0 <= index < %s: return %s |
| 86 |
raise KeyError |
| 87 |
""" % (name, length, first)) |
| 88 |
return |
| 89 |
|
| 90 |
last = lst[-1] |
| 91 |
stop = length |
| 92 |
for stop in range(length - 1, -1, -1): |
| 93 |
if lst[stop] != last: |
| 94 |
break |
| 95 |
else: |
| 96 |
assert 0, "unreachable" |
| 97 |
if start + length - stop > 20: |
| 98 |
self.print_code(""" |
| 99 |
def %(name)s(index): |
| 100 |
if %(start)s <= index <= %(stop)s: |
| 101 |
return %(name)s_middle(index - %(start)s) |
| 102 |
if index < %(start)s: |
| 103 |
return %(first)s |
| 104 |
if index < %(length)s: |
| 105 |
return %(last)s |
| 106 |
raise KeyError |
| 107 |
""" % dict(name=name, start=start, stop=stop, first=first, last=last, length=length)) |
| 108 |
name = name + "_middle" |
| 109 |
lst = lst[start:stop+1] |
| 110 |
if not lst: |
| 111 |
import pdb; pdb.set_trace() |
| 112 |
|
| 113 |
if not all(type(x) is int for x in lst): |
| 114 |
print('_%s = [' % (name, ), file=self.outfile) |
| 115 |
for val in lst: |
| 116 |
self._estimate_any(name, val, category) |
| 117 |
print('%r,' % val, file=self.outfile) |
| 118 |
print(']', file=self.outfile) |
| 119 |
print(file=self.outfile) |
| 120 |
size = len(lst) * WORDSIZE + WORDSIZE * 2 |
| 121 |
self._estimate_any(name, size, category) |
| 122 |
self.print_code("""def %s(index): |
| 123 |
try: |
| 124 |
return _%s[index] |
| 125 |
except IndexError: |
| 126 |
raise KeyError""" % (name, name)) |
| 127 |
return |
| 128 |
itemsize, unsigned = get_size_unsignedness(lst) |
| 129 |
chunksize = 64 |
| 130 |
if itemsize == 1: |
| 131 |
|
| 132 |
if unsigned: |
| 133 |
unwrapfunc = "ord" |
| 134 |
else: |
| 135 |
unwrapfunc = "signed_ord" |
| 136 |
self.print_string( |
| 137 |
"_" + name, "".join(chr(c & 0xff) for c in lst), category) |
| 138 |
self.print_code("""def %s(index): |
| 139 |
try: |
| 140 |
return %s(_%s[index]) |
| 141 |
except IndexError: |
| 142 |
raise KeyError""" % (name, unwrapfunc, name)) |
| 143 |
return |
| 144 |
unwrapfunc = "intmask" |
| 145 |
if itemsize == 2: |
| 146 |
if unsigned: |
| 147 |
typ = r_ushort |
| 148 |
conv_func = "_all_ushort" |
| 149 |
else: |
| 150 |
typ = r_short |
| 151 |
conv_func = "_all_short" |
| 152 |
elif itemsize == 4: |
| 153 |
if unsigned: |
| 154 |
typ = r_uint32 |
| 155 |
conv_func = "_all_uint32" |
| 156 |
else: |
| 157 |
typ = r_int32 |
| 158 |
conv_func = "_all_int32" |
| 159 |
size = len(lst) * itemsize + WORDSIZE * 2 |
| 160 |
self.print_comment("estimated %s KiB" % round(size / 1024., 2)) |
| 161 |
self._estimate(name, size, category) |
| 162 |
print("_%s = [" % name, file=self.outfile) |
| 163 |
chunksize = 16 |
| 164 |
res = [] |
| 165 |
for element in lst: |
| 166 |
assert intmask(typ(element)) == element |
| 167 |
res.append(str(element)) |
| 168 |
if len(res) == chunksize: |
| 169 |
print(", ".join(res) + ",", file=self.outfile) |
| 170 |
res = [] |
| 171 |
if res: |
| 172 |
print(", ".join(res) + ",", file=self.outfile) |
| 173 |
print("]", file=self.outfile) |
| 174 |
print("_%s = %s(_%s)" % (name, conv_func, name), file=self.outfile) |
| 175 |
self.print_code("""def %s(index): |
| 176 |
try: |
| 177 |
return %s(_%s[index]) |
| 178 |
except IndexError: |
| 179 |
raise KeyError""" % (name, unwrapfunc, name)) |
| 180 |
return |
| 181 |
|
| 182 |
def print_string(self, name, string, category=None): |
| 183 |
chunksize = 20 |
| 184 |
self._estimate_string(name, string, category) |
| 185 |
result = '' |
| 186 |
print("%s = (" % name, file=self.outfile) |
| 187 |
for i in range(0, len(string), chunksize): |
| 188 |
print(repr(string[i : i + chunksize]), file=self.outfile) |
| 189 |
print(")", file=self.outfile) |
| 190 |
|
| 191 |
def print_dict(self, name, d, category=None, outfunc=repr): |
| 192 |
items = d.items() |
| 193 |
items.sort() |
| 194 |
print('%s = {' % name, file=self.outfile) |
| 195 |
for key, value in items: |
| 196 |
self._estimate_any(name, key, category) |
| 197 |
self._estimate_any(name, value, category) |
| 198 |
print('%s: %s,' % (outfunc(key), outfunc(value)), file=self.outfile) |
| 199 |
|
| 200 |
size = len(d) * 16 + WORDSIZE * 4 |
| 201 |
self._estimate(name, size, category) |
| 202 |
print('}', file=self.outfile) |
| 203 |
print(file=self.outfile) |
| 204 |
|
| 205 |
def print_set(self, name, s, category=None): |
| 206 |
items = sorted(s) |
| 207 |
print('%s = {' % name, file=self.outfile) |
| 208 |
for key in items: |
| 209 |
self._estimate_any(name, key, category) |
| 210 |
print('%r: None,' % (key, ), file=self.outfile) |
| 211 |
|
| 212 |
size = len(s) * 16 + WORDSIZE * 4 |
| 213 |
self._estimate(name, size, category) |
| 214 |
print('}', file=self.outfile) |
| 215 |
print(file=self.outfile) |
| 216 |
|
| 217 |
def write(self, s): |
| 218 |
self._estimate("unknown", len(s)) |
| 219 |
return self.outfile.write(s) |
| 220 |
|
| 221 |
def print_code(self, s): |
| 222 |
self._estimate("code", len(s)) |
| 223 |
print(s, file=self.outfile) |
| 224 |
|
| 225 |
def print_uncounted(self, s): |
| 226 |
print(s, file=self.outfile) |
| 227 |
|
| 228 |
def print_comment(self, s): |
| 229 |
print("# " + "\n# ".join(s.splitlines()), file=self.outfile) |
| 230 |
|
| 231 |
def _estimate_any(self, name, obj, category): |
| 232 |
if isinstance(obj, str): |
| 233 |
return self._estimate_string(name, obj, category) |
| 234 |
if isinstance(obj, int): |
| 235 |
return self._estimate(name, WORDSIZE, category) |
| 236 |
if isinstance(obj, float): |
| 237 |
return self._estimate(name, WORDSIZE, category) |
| 238 |
if isinstance(obj, list): |
| 239 |
for elt in obj: |
| 240 |
if not isinstance(elt, int): |
| 241 |
return self._estimate(name, elt, category) |
| 242 |
return self._estimate(name, WORDSIZE * (len(obj) + 1), category) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
def _estimate_string(self, name, string, category=None): |
| 247 |
|
| 248 |
if string in self.seen_strings: |
| 249 |
return |
| 250 |
self.seen_strings.add(string) |
| 251 |
size = len(string) + 8 * 2 |
| 252 |
self._estimate(name, size, category) |
| 253 |
|
| 254 |
def _estimate(self, name, size, category=None): |
| 255 |
if category is None: |
| 256 |
category = name |
| 257 |
self.size_estimates[category] += size |