Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixes

- **Syncing a changed file in a large Python project is faster.** When a Python class has no method of the name being linked (a test class that passes `Handler.__init__` as a value, say), the resolver checks every class with that method for one that derives from it. For each class whose base is a package import (`import unittest`, then `unittest.TestCase`) it parsed the whole file again, and its cache holds only a few files, so two such references took about a second each. A plain one-line `import a.b` is now read from its line; other forms still use the parse. In CPython a one-file sync takes a median 5.5 s instead of 7.7 s on Node and 5.2 s instead of 8.1 s on Bun (4 alternating runs per build, ranges not overlapping; the phase that retries unresolved references drops from 4.6 s to 2.3 s). A full index is within the run-to-run variation, and the graph is the same on CPython and Django (no edge differs).
- **Questions to the MCP server are faster in a project with no `codegraph.json`.** While ranking results, CodeGraph checked for the project's `codegraph.json` once per result path, and with no such file each check raised and caught an error. It now asks the file system without raising, so `codegraph_explore` and the other query tools spend less CPU on most projects. Editing, adding or deleting `codegraph.json` is still picked up without a restart (#491).
- **Indexing a project with a bundled minified JavaScript file is faster.** For each reference in a one-line file such as `jquery.min.js`, the resolver counted characters from the start of the line to find the reference's column, many times per reference. It now skips straight to the column when the text before it is ASCII. Alamofire, whose docs folder carries jQuery, resolves its references in a median 0.93 s instead of 1.25 s (profiling runs on a busy host, 5,614 nodes and 13,673 edges either way). Separately, the resolver now looks nodes up by query only when there are fewer than 15,000 pending references and fewer than 0.3 per node; at either limit it loads the node table. A full index of a small project such as gin (about 4.4 references per node) therefore loads the table, saving about 60 ms of its one-second index, while a one-file sync still queries. In the 2026-10-12 benchmark on Node, a full index of Alamofire takes 1.81 s against upstream's 1.73 s and gin 1.15 s against 1.13 s ([results](docs/benchmarks/fork-vs-upstream-node-bun-2026-10-12.md)).
- **Indexing a project with long minified lines that contain a non-ASCII character is faster.** The resolver turns a reference's column into a byte offset many times per reference, and when any character before the column was not ASCII it walked every character on the line each time. It now counts a 64-byte block at a time and walks characters only inside the block that holds the column. Indexing JetBrains Exposed, whose docs folder carries minified scripts, takes a median 25.7 s instead of 37.0 s (5 alternating runs per build, baseline against baseline within 0.3%; 28,689 nodes and 82,842 edges either way). nocodb, with no long lines, is unchanged within the run-to-run band (6.53 s against 6.74 s).
Expand Down
47 changes: 46 additions & 1 deletion codegraph-kernel/src/resolve/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,15 @@ impl KernelResolver {
return false;
}
let Some(file) = self.read_file(&r.file_path) else { return false };
let Some(tree) = self.parsed_tree(&file, r) else { return false };
let row = (binding.line - 1).max(0) as usize;
let Some(line) = file.get(row) else { return false };
// A plain one-line `import a.b, c` names its modules on the line;
// reading the tree parses the whole file again for each class that
// imports a package, and the tree cache holds only a few files.
if let Some(named) = plain_import_names(line, source) {
return named;
}
let Some(tree) = self.parsed_tree(&file, r) else { return false };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
let point = tree_sitter::Point::new(row, line.len() - line.trim_start().len());
let mut node = tree.root_node().named_descendant_for_point_range(point, point);
while let Some(current) = node {
Expand Down Expand Up @@ -1471,3 +1477,42 @@ impl KernelResolver {
fn is_declaration_file(path: &str) -> bool {
[".d.ts", ".d.mts", ".d.cts"].iter().any(|ext| path.ends_with(ext))
}

/// Whether the `import` statement on `line` names the module `source` as a
/// plain dotted name (`import a.b, c`, not `import a.b as c`). `None` when
/// the line is not a plain one-line `import` statement, so the caller reads
/// the tree.
fn plain_import_names(line: &str, source: &str) -> Option<bool> {
let clause = line.trim_start().strip_prefix("import")?;
if !clause.starts_with([' ', '\t']) {
return None;
}
let clause = clause.split(['#', ';']).next().unwrap_or(clause);
if clause.contains(['\\', '(', '"', '\'']) {
return None;
}
Some(clause.split(',').any(|name| name.trim() == source))
Comment thread
bompus marked this conversation as resolved.
}

#[cfg(test)]
mod plain_import_tests {
use super::plain_import_names;

#[test]
fn names_a_dotted_module_on_a_plain_import_line() {
assert_eq!(plain_import_names("import os.path", "os.path"), Some(true));
assert_eq!(plain_import_names(" import os, os.path # paths", "os.path"), Some(true));
assert_eq!(plain_import_names("import os.path; x = 1", "os.path"), Some(true));
assert_eq!(plain_import_names("import os", "os.path"), Some(false));
assert_eq!(plain_import_names("import os.path as osp", "os.path"), Some(false));
}

#[test]
fn leaves_other_shapes_to_the_tree() {
assert_eq!(plain_import_names("from os import path", "os.path"), None);
assert_eq!(plain_import_names("importlib = 1", "importlib"), None);
assert_eq!(plain_import_names("x = 1; import os", "os"), None);
assert_eq!(plain_import_names("import os, \\", "os"), None);
assert_eq!(plain_import_names("import (os)", "os"), None);
}
}
Loading