forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-11680: [C++] Add vendored version of folly's spsc queue
See issue for rationale / description. To consider: * I based licensing text on other vendored libraries but I'm no expert in how exactly to dot all the i's and cross all the t's. * The constructor can throw if allocation of the ring buffer fails. In general, this sort of queue shouldn't be all that large anyways so this seems unlikely. We could modify the constructor to be private with a factory method that returns `Result` but at the moment I'm not making any changes to the existing code and so I'm hesitant to start doing so for this reason. * ~~There is a test case for the above point but I could take it out if desired.~~ Had to remove test since a massive allocation did not throw on mac. * ~~There is a comparative benchmark for boost. I'm not sure if we want to leave that in?~~ Had to remove benchmark since some build environments did not have boost::lockfree::spsc_queue * I've renamed folly::ProducerConsumerQueue to arrow::util::SpscQueue. I feel the name ProducerConsumerQueue is too vague and could lead to improper usage. This queue is not safe for multiple readers or multiple writers. Closes apache#9519 from westonpace/feature/folly Lead-authored-by: Weston Pace <[email protected]> Co-authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
- Loading branch information
1 parent
e7c47ba
commit fbb0662
Showing
8 changed files
with
448 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include "arrow/vendored/ProducerConsumerQueue.h" | ||
|
||
namespace arrow { | ||
namespace util { | ||
|
||
template <typename T> | ||
using SpscQueue = arrow_vendored::folly::ProducerConsumerQueue<T>; | ||
|
||
} | ||
} // namespace arrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <algorithm> | ||
#include <iterator> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
#include "arrow/buffer.h" | ||
#include "arrow/util/logging.h" | ||
#include "arrow/util/queue.h" | ||
|
||
namespace arrow { | ||
|
||
namespace util { | ||
|
||
static constexpr int64_t kSize = 100000; | ||
|
||
void Throughput(benchmark::State& state) { | ||
SpscQueue<std::shared_ptr<Buffer>> queue(16); | ||
|
||
std::vector<std::shared_ptr<Buffer>> source; | ||
std::vector<std::shared_ptr<Buffer>> sink; | ||
source.reserve(kSize); | ||
sink.resize(kSize); | ||
const uint8_t data[1] = {0}; | ||
for (int64_t i = 0; i < kSize; i++) { | ||
source.push_back(std::make_shared<Buffer>(data, 1)); | ||
} | ||
|
||
for (auto _ : state) { | ||
std::thread producer([&] { | ||
auto itr = std::make_move_iterator(source.begin()); | ||
auto end = std::make_move_iterator(source.end()); | ||
while (itr != end) { | ||
while (!queue.Write(*itr)) { | ||
} | ||
itr++; | ||
} | ||
}); | ||
|
||
std::thread consumer([&] { | ||
auto itr = sink.begin(); | ||
auto end = sink.end(); | ||
while (itr != end) { | ||
auto next = queue.FrontPtr(); | ||
if (next != nullptr) { | ||
(*itr).swap(*next); | ||
queue.PopFront(); | ||
itr++; | ||
} | ||
} | ||
}); | ||
|
||
producer.join(); | ||
consumer.join(); | ||
std::swap(source, sink); | ||
} | ||
|
||
for (const auto& buf : source) { | ||
ARROW_CHECK(buf && buf->size() == 1); | ||
} | ||
state.SetItemsProcessed(state.iterations() * kSize); | ||
} | ||
|
||
BENCHMARK(Throughput)->UseRealTime(); | ||
|
||
} // namespace util | ||
} // namespace arrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "arrow/testing/gtest_util.h" | ||
#include "arrow/util/queue.h" | ||
|
||
namespace arrow { | ||
namespace util { | ||
|
||
TEST(TestSpscQueue, TestMoveOnly) { | ||
SpscQueue<MoveOnlyDataType> queue(3); | ||
ASSERT_TRUE(queue.IsEmpty()); | ||
ASSERT_FALSE(queue.IsFull()); | ||
ASSERT_EQ(queue.SizeGuess(), 0); | ||
|
||
MoveOnlyDataType in(42); | ||
queue.Write(std::move(in)); | ||
ASSERT_FALSE(queue.IsEmpty()); | ||
ASSERT_FALSE(queue.IsFull()); | ||
ASSERT_EQ(queue.SizeGuess(), 1); | ||
|
||
queue.Write(43); | ||
ASSERT_FALSE(queue.IsEmpty()); | ||
ASSERT_TRUE(queue.IsFull()); | ||
ASSERT_EQ(queue.SizeGuess(), 2); | ||
|
||
MoveOnlyDataType out = std::move(*queue.FrontPtr()); | ||
ASSERT_EQ(42, *out.data); | ||
queue.PopFront(); | ||
ASSERT_TRUE(queue.Read(out)); | ||
ASSERT_EQ(43, *out.data); | ||
|
||
ASSERT_TRUE(queue.IsEmpty()); | ||
ASSERT_FALSE(queue.IsFull()); | ||
ASSERT_EQ(queue.SizeGuess(), 0); | ||
} | ||
|
||
} // namespace util | ||
} // namespace arrow |
Oops, something went wrong.