Skip to content

Commit

Permalink
feat: reduce the size of literal identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr committed Oct 21, 2024
1 parent 81689f3 commit b6ffcaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
17 changes: 14 additions & 3 deletions nada_dsl/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

OPERATION_ID_COUNTER = 0


def next_operation_id() -> int:
"""Returns the next value of the operation id counter."""
global OPERATION_ID_COUNTER
Expand Down Expand Up @@ -55,6 +56,9 @@ def to_mir(self):
# The key is the operation identifier, the value the operation
AST_OPERATIONS: Dict[int, ASTOperation] = SortedDict()

# Map of literal hashes to index
LITERALS: Dict[str, int] = {}


@dataclass
class BinaryASTOperation(ASTOperation):
Expand Down Expand Up @@ -167,7 +171,7 @@ class LiteralASTOperation(ASTOperation):

name: str
value: object
literal_name: str
literal_index: str

def __init__(
self,
Expand All @@ -184,16 +188,23 @@ def __init__(
self.source_ref = source_ref
# Generate a unique name depending on the value and type
# to prevent duplicating literals in the bytecode.
self.literal_name = hashlib.md5(
literal_name = hashlib.md5(
(str(self.value) + str(self.ty)).encode("UTF-8")
).hexdigest()

# Replace the literal name by an index so it is shorter
if literal_name not in LITERALS:
LITERALS[literal_name] = len(LITERALS)

self.literal_index = str(LITERALS[literal_name])

super().__init__(id=self.id, source_ref=self.source_ref, ty=self.ty)

def to_mir(self):
return {
"LiteralReference": {
"id": self.id,
"refers_to": self.literal_name,
"refers_to": self.literal_index,
"type": self.ty,
"source_ref_index": self.source_ref.to_index(),
}
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/compiler_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def process_operation(
processed_operation = ProcessOperationOutput(operation.to_mir(), None)
elif isinstance(operation, LiteralASTOperation):

LITERALS[operation.literal_name] = (str(operation.value), operation.ty)
LITERALS[operation.literal_index] = (str(operation.value), operation.ty)
processed_operation = ProcessOperationOutput(operation.to_mir(), None)
elif isinstance(
operation, (MapASTOperation, ReduceASTOperation, NadaFunctionCallASTOperation)
Expand Down
1 change: 0 additions & 1 deletion nada_dsl/compiler_frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ def test_binary_operator_integer_publicinteger(operator, name, ty):
right_ast = AST_OPERATIONS[inner["right"]]
assert isinstance(left_ast, LiteralASTOperation)
assert left_ast.value == -3
assert len(left_ast.literal_name) == 32
assert isinstance(right_ast, InputASTOperation)
assert right_ast.name == "right"
assert inner["type"] == to_type(ty)
Expand Down

0 comments on commit b6ffcaa

Please sign in to comment.