Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: add quick path for looking up too long strings in the keyword table #3664

Merged
merged 1 commit into from
Mar 17, 2023

Conversation

techee
Copy link
Contributor

@techee techee commented Mar 14, 2023

Parser code like

vString *str = vStringNew();
while (someCondition)
{
    int c = getcFromInputFile();
    vStringPut(str, c);
    if (lookupCaseKeyword (vStringValue(str), some_lang))
    {
        do_stuff();
        vStringClear(str);
    }
}

is prone to quadratic complexity because when someCondition isn't satisfied in the parsed file, the string str grows by one in each iteration and in each iteration lookupCaseKeyword() has to go through strlen(str) characters to compute the hash.

Since we know the maximum length of the strings inside the keyword hash table, we can store this value and if the queried string is longer than this value, we can be sure it isn't present in the hash table and return quickly without having to compute the full hash of the string.

Parser code like

vString *str = vStringNew();
while (someCondition)
{
    int c = getcFromInputFile();
    vStringPut(str, c);
    if (lookupCaseKeyword (vStringValue(str), some_lang))
    {
        do_stuff();
        vStringClear(str);
    }
}

is prone to quadratic complexity because when someCondition isn't
satisfied in the parsed file, the string str grows by one in each
iteration and in each iteration lookupCaseKeyword() has to go
through strlen(str) characters to compute the hash.

Since we know the maximum length of the strings inside the keyword
hash table, we can store this value and if the queried string is
longer than this value, we can be sure it isn't present in the hash
table and return quickly without having to compute the full hash of the
string.
@codecov
Copy link

codecov bot commented Mar 14, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (bca2e86) 82.83% compared to head (9a02e92) 82.83%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3664   +/-   ##
=======================================
  Coverage   82.83%   82.83%           
=======================================
  Files         223      223           
  Lines       54508    54518   +10     
=======================================
+ Hits        45149    45159   +10     
  Misses       9359     9359           
Impacted Files Coverage Δ
main/keyword.c 78.67% <100.00%> (+1.69%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@masatake masatake changed the title Add quick path for looking up too long strings in the keyword table main: add quick path for looking up too long strings in the keyword table Mar 14, 2023
@masatake masatake merged commit 6e720ab into universal-ctags:master Mar 17, 2023
@masatake
Copy link
Member

Thank you,

@masatake
Copy link
Member

Now we can add

int lookupKeywordVString (const vString *string, langType language);

:-)

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.

2 participants