Skip to content

Replace O(n) function lookup with O(1) hash map and fix memory leaks#146

Merged
leo-aa88 merged 4 commits intomainfrom
copilot/improve-slow-code-efficiency
Nov 12, 2025
Merged

Replace O(n) function lookup with O(1) hash map and fix memory leaks#146
leo-aa88 merged 4 commits intomainfrom
copilot/improve-slow-code-efficiency

Conversation

Copy link
Contributor

Copilot AI commented Nov 12, 2025

Function lookups used linear search through a linked list (strcmp on every node), degrading performance with program size. Additionally, functions were created twice (parse + execute phases) causing memory leaks.

Changes

Function storage

  • Replaced function_table linked list with function_map hash map
  • Removed Function.next pointer from struct
  • get_function() now O(1) instead of O(n)

Memory management

  • Added duplicate check in create_function() to prevent double-creation
  • Iterate hash map in free_function_table() to properly free all functions
  • Added hm_free_shallow() for hash maps storing pointers to externally-managed memory

Code quality

  • Replaced strdup() with safe_strdup() for consistent error handling
  • Cache strlen() results in add_variable_to_scope() to avoid redundant calculations

Example

Before:

Function *get_function(const char *name) {
    Function *func = function_table;
    while (func != NULL) {
        if (strcmp(func->name, name) == 0)  // O(n) scan
            return func;
        func = func->next;
    }
    return NULL;
}

After:

Function *get_function(const char *name) {
    if (!function_map || !name) return NULL;
    Function **func_ptr = (Function **)hm_get(function_map, name, strlen(name));  // O(1)
    return func_ptr ? *func_ptr : NULL;
}

Testing

All 63 unit tests and 69 valgrind memory tests pass with zero leaks detected.

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 12, 2025 01:25
Co-authored-by: SIGMazer <88988252+SIGMazer@users.noreply.github.com>
…eaks

Co-authored-by: SIGMazer <88988252+SIGMazer@users.noreply.github.com>
@SIGMazer SIGMazer marked this pull request as ready for review November 12, 2025 01:43
@SIGMazer SIGMazer self-requested a review as a code owner November 12, 2025 01:43
Copilot AI changed the title [WIP] Identify and suggest improvements to slow code Replace O(n) function lookup with O(1) hash map and fix memory leaks Nov 12, 2025
@SIGMazer
Copy link
Member

@leo-aa88 check this out :"

@leo-aa88
Copy link
Member

@leo-aa88 check this out :"

image

@leo-aa88
Copy link
Member

LGTM

@SIGMazer SIGMazer self-requested a review November 12, 2025 03:05
@SIGMazer SIGMazer requested a review from leo-aa88 November 12, 2025 03:06
@SIGMazer
Copy link
Member

@leo-aa88 can u approve this again

@SIGMazer
Copy link
Member

@copilot u can merge the pr now

Copy link
Contributor Author

Copilot AI commented Nov 12, 2025

@copilot u can merge the pr now

I don't have permission to merge PRs directly. The PR can be merged by maintainers with write access through GitHub's interface. The PR is ready - all tests pass and it has been approved.

@SIGMazer
Copy link
Member

somehow i can't merge it can u do @leo-aa88

@leo-aa88 leo-aa88 merged commit de2e52f into main Nov 12, 2025
4 of 5 checks passed
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.

3 participants