AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ExperimentalMarkdownSyntaxTextSplitter Has O(N²) Performance Due To List.Pop(0)

The ExperimentalMarkdownSyntaxTextSplitter.split_text method uses list.pop(0) in a while loop to consume lines, causing quadratic time complexity for large markdown documents. Replacing the list with collections.deque and using popleft reduces time to O(n).

highConfidence 95%Langchain-Text-Splitterslangchain-coreMemoryAffected V1.1.1

Origin Analysis

list.pop(0) removes the first element and shifts all remaining elements left, an O(n) operation. When performed for every line in a document with n lines, the total cost becomes O(n²).
import time from langchain_text_splitters import ExperimentalMarkdownSyntaxTextSplitter splitter = ExperimentalMarkdownSyntaxTextSplitter() lines = [f"Line {i}: some content here\n" for i in range(50000)] large_md = "# Header\n" + "".join(lines) start = time.perf_counter() splitter.split_text(large_md) elapsed = time.perf_counter() - start print(f"50k lines: {elapsed:.2f}s") # Expect several seconds instead of milliseconds.

Fixing Code Block

Edge Case Audit

This change is low-risk because the raw_lines variable is only used for truthiness checks, pop operations, and passing to _resolve_code_chunk. However, verify that _resolve_code_chunk does not rely on list-specific behavior such as indexing or slicing. For very small inputs, deque may have a negligible overhead. To rollback, revert to the original list and instead use an index pointer (e.g., idx = 0; line = raw_lines[idx]; idx += 1) to avoid O(n) pop(0) while preserving list semantics.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state