Skip to content

import: a large event is re-parsed from the start on every read, so importing is quadratic in element size #1101

Done

Description

@chappie-daemon

Found by the round-9 static review of #870 (PR #1094). Pre-existing on rewrite/main at 68be6f3: the file appears in that PR's diff only for a hunk at line 493, and this site is untouched.

The mechanism

crates/nostream-services/src/services/event_import_service.rs:390-437. ArrayStream::next_value parses the current element with a fresh serde_json::Deserializer::from_slice(&self.buffer[self.position..]) every call. On an incomplete element the arm

Some(Err(error)) if error.is_eof() => {
    if !self.read_more().await? { ... }
}

reads more and loops, and self.position has not advanced — only values.byte_offset() past a successful parse would have. So the element is parsed again from byte zero each time it grows.

A large event (a long article, a big NIP-46 payload) read in 8 KiB chunks is therefore parsed once per chunk over a growing prefix: import cost is quadratic in element size, and the peak allocation per parse grows with it.

What a fix needs

Either keep the parser state across reads (serde_json::Deserializer over a Read-like source that can be fed, rather than re-slicing), or find the element's end first — scan for the closing brace at depth 0, skipping strings and escapes — and parse the complete slice once. The second is a self-contained function and easy to test with an element larger than the read size; the existing tests already build arrays from a reader.

Activity

  1. AgrimTawani commented on Nov 23, 2026

    @AgrimTawani

    Hey @cameri, I'd like to take this one since I was just in the import code for #1097. Is it still open?

    • Find where the element ends first (depth scan that skips strings and escapes), then parse it once
    • Keep the scan position across reads, so a growing element isn't rescanned from the start
    • Add a test with an element bigger than the 8 KiB read size
  2. cameri commented on Nov 23, 2026

    @cameri
    Owner

    @AgrimTawani Assigned to you. Thanks.

  3. AgrimTawani commented on Nov 23, 2026

    @AgrimTawani

    Hey @cameri, the fix is ready on my fork; I still can't open the PR myself: rewrite/main...AgrimTawani:nostream:fix/1101-import-scan-once

    • The reader finds each element's end as bytes arrive and parses it once; a 4 MiB event goes from about 1 GiB parsed to under 3× its size
    • A malformed element still fails early (it's tried each time it doubles), error messages are unchanged, and a number split across two reads no longer fails the import
    • 8 new tests; all five CLAUDE.md checks pass (1816 tests)
  4. chappie-daemon commented on Nov 23, 2026

    @chappie-daemon
    CollaboratorAuthor

    Opened it for you, with your commit and authorship intact: #1235

    I verified before pushing rather than taking the summary at face value: cargo fmt --all --check, both clippy invocations, cargo test --workspace --all-features (1816 passed, 0 failed — matching your number exactly) and the release build. The diff also scanned clean for hidden characters, and it touches no workflow, dependency or install path. Thanks for the careful write-up of the doubling probe; that is the part that makes the linearity claim checkable.

  5. added a commit that references this issue on Nov 23, 2026
    5ac6996
  6. chappie-daemon commented on Nov 23, 2026

    @chappie-daemon
    CollaboratorAuthor

    Landed on rewrite/main in 5ac6996 via #1235 — your commit was carried with its authorship intact, and the memory-buffering edge Greptile flagged on that path is fixed on top. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions