Skip to content

perf: encode file contents once in tree-sitter scoring - #984

Closed
aliangm wants to merge 3 commits into
entrius:testfrom
aliangm:fix-tree-sitter-double-encode
Closed

perf: encode file contents once in tree-sitter scoring#984
aliangm wants to merge 3 commits into
entrius:testfrom
aliangm:fix-tree-sitter-double-encode

Conversation

@aliangm

@aliangm aliangm commented May 4, 2026

Copy link
Copy Markdown
Contributor

Description:

Summary

  • new_content was encoded to UTF-8 twice — once for the MAX_FILE_SIZE_BYTES size check at tree_sitter_scoring.py:312 and again inside parse_code() at line 77. old_content was likewise re-encoded inside parse_code().
  • Encode each side once at the call site in calculate_token_score_from_file_changes() and pass bytes through. parse_code() and score_tree_diff() now accept Union[str, bytes] so existing str callers (tests) keep working unchanged.
  • Reorders the skip checks so supports_tree_sitter(ext) short-circuits before any encoding — unsupported extensions no longer pay encode cost.

Behavior change

A file that is both unsupported-extension and larger than MAX_FILE_SIZE_BYTES will now log as skipped-unsupported instead of skipped-large. Same final outcome (skipped, score 0), only the metric label differs.

Test plan

  • Existing token-scoring integration tests pass (pytest tests/validator/test_token_scoring_integration.py).
  • Spot-check scoring totals on a real PR before/after — should be identical.
  • Confirm large .py file is skipped as skipped-large (size check still effective).
  • Confirm large unsupported file is skipped as skipped-unsupported (new behavior).

Fixes #983

new_content was encoded to UTF-8 twice — once for the MAX_FILE_SIZE_BYTES
size check and again inside parse_code() before tree-sitter parsing.
old_content was likewise re-encoded inside parse_code().

Encode each side once at the call site and pass bytes through.
parse_code() and score_tree_diff() now accept Union[str, bytes] so
existing str callers (tests) keep working unchanged.

Also reorders the skip checks so unsupported extensions short-circuit
before any encoding work.
@xiao-xiao-mao xiao-xiao-mao Bot added the enhancement New feature or request label May 4, 2026
@anderdc

anderdc commented May 5, 2026

Copy link
Copy Markdown
Collaborator

fix CI

@aliangm

aliangm commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

@anderdc Fixed CI, ty

@anderdc

anderdc commented May 9, 2026

Copy link
Copy Markdown
Collaborator

fix conflicts

@anderdc anderdc closed this May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant UTF-8 encoding of file contents in tree-sitter scoring

2 participants