Skip to content

Cache comment checker tree-sitter queries - #5

Merged
code-yeongyu merged 1 commit into
masterfrom
ulw/rule-comment-baseline-20260518
May 18, 2026
Merged

code-yeongyu merged 1 commit into
masterfrom
ulw/rule-comment-baseline-20260518

Conversation

@code-yeongyu

@code-yeongyu code-yeongyu commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • Cache tree-sitter queries per detector/language to avoid repeated query compilation.
  • Add tests for cache reuse behavior.

Improvement table

Benchmark Initial baseline Final benchmark Delta
BenchmarkCommentDetectorDetectPythonNoDocstrings-18 714618 ns/op, 1056 B/op, 22 allocs/op 11275 ns/op, 1024 B/op, 19 allocs/op ~63.4x faster, -32 B/op, -3 allocs/op
BenchmarkCommentDetectorDetectPythonWithDocstrings-18 3617623 ns/op, 2096 B/op, 43 allocs/op 14156 ns/op, 1800 B/op, 34 allocs/op ~255.6x faster, -296 B/op, -9 allocs/op
Unsupported extension 12.71 ns/op, 0 B/op, 0 allocs/op unchanged fast path Zero-allocation unsupported-file path preserved

Verification

  • go build -o comment-checker ./cmd/comment-checker
  • go test ./...
  • go test -race ./pkg/core
  • go test -bench='BenchmarkCommentDetectorDetectPython(NoDocstrings|WithDocstrings)$' -benchmem ./pkg/core
  • manual CLI pass/warning QA

Review

  • Security, QA, context, and goal-verifier lanes completed locally.
  • Cubic review passed before merge.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Confidence score: 3/5

  • There is a concrete resource-management risk in pkg/core/detector.go: cached *sitter.Query objects are not closed, so C-backed query memory can leak for the detector lifetime in long-running use.
  • The test in pkg/core/detector_test.go currently validates cache size only, which may miss a regression where queries are recompiled each call under the same key; this leaves cache-reuse behavior under-verified.
  • Given the moderate severities and fairly strong confidence, this looks mergeable with caution, but fixing the query cleanup and strengthening reuse assertions would reduce regression risk.
  • Pay close attention to pkg/core/detector.go and pkg/core/detector_test.go - query lifecycle cleanup and true cache-reuse validation need tightening.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="pkg/core/detector.go">

<violation number="1" location="pkg/core/detector.go:171">
P2: Cached `*sitter.Query` instances are never closed, so query C-resources can leak for the lifetime of each detector.</violation>
</file>

<file name="pkg/core/detector_test.go">

<violation number="1" location="pkg/core/detector_test.go:120">
P2: This only checks cache size, so it won’t fail if the query is recompiled on every call and stored under the same key. Verify reuse by comparing the cached query pointer across calls (or by counting compilations) instead.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Re-trigger cubic

Comment thread pkg/core/detector.go
return nil
}

cache[langName] = compiledQuery

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Cached *sitter.Query instances are never closed, so query C-resources can leak for the lifetime of each detector.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At pkg/core/detector.go, line 171:

<comment>Cached `*sitter.Query` instances are never closed, so query C-resources can leak for the lifetime of each detector.</comment>

<file context>
@@ -137,6 +131,47 @@ func (d *CommentDetector) detectDocstrings(sourceCode []byte, filePath string, l
+		return nil
+	}
+
+	cache[langName] = compiledQuery
+	return compiledQuery
+}
</file context>

Comment thread pkg/core/detector_test.go
// then
detector.queryMu.Lock()
defer detector.queryMu.Unlock()
assert.Len(t, detector.commentQueries, 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This only checks cache size, so it won’t fail if the query is recompiled on every call and stored under the same key. Verify reuse by comparing the cached query pointer across calls (or by counting compilations) instead.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At pkg/core/detector_test.go, line 120:

<comment>This only checks cache size, so it won’t fail if the query is recompiled on every call and stored under the same key. Verify reuse by comparing the cached query pointer across calls (or by counting compilations) instead.</comment>

<file context>
@@ -98,3 +98,25 @@ func main() {}`
+	// then
+	detector.queryMu.Lock()
+	defer detector.queryMu.Unlock()
+	assert.Len(t, detector.commentQueries, 1)
+	assert.Len(t, detector.docstringQueries, 1)
+}
</file context>

@code-yeongyu
code-yeongyu merged commit db8b7ee into master May 18, 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.

1 participant