Skip to content

Cache boundQuery as lazy property to avoid redundant SQL parsing#20

Open
39ff wants to merge 1 commit into
mainfrom
claude/cache-boundquery-getter-IzITs
Open

Cache boundQuery as lazy property to avoid redundant SQL parsing#20
39ff wants to merge 1 commit into
mainfrom
claude/cache-boundquery-getter-IzITs

Conversation

@39ff

@39ff 39ff commented Feb 19, 2026

Copy link
Copy Markdown
Owner

The boundQuery getter was recomputed on every access (used from applyFilters and again via shortSql in getValueAt). Convert it from a computed property to a lazy-delegated property so the parsed/bound SQL is computed once per QueryEntry instance. The lazy val lives in the data class body so equals/hashCode/copy remain unaffected.

https://claude.ai/code/session_01YAhwF4pT5iPQoSqA9iSEz7

Summary by CodeRabbit

リリースノート

  • リファクタリング
    • クエリパラメータの処理方法を最適化しました。

The boundQuery getter was recomputed on every access (used from
applyFilters and again via shortSql in getValueAt). Convert it from
a computed property to a lazy-delegated property so the parsed/bound
SQL is computed once per QueryEntry instance. The lazy val lives in
the data class body so equals/hashCode/copy remain unaffected.

https://claude.ai/code/session_01YAhwF4pT5iPQoSqA9iSEz7
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Feb 19, 2026

Copy link
Copy Markdown

Walkthrough

QueryEntry.ktのboundQueryプロパティが従来のカスタムgetterから遅延初期化プロパティに変更されました。パラメータ置換ロジックは構造化され、明示的なcontinueステートメントが追加されましたが、機能は変わりません。

Changes

Cohort / File(s) Summary
Lazy-initialized boundQuery property
jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt
boundQueryプロパティをカスタムgetterから遅延初期化プロパティに変更。パラメータ置換ロジックを再構成し、コメント、文字列、ブロック処理後に明示的なcontinueステートメントを追加して制御フローを簡潔化。プレースホルダーがパラメータ不足時の動作を調整。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 ✨
遅延初期化の夢を見て、
バウンドクエリは静かに待つ、
パラメータたちが踊り出す、
再構成された制御フロー、
スムーズなコードの流れよ、ホップホップ!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: converting boundQuery to a lazy-initialized property for caching to avoid redundant SQL parsing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/cache-boundquery-getter-IzITs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt (1)

44-44: LazyThreadSafetyMode.NONE によるロックオーバーヘッドの削減(任意)

デフォルトの by lazyLazyThreadSafetyMode.SYNCHRONIZED を使用し、初回アクセス時にロックを取得します。QueryEntry が常に EDT(Event Dispatch Thread)などの単一スレッドからのみアクセスされる場合は、by lazy(LazyThreadSafetyMode.NONE) に変更することで同期コストを省けます。

-    val boundQuery: String? by lazy {
+    val boundQuery: String? by lazy(LazyThreadSafetyMode.NONE) {

バックグラウンドスレッドからアクセスされる可能性がある場合はデフォルトのままにしてください。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt`
at line 44, The boundQuery lazy property currently uses the default thread-safe
lazy which incurs synchronization overhead; change the declaration of the
boundQuery property in QueryEntry (the val boundQuery: String? by lazy { ... }
property) to use by lazy(LazyThreadSafetyMode.NONE) { ... } to remove locking if
QueryEntry instances are only accessed from a single thread (e.g., EDT); if
QueryEntry may be accessed from background threads, keep the default
synchronized lazy instead.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt`:
- Around line 55-66: The logic in QueryEntry.kt that parses line comments is
only searching for '\n' (eol = query.indexOf('\n', i)), so a lone '\r' ends up
treated as "no eol" and the rest of the query is appended incorrectly; update
the comment-handling code (the block referencing sb, query, i, and eol for '--'
and the analogous '#' block) to locate the nearest line terminator by searching
for both '\n' and '\r' and taking the earliest non-negative index (handling -1s
appropriately), then use that index to append until eol+1 (or to end if none
found) so '\r'‑only line endings are handled correctly; apply the same change to
the '#' comment handling block.

---

Nitpick comments:
In
`@jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt`:
- Line 44: The boundQuery lazy property currently uses the default thread-safe
lazy which incurs synchronization overhead; change the declaration of the
boundQuery property in QueryEntry (the val boundQuery: String? by lazy { ... }
property) to use by lazy(LazyThreadSafetyMode.NONE) { ... } to remove locking if
QueryEntry instances are only accessed from a single thread (e.g., EDT); if
QueryEntry may be accessed from background threads, keep the default
synchronized lazy instead.

Comment on lines +55 to 66
if (ch == '-' && i + 1 < query.length && query[i + 1] == '-'
&& i + 2 < query.length && (query[i + 2] == ' ' || query[i + 2] == '\t' || query[i + 2] == '\n' || query[i + 2] == '\r')) {
val eol = query.indexOf('\n', i)
if (eol == -1) {
sb.append(query, i, query.length)
i = query.length
} else {
sb.append(query, i, eol + 1)
i = eol + 1
}
continue
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

\r のみの行末記号で -- コメントの残りクエリ全体が誤って消費される

eol の検索に query.indexOf('\n', i) のみを使用しており、\r だけの行末(旧 Mac スタイル)の場合は eol == -1 となって、クエリの残り全体がコメントとして StringBuilder に追記されます。

例: "SELECT --\r WHERE id=?"eol = -1sb.append(query, i, query.length) → プレースホルダ ? が置換されずに残る。

同じ問題は # コメント(lines 69–79)にも存在します。実際の SQL では \r 単体の行末は稀ですが、念のため \r の検索も追加することを推奨します。

🐛 修正案(`--` コメントおよび `#` コメントの両方)
-                        val eol = query.indexOf('\n', i)
+                        var eol = query.indexOf('\n', i)
+                        if (eol == -1) eol = query.indexOf('\r', i)
                         if (eol == -1) {
                             sb.append(query, i, query.length)
                             i = query.length
                         } else {
-                            sb.append(query, i, eol + 1)
-                            i = eol + 1
+                            // \r\n の場合は \r も含めて読み飛ばす
+                            val end = if (eol + 1 < query.length && query[eol] == '\r' && query[eol + 1] == '\n') eol + 2 else eol + 1
+                            sb.append(query, i, end)
+                            i = end
                         }

# コメント(lines 70–77)も同様に修正してください。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@jetbrains-plugin/src/main/kotlin/com/mariadbprofiler/plugin/model/QueryEntry.kt`
around lines 55 - 66, The logic in QueryEntry.kt that parses line comments is
only searching for '\n' (eol = query.indexOf('\n', i)), so a lone '\r' ends up
treated as "no eol" and the rest of the query is appended incorrectly; update
the comment-handling code (the block referencing sb, query, i, and eol for '--'
and the analogous '#' block) to locate the nearest line terminator by searching
for both '\n' and '\r' and taking the earliest non-negative index (handling -1s
appropriately), then use that index to append until eol+1 (or to end if none
found) so '\r'‑only line endings are handled correctly; apply the same change to
the '#' comment handling block.

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