Skip to content

Instantly share code, notes, and snippets.

1: LOAD_FAST <index1>
LOAD_CONST <value1>
COMPARE_OP <operation1>
JUMP_IF_FALSE_OR_POP <return>
2: LOAD_FAST <index2>
LOAD_CONST <value2>
COMPARE_OP <operation2>
JUMP_IF_FALSE_OR_POP <return>
... repeated for each value except the last one ...
N: LOAD_FAST <indexN>
from dis import cmp_op
from typing import List, NamedTuple, Optional
from fast_comparator import Comparison
class KeyRange(NamedTuple):
exact_value: Optional[bytes]
min_value: Optional[bytes]
import dis
import unittest
from humu.storage.impl.fast_comparator import Comparison, makeFastComparator
class testFastComparator(unittest.TestCase):
def testSimpleComparator(self):
# This should make a function
# def compareTwo(x1, x2):
@yonatanzunger
yonatanzunger / fast_comparator.py
Created August 11, 2022 00:22
Python Codegen Example: Main code
import dis
import io
from types import CodeType, FunctionType
from typing import Any, Callable, List, NamedTuple, Tuple
# Opcodes
_LOAD_FAST = dis.opname.index('LOAD_FAST')
_LOAD_CONST = dis.opname.index('LOAD_CONST')
_COMPARE_OP = dis.opname.index('COMPARE_OP')
_JUMP_IF_FALSE_OR_POP = dis.opname.index('JUMP_IF_FALSE_OR_POP')
@yonatanzunger
yonatanzunger / fast_comparator.py
Created August 11, 2022 00:05
Advanced Python: High Performance with Codegen (Example)
import dis
import io
from types import CodeType, FunctionType
from typing import Any, Callable, List, NamedTuple, Tuple
# Opcodes
_LOAD_FAST = dis.opname.index('LOAD_FAST')
_LOAD_CONST = dis.opname.index('LOAD_CONST')
_COMPARE_OP = dis.opname.index('COMPARE_OP')
_JUMP_IF_FALSE_OR_POP = dis.opname.index('JUMP_IF_FALSE_OR_POP')