Skip to content

perf/fix: optimize hash table operations and fix AST traversal bugs in semantic analysis - #2

Open
RustamSheoran wants to merge 1 commit into
pie-314:mainfrom
RustamSheoran:main
Open

perf/fix: optimize hash table operations and fix AST traversal bugs in semantic analysis#2
RustamSheoran wants to merge 1 commit into
pie-314:mainfrom
RustamSheoran:main

Conversation

@RustamSheoran

@RustamSheoran RustamSheoran commented Jul 4, 2026

Copy link
Copy Markdown

No description provided.

@RustamSheoran

Copy link
Copy Markdown
Author

@pie-314

@RustamSheoran

RustamSheoran commented Jul 4, 2026

Copy link
Copy Markdown
Author

@pie-314

Technical Overview of Changes

This patch resolves AST traversal gaps in the semantic analysis phase and implements key performance optimizations within the compiler's symbol table scope engine.

1. Semantic Analyzer AST Traversal Fixes (semantic/semantic.c)

  • NODE_CALL Argument Traversal:
    The parser stores function call arguments in the children array (ASTNode **children) while leaving node->right as NULL. The semantic analyzer previously checked node->right, skipping argument verification entirely. This has been updated to loop through and analyze all argument nodes stored in node->children.
  • NODE_IF / NODE_ELIF Conditional Branches:
    Subsequent elif and else blocks are parsed and appended to the parent NODE_IF node's children array. The analyzer previously only traversed the initial left (condition) and right (then-block) branches, completely bypassing semantic checks for other branches. We added a loop to recursively analyze all conditional branches in the children array.

2. Symbol Table Performance Optimizations (semantic/symbol_table.h, semantic/symbol_table.c)

  • Cached Hash Values in Hash Table Entries:
    Calculating the string hash (via djb2) is an $O(L)$ operation, where $L$ is the string length. During a table resize (st_resize), re-calculating the hash for every key to determine new bucket locations introduces high overhead. We added an unsigned long hash field to the Entry struct to cache the key's hash value during insertion, reducing key re-indexing to an $O(1)$ operation.
  • Bitwise Modulo Substitution:
    Integer division/modulo (h % size) is an expensive instruction on modern CPU architectures (often taking 10–40 clock cycles). Since the symbol table capacity is initialized to $128$ and strictly doubled on resize, the capacity size is guaranteed to be a power of two ($2^k$). This allows us to substitute all modulo operations with a bitwise AND mask (h & (size - 1)), which executes in a single CPU cycle.

@RustamSheoran RustamSheoran changed the title perf/fix: optimize hash table operations and fix AST traversal bugs i… perf/fix: optimize hash table operations and fix AST traversal bugs in semantic analysis Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant