Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cpp/src/arrow/csv/reader.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -703,14 +703,11 @@ class SerialStreamingReader : public BaseStreamingReader,
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

// TODO Consider exposing readahead as a read option (ARROW-12090)
ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));

// TODO Consider exposing readahead as a read option (ARROW-12090)
auto rh_it =
MakeSerialReadaheadGenerator(std::move(bg_it), cpu_executor_->GetCapacity());

auto transferred_it = MakeTransferredGenerator(rh_it, cpu_executor_);
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);

buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
task_group_ = internal::TaskGroup::MakeSerial(io_context_.stop_token());
Expand DownExpand Up@@ -909,15 +906,15 @@ class AsyncThreadedTableReader
ARROW_ASSIGN_OR_RAISE(auto istream_it,
io::MakeInputStreamIterator(input_, read_options_.block_size));

ARROW_ASSIGN_OR_RAISE(auto bg_it, MakeBackgroundGenerator(std::move(istream_it),
io_context_.executor()));
int max_readahead = cpu_executor_->GetCapacity();
int readahead_restart = std::max(1, max_readahead / 2);

auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
ARROW_ASSIGN_OR_RAISE(
auto bg_it, MakeBackgroundGenerator(std::move(istream_it), io_context_.executor(),
max_readahead, readahead_restart));

int32_t block_queue_size = cpu_executor_->GetCapacity();
auto rh_it =
MakeSerialReadaheadGenerator(std::move(transferred_it), block_queue_size);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(rh_it));
auto transferred_it = MakeTransferredGenerator(bg_it, cpu_executor_);
buffer_generator_ = CSVBufferIterator::MakeAsync(std::move(transferred_it));
return Status::OK();
}

