| 431 | uint32_t PleReader::row_bytes() const { return impl_->row_bytes; } |
| 432 | |
| 433 | PleReader::Ticket PleReader::issue(const uint32_t* rows, size_t n, uint8_t* out_raw) { |
| 434 | Impl& m = *impl_; |
| 435 | std::unique_lock<std::mutex> lk(m.mu, std::defer_lock); |
| 436 | if (m.threaded) lk.lock(); |
| 437 | const uint32_t id = m.next_ticket++; |
| 438 | if (m.next_ticket == 0) m.next_ticket = 1; |
| 439 | const double now = now_us(); |
| 440 | const bool rearm = m.keep_us > 0 && (m.last_issue_us <= 0 || now - m.last_issue_us > m.keep_window_us); |
| 441 | m.last_issue_us = now; |
| 442 | TicketState& ts = m.tickets[id]; |
| 443 | // After a failed read the reader stays failed (`collect` reports `error` until the next `open`) and its worker has |
| 444 | // parked (worker_loop). A ticket queued now would never be read and `collect` would wait for it for ever, so it |
| 445 | // is not queued: it has no jobs, and `collect` returns the error at once. |
| 446 | if (!m.error.empty()) { |
| 447 | ts.pending = 0; |
| 448 | return Ticket{id}; |
| 449 | } |
| 450 | // The common batches -- a decode step's 1..16 rows, the prefetch of PLE_N_HEADS = 16 -- are folded in a fixed stack |
| 451 | // buffer: no hash table, no temporary job vector, sorted by page offset and merged in one pass straight into the |
| 452 | // queue. A batch above kStackMisses (a prompt chunk's thousands of rows) takes the general path below: a hash by page, |
| 453 | // as before. Measured, the sort of every miss loses to the hash there when many rows share a page, so the stack path is |
| 454 | // kept to the size it wins at. Nothing here outlives the call, so concurrent issuers (they hold m.mu when threaded) |
| 455 | // share no scratch. |
| 456 | if (n > UINT32_MAX) { |
| 457 | m.error = "PleReader: more than 2^32 rows in one issue"; |
| 458 | ts.pending = 0; |
| 459 | return Ticket{id}; |
| 460 | } |
| 461 | constexpr size_t kStackMisses = 64; |
| 462 | uint32_t n_jobs = 0; |
| 463 | if (n <= kStackMisses) { |
| 464 | Miss misses[kStackMisses]; |
| 465 | size_t n_miss = 0; |
| 466 | const uint32_t rb = m.row_bytes; |
| 467 | for (size_t i = 0; i < n; ++i) { |
| 468 | uint8_t* dst = out_raw + i * rb; |
| 469 | ++m.stats.requests; |
| 470 | if (rows[i] >= m.n_rows) { |
| 471 | std::memset(dst, 0, rb); |
| 472 | continue; |
| 473 | } |
| 474 | if (const uint8_t* hit = m.cache.find(rows[i])) { |
| 475 | std::memcpy(dst, hit, rb); |
| 476 | ++m.stats.cache_hits; |
| 477 | continue; |
| 478 | } |
| 479 | const uint64_t at = m.table_offset + (uint64_t) rows[i] * rb; |
| 480 | const uint64_t first = at / PAGE * PAGE; |
| 481 | const uint32_t length = (uint32_t) ((at + rb - 1) / PAGE * PAGE - first + PAGE); |
| 482 | misses[n_miss++] = Miss{first, length, rows[i], (uint32_t) (at - first), (uint32_t) i, dst}; |
| 483 | } |
| 484 | // Sorted by offset: prefill chunks then read the SSD in near-sequential order, and equal pages sit adjacent. |
| 485 | if (n_miss > 1) { |
| 486 | std::sort(misses, misses + n_miss, [](const Miss& a, const Miss& b) { |
| 487 | return a.first != b.first ? a.first < b.first : a.order < b.order; |
| 488 | }); |
| 489 | } |
| 490 | for (size_t k = 0; k < n_miss;) { |