Repository navigation
Optimize markdown batch creation with 300x speedup - #738
Conversation
## Summary Implements two-phase batch creation for markdown import: - Phase 1: Prepare all nodes in memory with pre-assigned UUIDs - Phase 2: Insert all nodes in a single database transaction ## Performance Results | Test | Before | After | Speedup | |------|--------|-------|---------| | 100 nodes | 25.7s | 86ms | **~300x** | | 500 nodes | ~128s | 388ms | **~330x** | | 1000 nodes | ~256s | 756ms | **~340x** | ## Changes - Add `PreparedNode` struct for in-memory node preparation - Add `prepare_nodes_from_markdown()` pure function (no DB calls) - Add `bulk_create_hierarchy()` in SurrealStore for single-transaction insert - Add `bulk_create_hierarchy()` in NodeService with validation - Rename `ensure_date_container_exists` to `ensure_date_exists` (public API) - Update `handle_create_nodes_from_markdown()` to use two-phase approach - Add performance benchmark tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Replace O(depth) BFS traversal with SurrealDB's {..+collect} recursive syntax:
- get_nodes_in_subtree(): Now uses single recursive collect query instead of
iterative BFS that did 2 queries per tree level
- get_edges_in_subtree(): Now uses single recursive collect query instead of
calling get_nodes_in_subtree() first then querying edges
Performance improvement:
- Before: O(depth) queries - tree with 10 levels = ~20 queries
- After: O(1) queries - 2 queries regardless of tree depth
This benefits both:
- Frontend: get_children_tree() for loading document trees
- MCP: get_markdown_from_node_id() for exporting markdown
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Combine multiple separate queries into one efficient database round-trip:
Before (3+ queries):
1. get_node(root_id) - fetch root
2. get_nodes_in_subtree() - O(depth) BFS traversal
3. get_edges_in_subtree() - separate edge query
4. enrich_nodes_with_spoke_data() - separate task spoke query
After (1 query):
- get_subtree_with_edges() fetches everything in single query:
- Root + all descendants via recursive {..+collect}
- All edges
- Task spoke data (status, priority, etc.)
Benefits:
- Single database round-trip regardless of tree size/depth
- Task nodes have properties populated (strongly-typed frontend support)
- Removed separate enrich_nodes_with_spoke_data method
- Simpler code with less duplication
This optimization benefits both:
- Frontend: get_children_tree() for loading document trees
- MCP: get_markdown_from_node_id() for exporting markdown
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Code Review SummaryReview Type: Initial Review Overall AssessmentThis PR delivers an exceptional performance optimization for markdown import, achieving 300-340x speedup (far exceeding the 10-15x target from issue #737). The implementation follows a clean two-phase batch creation pattern that fundamentally improves scalability. Net Positive Assessment: APPROVED - This is a significant improvement to code health with sound engineering principles applied throughout. Requirements ValidationFrom Issue #737 Acceptance Criteria:
Code Review FindingsCritical IssuesNone identified. Suggested Improvements1. [Improvement] SQL Injection Prevention - Verify Escaping Completeness 📁 fn escape_surql_string(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t")
}Analysis: The Recommendation: Consider adding a test case with adversarial content (e.g., Principle: Defense in Depth (Security) 2. [Improvement] Consolidate Subtree Methods - Minor Redundancy 📁 The pub async fn get_nodes_in_subtree(&self, root_id: &str) -> Result<Vec<Node>> {
// Delegate to consolidated method, excluding root
let (all_nodes, _edges) = self.get_subtree_with_edges(root_id).await?;
...
}
pub async fn get_edges_in_subtree(&self, root_id: &str) -> Result<Vec<EdgeRecord>> {
// Delegate to consolidated method, discarding nodes
let (_nodes, edges) = self.get_subtree_with_edges(root_id).await?;
...
}Recommendation: Add a doc comment warning callers that need both nodes AND edges to use Principle: DRY / Performance Awareness 3. [Nit] Performance Test Assertions 📁 // After implementing batch optimization, uncomment:
// assert!(duration.as_millis() < 1000, "Should complete in <1 second after optimization");Consider uncommenting this assertion now that the optimization is complete, or update the comment to reflect the achieved performance (300x improvement means the assertion would easily pass). 4. [Nit] Type Signature Ergonomics 📁 pub async fn bulk_create_hierarchy(
&self,
nodes: Vec<(
String,
String,
String,
Option<String>,
f64,
serde_json::Value,
)>,
) -> Result<Vec<String>> {The 6-element tuple is harder to read than a struct. While
This is purely ergonomic - the current approach works correctly. Architecture Assessment
Security Assessment
Testing Assessment
Final Recommendation✅ APPROVE This PR represents high-quality engineering work that delivers exceptional performance gains with clean architecture. The two-phase batch creation pattern is well-implemented, and all acceptance criteria are met. Minor Action Items (Non-Blocking):
🤖 Generated with Claude Code Pragmatic Code Review |
malibio
left a comment
There was a problem hiding this comment.
Principal Engineer Review Complete
Recommendation: APPROVE
Excellent performance optimization with clean architecture, sound security practices, and comprehensive testing. All acceptance criteria from Issue #737 are met. The 300x speedup far exceeds the 10-15x target.
See detailed review comment above for minor non-blocking improvement suggestions.
- Add doc comments to get_nodes_in_subtree and get_edges_in_subtree recommending get_subtree_with_edges for callers needing both - Enable performance assertion in benchmark test (now validated) - Apply rustfmt formatting Addresses reviewer recommendations from PR #738. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Review Feedback Addressed✅ Addressed (3 recommendations)
⏭️ Skipped (2 recommendations)
📝 Commit
🧪 TestsAll 4 performance tests pass, including the newly enabled assertion. 🤖 Generated with Claude Code |
* Optimize markdown batch creation with 300x speedup (#737) ## Summary Implements two-phase batch creation for markdown import: - Phase 1: Prepare all nodes in memory with pre-assigned UUIDs - Phase 2: Insert all nodes in a single database transaction ## Performance Results | Test | Before | After | Speedup | |------|--------|-------|---------| | 100 nodes | 25.7s | 86ms | **~300x** | | 500 nodes | ~128s | 388ms | **~330x** | | 1000 nodes | ~256s | 756ms | **~340x** | ## Changes - Add `PreparedNode` struct for in-memory node preparation - Add `prepare_nodes_from_markdown()` pure function (no DB calls) - Add `bulk_create_hierarchy()` in SurrealStore for single-transaction insert - Add `bulk_create_hierarchy()` in NodeService with validation - Rename `ensure_date_container_exists` to `ensure_date_exists` (public API) - Update `handle_create_nodes_from_markdown()` to use two-phase approach - Add performance benchmark tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Optimize subtree fetching with O(1) recursive collect queries Replace O(depth) BFS traversal with SurrealDB's {..+collect} recursive syntax: - get_nodes_in_subtree(): Now uses single recursive collect query instead of iterative BFS that did 2 queries per tree level - get_edges_in_subtree(): Now uses single recursive collect query instead of calling get_nodes_in_subtree() first then querying edges Performance improvement: - Before: O(depth) queries - tree with 10 levels = ~20 queries - After: O(1) queries - 2 queries regardless of tree depth This benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Consolidate subtree fetching into single database query Combine multiple separate queries into one efficient database round-trip: Before (3+ queries): 1. get_node(root_id) - fetch root 2. get_nodes_in_subtree() - O(depth) BFS traversal 3. get_edges_in_subtree() - separate edge query 4. enrich_nodes_with_spoke_data() - separate task spoke query After (1 query): - get_subtree_with_edges() fetches everything in single query: - Root + all descendants via recursive {..+collect} - All edges - Task spoke data (status, priority, etc.) Benefits: - Single database round-trip regardless of tree size/depth - Task nodes have properties populated (strongly-typed frontend support) - Removed separate enrich_nodes_with_spoke_data method - Simpler code with less duplication This optimization benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address code review feedback - Add doc comments to get_nodes_in_subtree and get_edges_in_subtree recommending get_subtree_with_edges for callers needing both - Enable performance assertion in benchmark test (now validated) - Apply rustfmt formatting Addresses reviewer recommendations from PR #738. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* Optimize markdown batch creation with 300x speedup (#737) ## Summary Implements two-phase batch creation for markdown import: - Phase 1: Prepare all nodes in memory with pre-assigned UUIDs - Phase 2: Insert all nodes in a single database transaction ## Performance Results | Test | Before | After | Speedup | |------|--------|-------|---------| | 100 nodes | 25.7s | 86ms | **~300x** | | 500 nodes | ~128s | 388ms | **~330x** | | 1000 nodes | ~256s | 756ms | **~340x** | ## Changes - Add `PreparedNode` struct for in-memory node preparation - Add `prepare_nodes_from_markdown()` pure function (no DB calls) - Add `bulk_create_hierarchy()` in SurrealStore for single-transaction insert - Add `bulk_create_hierarchy()` in NodeService with validation - Rename `ensure_date_container_exists` to `ensure_date_exists` (public API) - Update `handle_create_nodes_from_markdown()` to use two-phase approach - Add performance benchmark tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Optimize subtree fetching with O(1) recursive collect queries Replace O(depth) BFS traversal with SurrealDB's {..+collect} recursive syntax: - get_nodes_in_subtree(): Now uses single recursive collect query instead of iterative BFS that did 2 queries per tree level - get_edges_in_subtree(): Now uses single recursive collect query instead of calling get_nodes_in_subtree() first then querying edges Performance improvement: - Before: O(depth) queries - tree with 10 levels = ~20 queries - After: O(1) queries - 2 queries regardless of tree depth This benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Consolidate subtree fetching into single database query Combine multiple separate queries into one efficient database round-trip: Before (3+ queries): 1. get_node(root_id) - fetch root 2. get_nodes_in_subtree() - O(depth) BFS traversal 3. get_edges_in_subtree() - separate edge query 4. enrich_nodes_with_spoke_data() - separate task spoke query After (1 query): - get_subtree_with_edges() fetches everything in single query: - Root + all descendants via recursive {..+collect} - All edges - Task spoke data (status, priority, etc.) Benefits: - Single database round-trip regardless of tree size/depth - Task nodes have properties populated (strongly-typed frontend support) - Removed separate enrich_nodes_with_spoke_data method - Simpler code with less duplication This optimization benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address code review feedback - Add doc comments to get_nodes_in_subtree and get_edges_in_subtree recommending get_subtree_with_edges for callers needing both - Enable performance assertion in benchmark test (now validated) - Apply rustfmt formatting Addresses reviewer recommendations from PR #738. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* Optimize markdown batch creation with 300x speedup (#737) ## Summary Implements two-phase batch creation for markdown import: - Phase 1: Prepare all nodes in memory with pre-assigned UUIDs - Phase 2: Insert all nodes in a single database transaction ## Performance Results | Test | Before | After | Speedup | |------|--------|-------|---------| | 100 nodes | 25.7s | 86ms | **~300x** | | 500 nodes | ~128s | 388ms | **~330x** | | 1000 nodes | ~256s | 756ms | **~340x** | ## Changes - Add `PreparedNode` struct for in-memory node preparation - Add `prepare_nodes_from_markdown()` pure function (no DB calls) - Add `bulk_create_hierarchy()` in SurrealStore for single-transaction insert - Add `bulk_create_hierarchy()` in NodeService with validation - Rename `ensure_date_container_exists` to `ensure_date_exists` (public API) - Update `handle_create_nodes_from_markdown()` to use two-phase approach - Add performance benchmark tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Optimize subtree fetching with O(1) recursive collect queries Replace O(depth) BFS traversal with SurrealDB's {..+collect} recursive syntax: - get_nodes_in_subtree(): Now uses single recursive collect query instead of iterative BFS that did 2 queries per tree level - get_edges_in_subtree(): Now uses single recursive collect query instead of calling get_nodes_in_subtree() first then querying edges Performance improvement: - Before: O(depth) queries - tree with 10 levels = ~20 queries - After: O(1) queries - 2 queries regardless of tree depth This benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Consolidate subtree fetching into single database query Combine multiple separate queries into one efficient database round-trip: Before (3+ queries): 1. get_node(root_id) - fetch root 2. get_nodes_in_subtree() - O(depth) BFS traversal 3. get_edges_in_subtree() - separate edge query 4. enrich_nodes_with_spoke_data() - separate task spoke query After (1 query): - get_subtree_with_edges() fetches everything in single query: - Root + all descendants via recursive {..+collect} - All edges - Task spoke data (status, priority, etc.) Benefits: - Single database round-trip regardless of tree size/depth - Task nodes have properties populated (strongly-typed frontend support) - Removed separate enrich_nodes_with_spoke_data method - Simpler code with less duplication This optimization benefits both: - Frontend: get_children_tree() for loading document trees - MCP: get_markdown_from_node_id() for exporting markdown 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address code review feedback - Add doc comments to get_nodes_in_subtree and get_edges_in_subtree recommending get_subtree_with_edges for callers needing both - Enable performance assertion in benchmark test (now validated) - Apply rustfmt formatting Addresses reviewer recommendations from PR #738. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Summary
Implements two-phase batch creation for markdown import to dramatically improve performance. Closes #737.
Performance Results
The optimization exceeded expectations - the issue anticipated 10-15x improvement, but we achieved 300-340x.
Changes
PreparedNodestruct for in-memory node preparationprepare_nodes_from_markdown()pure function (no DB calls)bulk_create_hierarchy()in SurrealStore for single-transaction insertbulk_create_hierarchy()in NodeService with validationensure_date_container_existstoensure_date_exists(public API)handle_create_nodes_from_markdown()to use two-phase approachTest plan
🤖 Generated with Claude Code