Expand Down
182 changes: 138 additions & 44 deletions cpp/src/arrow/util/async_generator.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -1096,65 +1096,158 @@ AsyncGenerator<T> MakeIteratorGenerator(Iterator<T> it) {
template <typename T>
class BackgroundGenerator {
public:
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor)
: io_executor_(io_executor) {
task_ = Task{std::make_shared<Iterator<T>>(std::move(it)),
std::make_shared<std::atomic<bool>>(false)};
}

~BackgroundGenerator() {
// The thread pool will be disposed of automatically. By default it will not wait
// so the background thread may outlive this object. That should be ok. Any task
// objects in the thread pool are copies of task_ and have their own shared_ptr to
// the iterator.
}
explicit BackgroundGenerator(Iterator<T> it, internal::Executor* io_executor, int max_q,
int q_restart)
: state_(std::make_shared<State>(io_executor, std::move(it), max_q, q_restart)) {}

ARROW_DEFAULT_MOVE_AND_ASSIGN(BackgroundGenerator);
ARROW_DISALLOW_COPY_AND_ASSIGN(BackgroundGenerator);
~BackgroundGenerator() {}

Future<T> operator()() {
auto submitted_future = io_executor_->Submit(task_);
if (!submitted_future.ok()) {
return Future<T>::MakeFinished(submitted_future.status());
auto guard = state_->mutex.Lock();
Future<T> waiting_future;
if (state_->queue.empty()) {
if (state_->finished) {
return AsyncGeneratorEnd<T>();
} else {
waiting_future = Future<T>::Make();
state_->waiting_future = waiting_future;
}
} else {
auto next = Future<T>::MakeFinished(std::move(state_->queue.front()));
state_->queue.pop();
if (!state_->running &&
static_cast<int>(state_->queue.size()) <= state_->q_restart) {
state_->RestartTask(state_, std::move(guard));
}
return next;
}
if (!state_->running) {
// This branch should only be needed to start the background thread on the first
// call
state_->RestartTask(state_, std::move(guard));
}
return std::move(*submitted_future);
return waiting_future;
}

protected:
struct Task {
Result<T> operator()() {
if (*done_) {
return IterationTraits<T>::End();
struct State {
State(internal::Executor* io_executor, Iterator<T> it, int max_q, int q_restart)
: io_executor(io_executor),
it(std::move(it)),
running(false),
finished(false),
max_q(max_q),
q_restart(q_restart) {}

void ClearQueue() {
while (!queue.empty()) {
queue.pop();
}
auto next = it_->Next();
if (!next.ok() || IsIterationEnd(*next)) {
*done_ = true;
}

void RestartTask(std::shared_ptr<State> state, util::Mutex::Guard guard) {
if (!finished) {
running = true;
auto spawn_status = io_executor->Spawn([state]() { Task()(std::move(state)); });
if (!spawn_status.ok()) {
running = false;
finished = true;
if (waiting_future.has_value()) {
auto to_deliver = std::move(waiting_future.value());
waiting_future.reset();
guard.Unlock();
to_deliver.MarkFinished(spawn_status);
} else {
ClearQueue();
queue.push(spawn_status);
}
}
}
return next;
}
// This task is going to be copied so we need to convert the iterator ptr to
// a shared ptr. This should be safe however because the background executor only
// has a single thread so it can't access it_ across multiple threads.
std::shared_ptr<Iterator<T>> it_;
std::shared_ptr<std::atomic<bool>> done_;

internal::Executor* io_executor;
Iterator<T> it;
bool running;
bool finished;
int max_q;
int q_restart;
std::queue<Result<T>> queue;
util::optional<Future<T>> waiting_future;
util::Mutex mutex;
};

Task task_;
internal::Executor* io_executor_;
class Task {
public:
void operator()(std::shared_ptr<State> state) {
// while condition can't be based on state_ because it is run outside the mutex
bool running = true;
while (running) {
auto next = state->it.Next();
// Need to capture state->waiting_future inside the mutex to mark finished outside
Future<T> waiting_future;
{
auto guard = state->mutex.Lock();

if (!next.ok() || IsIterationEnd<T>(*next)) {
state->finished = true;
state->running = false;
if (!next.ok()) {
state->ClearQueue();
}
}
if (state->waiting_future.has_value()) {
waiting_future = std::move(state->waiting_future.value());
state->waiting_future.reset();
} else {
state->queue.push(std::move(next));
if (static_cast<int>(state->queue.size()) >= state->max_q) {
state->running = false;
}
}
running = state->running;
}
// This must happen outside the task. Although presumably there is a transferring
// generator on the other end that will quickly transfer any callbacks off of this
// thread so we can continue looping. Still, best not to rely on that
if (waiting_future.is_valid()) {
waiting_future.MarkFinished(next);
}
}
}
};

std::shared_ptr<State> state_;
};

constexpr int kDefaultBackgroundMaxQ = 32;
constexpr int kDefaultBackgroundQRestart = 16;

/// \brief Creates an AsyncGenerator<T> by iterating over an Iterator<T> on a background
/// thread
///
/// This generator is async-reentrant
/// The parameter max_q and q_restart control queue size and background thread task
/// management. If the background task is fast you typically don't want it creating a
/// thread task for every item. Instead the background thread will run until it fills
/// up a readahead queue.
///
/// This generator will not queue
/// Once the queue has filled up the background thread task will terminate (allowing other
/// I/O tasks to use the thread). Once the queue has been drained enough (specified by
/// q_restart) then the background thread task will be restarted. If q_restart is too low
/// then you may exhaust the queue waiting for the background thread task to start running
/// again. If it is too high then it will be constantly stopping and restarting the
/// background queue task
///
/// This generator is not async-reentrant
///
/// This generator will queue up to max_q blocks
template <typename T>
static Result<AsyncGenerator<T>> MakeBackgroundGenerator(
Iterator<T> iterator, internal::Executor* io_executor) {
auto background_iterator = std::make_shared<BackgroundGenerator<T>>(
std::move(iterator), std::move(io_executor));
return [background_iterator]() { return (*background_iterator)(); };
Iterator<T> iterator, internal::Executor* io_executor,
int max_q = kDefaultBackgroundMaxQ, int q_restart = kDefaultBackgroundQRestart) {
if (max_q < q_restart) {
return Status::Invalid("max_q must be >= q_restart");
}
return BackgroundGenerator<T>(std::move(iterator), io_executor, max_q, q_restart);
}

/// \see MakeGeneratorIterator
Expand DownExpand Up@@ -1185,16 +1278,17 @@ Result<Iterator<T>> MakeGeneratorIterator(AsyncGenerator<T> source) {
template <typename T>
Result<Iterator<T>> MakeReadaheadIterator(Iterator<T> it, int readahead_queue_size) {
ARROW_ASSIGN_OR_RAISE(auto io_executor, internal::ThreadPool::Make(1));
ARROW_ASSIGN_OR_RAISE(auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get()));
auto max_q = readahead_queue_size;
auto q_restart = std::max(1, max_q / 2);
ARROW_ASSIGN_OR_RAISE(
auto background_generator,
MakeBackgroundGenerator(std::move(it), io_executor.get(), max_q, q_restart));
// Capture io_executor to keep it alive as long as owned_bg_generator is still
// referenced
AsyncGenerator<T> owned_bg_generator = [io_executor, background_generator]() {
return background_generator();
};
auto readahead_generator =
MakeReadaheadGenerator(std::move(owned_bg_generator), readahead_queue_size);
return MakeGeneratorIterator(std::move(readahead_generator));
return MakeGeneratorIterator(std::move(owned_bg_generator));
}

} // namespace arrow
Loading