MCPcopy Create free account
hub / github.com/apache/arrow / MakeAsync

Method MakeAsync

cpp/src/arrow/json/reader.cc:373–447  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

371 }
372
373 static Future<std::shared_ptr<StreamingReaderImpl>> MakeAsync(
374 std::shared_ptr<DecodeContext> context, std::shared_ptr<io::InputStream> stream,
375 io::IOContext io_context, Executor* cpu_executor, const ReadOptions& read_options) {
376 ARROW_ASSIGN_OR_RAISE(
377 auto buffer_it,
378 io::MakeInputStreamIterator(std::move(stream), read_options.block_size));
379 ARROW_ASSIGN_OR_RAISE(
380 auto buffer_gen,
381 MakeBackgroundGenerator(std::move(buffer_it), io_context.executor()));
382
383 AsyncGenerator<DecodedBlock> decoding_gen;
384 int max_readahead = 0;
385 if (read_options.use_threads) {
386 // Prepare a source generator capable of async-reentrancy and parallel execution
387 if (!cpu_executor) {
388 cpu_executor = GetCpuThreadPool();
389 }
390 max_readahead = cpu_executor->GetCapacity();
391
392 // Since the chunking/decoding steps are heavy we want to schedule them as a
393 // separate task so as to maximize task distribution across CPU cores
394 //
395 // TODO: Add an `always_transfer` parameter to `MakeTransferredGenerator`?
396 buffer_gen = [source = std::move(buffer_gen), cpu_executor] {
397 return cpu_executor->TransferAlways(source());
398 };
399 auto chunking_gen = MakeChunkingGenerator(
400 std::move(buffer_gen), MakeChunker(context->parse_options(), context->pool()));
401
402 // At this stage, we want to allow the decoding tasks for each chunked block to run
403 // in parallel on the CPU executor. However:
404 // - Chunking is inherently serial and not thread-safe
405 // - The chunking generator is not async-reentrant, won't play well with readahead
406 //
407 // Fortunately, `MappingGenerator` queues pending jobs and keeps only one future
408 // from its source active at a time - which takes care of those concerns. In
409 // addition, it will start the next job within the continuation of the previous one,
410 // but before invoking its map function (in our case, `DecodingOperator`). This
411 // allows for decoding tasks to gradually saturate multiple CPU cores over multiple
412 // iterations. At a high level, this is how the full pipeline would operate in cases
413 // where decoding tasks are disproportionately expensive:
414 //
415 // --------------------------------------------------------------------------
416 // Reading: IoThread(?) --> Chunking: CpuThread(0) ... Decoding: CpuThread(0)
417 // --------------------------------------------------------------------------
418 // Decoding: CpuThread(0)
419 // Reading: IoThread(?) --> Chunking: CpuThread(1) ... Decoding: CpuThread(1)
420 // --------------------------------------------------------------------------
421 // Decoding: CpuThread(0)
422 // Decoding: CpuThread(1)
423 // Reading: IoThread(?) --> Chunking: CpuThread(2) ... Decoding: CpuThread(2)
424 // --------------------------------------------------------------------------
425 //
426 // Remember that we should already be on the CPU executor following chunking, so the
427 // decoding task simply continues to use that thread rather than spawning a new one.
428 decoding_gen =
429 MakeMappedGenerator(std::move(chunking_gen), DecodingOperator(context));
430 } else {

Callers

nothing calls this directly

Calls 13

GetCpuThreadPoolFunction · 0.85
MakeChunkingGeneratorFunction · 0.85
MakeMappedGeneratorFunction · 0.85
DecodingOperatorClass · 0.85
MakeTransferredGeneratorFunction · 0.85
MakeChunkingIteratorFunction · 0.85
MakeGeneratorIteratorFunction · 0.85
MakeDecodingGeneratorFunction · 0.85
ThenMethod · 0.80
MakeChunkerFunction · 0.70
GetCapacityMethod · 0.45
poolMethod · 0.45

Tested by

no test coverage detected