From 647d44a07747c79d36e51d369456af88c2878ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alice=20Bevan=E2=80=93McGregor?= Date: Sun, 6 Dec 2015 05:26:15 -0500 Subject: [PATCH] Now encodes line mapping. As a module-level list named `__mapping__`, and as a compressed delta encoding for production use as ``__gzmapping__``. The compressed version is the base-84 representation of a gzipped stream of packed integer bytes defining the difference in line number from line to line. --- cinje/block/module.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/cinje/block/module.py b/cinje/block/module.py index 7fa1b95..62e20b3 100644 --- a/cinje/block/module.py +++ b/cinje/block/module.py @@ -1,9 +1,27 @@ # encoding: utf-8 +from __future__ import unicode_literals + +from gzip import compress, decompress +from base64 import b85encode, b85decode from pprint import pformat from collections import deque -from ..util import py, Line +from ..util import py, Line, iterate + + +def red(numbers): + """Encode the deltas to reduce entropy.""" + + line = 0 + deltas = [] + + for value in numbers: + deltas.append(value - line) + line = value + + return b85encode(compress(b''.join(chr(i).encode('latin1') for i in deltas))).decode('latin1') + class Module(object): @@ -55,15 +73,14 @@ def __call__(self, context): context.templates = [] # Snapshot the line number mapping. - # TODO: Run-length encode the line number deltas, 'cause damn, this is a lot of data. mapping = deque(context.mapping) mapping.reverse() - mapping = deque(pformat(list(mapping), indent=0, width=105).split('\n')) - yield Line(0, '') - yield Line(0, '__mapping__ = ' + mapping.popleft()) - for line in mapping: - yield Line(0, line) + + if __debug__: + yield Line(0, '__mapping__ = [' + ','.join(str(i) for i in mapping) + ']') + + yield Line(0, '__gzmapping__ = rb"""' + red(mapping) + '"""') context.flag.remove('init')