Skip to content

extracted code to prevent duplicated execution of string index search - #13

Merged
JesseHerrick merged 1 commit into
remoteoss:mainfrom
M-T3K:perf/extract-param-content
Apr 10, 2026
Merged

extracted code to prevent duplicated execution of string index search#13
JesseHerrick merged 1 commit into
remoteoss:mainfrom
M-T3K:perf/extract-param-content

Conversation

@M-T3K

@M-T3K M-T3K commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Note: Apologies, I found no PR format established for the project.

This PR is about avoiding duplicate work by extracting the common calls accross parser.ExtractArity(line, name) and parser.CountDefaultParams(line, name). to the string functions strings.Index(line, funcName) and parenIdx := strings.IndexByte(rest, '('). String functions are typically considered expensive, as require browsing & comparing many bytes.

Consequences and implications: since the code was being run twice (and in every case that CountDefaultParams was called, ExtractArity had been called beforehand, meaning it happened quite often), by isolating the code that run twice we can avoid the expensive, constant string index operations, reducing in less instructions.

Implementation:

  • I propose the creation of parser.FindParamContent() to do the common string index operations.
  • Then, I create ArityFromParams & DefaultsFromParams to contain the individual parts of what before was ExtractArity CountDefaultParams
  • I propose maintaining the original function calls parser.ExtractArity(line, name) & parser.CountDefaultParams(line, name), as they were being exported. I adapt them to call to the new functions.
  • I update comments to reflect the changes proposed.

Note

Low Risk
Low risk refactor limited to parsing helpers for function arity/default-parameter detection; main risk is subtle behavior drift in edge-case Elixir function definitions.

Overview
Reduces duplicated string scanning when parsing Elixir function definitions by extracting shared lookup logic into parser.FindParamContent and splitting computation into ArityFromParams/DefaultsFromParams.

Updates both the indexer (ParseText) and LSP buffer scanning (FindBufferFunctions) to reuse the extracted parameter substring rather than recomputing arity and default counts separately, while keeping the existing exported APIs ExtractArity and CountDefaultParams as thin wrappers.

Reviewed by Cursor Bugbot for commit 8571a67. Bugbot is set up for automated code reviews on this repo. Configure here.

@JesseHerrick

Copy link
Copy Markdown
Member

Thanks for your contribution and great thinking @M-T3K! I'll review and test this locally later.

@JesseHerrick

Copy link
Copy Markdown
Member

Code looks good - good to not redo work when parsing.

Ran benchmarks against a large Elixir codebase. This resulted in a minor improvement. Thank you!

Benchmark 1: main
  Time (mean ± σ):     11.721 s ±  0.132 s    [User: 19.523 s, System: 6.006 s]
  Range (min … max):   11.559 s … 11.877 s    5 runs

Benchmark 2: perf/extract-param-content
  Time (mean ± σ):     11.535 s ±  0.041 s    [User: 19.518 s, System: 5.915 s]
  Range (min … max):   11.480 s … 11.581 s    5 runs

Summary
  perf/extract-param-content ran
    1.02 ± 0.01 times faster than main

@JesseHerrick
JesseHerrick merged commit dbd8365 into remoteoss:main Apr 10, 2026
4 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.

2 participants