Skip to content

Commit ca95a59

Browse files
behdadclaude
andcommitted
Implement dual compression with #ifdef __OPTIMIZE_SIZE__
Add support for --compression 1,9 to generate two variants: - Speed-optimized (compression=1) - Size-optimized (compression=9) Wrapped in #ifdef __OPTIMIZE_SIZE__ so the same generated file adapts to compiler flags (-Os vs -O3). Example: python -m packTab --compression 1,9 data.txt Also fixed: - Remove duplicate sys import that caused UnboundLocalError - Replace type() == comparisons with isinstance() (E721 flake8) All 172 tests pass. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent cbd2af2 commit ca95a59

2 files changed

Lines changed: 38 additions & 17 deletions

File tree

packTab/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def binaryBitsFor(minV, maxV):
151151
8
152152
"""
153153

154-
if type(minV) != int or type(maxV) != int:
154+
if not isinstance(minV, int) or not isinstance(maxV, int):
155155
return 8
156156

157157
if minV > maxV:
@@ -296,7 +296,7 @@ def type_name(self, typ):
296296
return "%sint%s_t" % (signed, size)
297297

298298
def type_for(self, minV, maxV):
299-
if type(minV) != int or type(maxV) != int:
299+
if not isinstance(minV, int) or not isinstance(maxV, int):
300300
return "uint8_t"
301301

302302
if 0 <= minV and maxV <= 255:
@@ -392,7 +392,7 @@ def type_name(self, typ):
392392
return "%s%s" % (signed, size)
393393

394394
def type_for(self, minV, maxV):
395-
if type(minV) != int or type(maxV) != int:
395+
if not isinstance(minV, int) or not isinstance(maxV, int):
396396
return "u8"
397397

398398
if 0 <= minV and maxV <= 255:
@@ -1180,7 +1180,7 @@ def __init__(self, data, default):
11801180
bias = 0
11811181
mult = 1
11821182
unitBits = binaryBitsFor(self.minV, self.maxV)
1183-
if type(self.minV) == int and type(self.maxV) == int:
1183+
if isinstance(self.minV, int) and isinstance(self.maxV, int):
11841184
unitBits, bias, mult = _best_reduction(data, self.minV, self.maxV)
11851185

11861186
# Try identity subtraction: store data[i] - i instead.
@@ -1230,7 +1230,7 @@ def __init__(self, data, default):
12301230

12311231
self.unitBits = unitBits
12321232
self.extraOps = subByteAccessOps if self.unitBits < 8 else 0
1233-
self.identity = identity if type(self.minV) == int else False
1233+
self.identity = identity if isinstance(self.minV, int) else False
12341234
if self.identity:
12351235
self.extraOps += 1
12361236
self.bias = bias

packTab/__main__.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def main(args=None):
107107
parser.error(f"no data in input file: {parsed.input}")
108108
elif not parsed.data:
109109
# Read from stdin
110-
import sys
111-
112110
stdin_text = sys.stdin.read().strip()
113111
if not stdin_text:
114112
parser.error("no data provided (use positional args, -i, or stdin)")
@@ -227,19 +225,42 @@ def main(args=None):
227225
return 0
228226

229227
# Normal code generation path
230-
solution = pack_table(parsed.data, parsed.default, compression=compression_values[0])
231-
232228
lang = languageClasses[language](unsafe_array_access=parsed.unsafe)
233229

234-
code = Code(parsed.name)
235-
solution.genCode(code, "get", language=lang, private=False)
230+
# Determine output file
231+
output_file = open(parsed.output, "w") if parsed.output else sys.stdout
236232

237-
# Handle output file
238-
if parsed.output:
239-
with open(parsed.output, "w") as f:
240-
code.print_code(language=lang, file=f)
241-
else:
242-
code.print_code(language=lang)
233+
try:
234+
if len(compression_values) == 1:
235+
# Single compression - generate normally
236+
solution = pack_table(parsed.data, parsed.default, compression=compression_values[0])
237+
code = Code(parsed.name)
238+
solution.genCode(code, "get", language=lang, private=False)
239+
code.print_code(language=lang, file=output_file)
240+
else:
241+
# Dual compression - generate both with #ifdef
242+
solution_speed = pack_table(parsed.data, parsed.default, compression=compression_values[0])
243+
solution_size = pack_table(parsed.data, parsed.default, compression=compression_values[1])
244+
245+
code_speed = Code(parsed.name)
246+
solution_speed.genCode(code_speed, "get", language=lang, private=False)
247+
248+
code_size = Code(parsed.name)
249+
solution_size.genCode(code_size, "get", language=lang, private=False)
250+
251+
# Print with #ifdef wrapper
252+
print("#ifdef __OPTIMIZE_SIZE__", file=output_file)
253+
print(file=output_file)
254+
code_size.print_code(language=lang, file=output_file)
255+
print(file=output_file)
256+
print("#else /* optimize for speed */", file=output_file)
257+
print(file=output_file)
258+
code_speed.print_code(language=lang, file=output_file)
259+
print(file=output_file)
260+
print("#endif", file=output_file)
261+
finally:
262+
if parsed.output:
263+
output_file.close()
243264

244265
return 0
245266

0 commit comments

Comments
 (0)