You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zanmato1984 (via GitHub)" <gi...@apache.org> on 2024/02/26 15:57:58 UTC
[PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
zanmato1984 opened a new pull request, #40237:
URL: https://github.com/apache/arrow/pull/40237
<!--
Thanks for opening a pull request!
If this is your first pull request you can find detailed information on how
to contribute here:
* [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
* [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
Then could you also rename the pull request title in the following format?
GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
or
MINOR: [${COMPONENT}] ${SUMMARY}
In the case of PARQUET issues on JIRA the title also supports:
PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
-->
### Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
### What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
### Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
### Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
-->
<!-- **This PR includes breaking changes to public APIs.** -->
<!--
Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
-->
<!-- **This PR contains a "Critical Fix".** -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1966538437
Ok, before we introduce even more complexity and ad-hoc synchronization, let's step back a bit and ask ourselves the question: why is a ArraySpan or BufferSpan being initialized concurrently? We should probably fix that, instead of adding bandaids so that it doesn't trigger TSAN warnings.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1561378714
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
I understand that it is unnecessary for this function to accept arbitrary arguments. I will change it for the sake of that.
However I am curious about the risk of UB you mentioned. Did you mean C++ UB wrt. strong aliasing rule? If yes, will memcpy instead eliminate this kind of UB?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503043000
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
+
+// GH-40069: race condition when filling the scratch space of a scalar in parallel.
+TYPED_TEST(TestScalarScratchSpace, ParallelFillScratchSpace) {
+ using ScalarType = typename TypeTraits<TypeParam>::ScalarType;
+
Review Comment:
Got it. Thanks for the tips!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1969102914
> Will this approach break the API compatibility?
Yes, this would be a breaking change. I would guess that not much user code would be invalidated by the change
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2084654175
> The added test `TestConcurrentFillFromScalar` is failing on the emscripten build ([ursacomputing/crossbow/actions/runs/8871520273/job/24354620994](https://github.com/ursacomputing/crossbow/actions/runs/8871520273/job/24354620994))
>
> From a quick look at the diff here, I _think_ it just needs some compile guards because the test requires threads to be available, like:
>
> ```
> #ifndef ARROW_ENABLE_THREADING
> GTEST_SKIP() << "Test requires threading support";
> #endif
> ```
Oops, didn't expect that coming. I'll file a quick fix. Thank you @jorisvandenbossche for locating the cause.
One question though (I'm not familiar with emscripten): is emscripten's threading compilable but not runnable?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576551390
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
Aha, got it. Thanks for the explanation, and the review of course. Appreciate it!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2068522366
Hi @bkietz , shall we move on with this PR? Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503033541
##########
cpp/src/arrow/array/data.cc:
##########
@@ -286,8 +286,10 @@ namespace {
template <typename offset_type>
BufferSpan OffsetsForScalar(uint8_t* scratch_space, offset_type value_size) {
auto* offsets = reinterpret_cast<offset_type*>(scratch_space);
- offsets[0] = 0;
- offsets[1] = static_cast<offset_type>(value_size);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[0])
+ ->store(0, std::memory_order_relaxed);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[1])
+ ->store(static_cast<offset_type>(value_size), std::memory_order_relaxed);
Review Comment:
Well, the reason for the extra reinterpret casts is that: I'm not sure if it is guaranteed in C++ that `sizeof(std::atomic<offset_type>)` is the same as `sizeof(offset_type)`. If they are not the same, the pointer will be wrongly offset-ed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1967252242
I think it would be good to move to an immutable-after-instantiation model. I don't think it's worth supporting the mutation of a Scalar after it was constructed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2073282692
After merging your PR, Conbench analyzed the 7 benchmarking runs that have been run so far on merge-commit d2f140dd7540420aad7685e035f887b0cc9baf91.
There were no benchmark performance regressions. 🎉
The [full Conbench report](https://github.com/apache/arrow/runs/24170493618) has more details. It also includes information about 54 possible false positives for unstable benchmarks that are known to sometimes produce them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz merged PR #40237:
URL: https://github.com/apache/arrow/pull/40237
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503072581
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
+
+// GH-40069: race condition when filling the scratch space of a scalar in parallel.
Review Comment:
Oh BTW, if we want to see a TSAN CI job fail, I'll have to revert the change in `data.cc`, which contains the fix already.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalars with scratch space immutable [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2029709056
Sorry for coming back to this late. I'm pushing a new approach into this PR as previously discussed (thus the title change).
It is still WIP, with some further refactoring to be done and a little more tests to be added. But I think it is enough to show the essential of the approach. So I'd like to start the discussion now before further coding, to prevent any last minute surprise. There are two things:
1. The change in `scalar.h` may seem a little overkill, specifically the `FillScalarScratchSpace` and subclasses. This is because I want to mandate the initialization of the scratch space in the subclasses' constructors, meanwhile do it in a polymorphic fashion. Do you think this is proper?
2. So far I have only made the `value`s immutable for subclasses of `ArraySpanFillFromScalarScratchSpace`. This is sufficient to ensure the validity of the scratch space content. But this doesn't fully achieve the goal of "making scalar absolutely immutable by all means" - which is doable (partially done in my other branch, not shown in this PR), but just requires a lot more changes. Should I move on towards the full-immutability?
I appreciate your feedback @pitrou @bkietz . Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576604124
##########
cpp/src/arrow/scalar.cc:
##########
@@ -969,58 +1058,72 @@ std::shared_ptr<Buffer> FormatToBuffer(Formatter&& formatter, const ScalarType&
}
// error fallback
-Status CastImpl(const Scalar& from, Scalar* to) {
+template <typename To>
+Result<std::shared_ptr<Scalar>> CastImpl(const Scalar& from,
+ std::shared_ptr<DataType> to_type) {
return Status::NotImplemented("casting scalars of type ", *from.type, " to type ",
- *to->type);
+ *to_type);
}
// numeric to numeric
-template <typename From, typename To>
-Status CastImpl(const NumericScalar<From>& from, NumericScalar<To>* to) {
- to->value = static_cast<typename To::c_type>(from.value);
- return Status::OK();
+template <typename To, typename From>
+enable_if_number<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// numeric to boolean
-template <typename T>
-Status CastImpl(const NumericScalar<T>& from, BooleanScalar* to) {
- constexpr auto zero = static_cast<typename T::c_type>(0);
- to->value = from.value != zero;
- return Status::OK();
+template <typename To, typename From>
+enable_if_boolean<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ constexpr auto zero = static_cast<typename From::c_type>(0);
+ return std::make_shared<BooleanScalar>(from.value != zero, std::move(to_type));
}
// boolean to numeric
-template <typename T>
-Status CastImpl(const BooleanScalar& from, NumericScalar<T>* to) {
- to->value = static_cast<typename T::c_type>(from.value);
- return Status::OK();
+template <typename To>
+enable_if_number<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const BooleanScalar& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// numeric to temporal
-template <typename From, typename To>
+template <typename To, typename From>
typename std::enable_if<std::is_base_of<TemporalType, To>::value &&
!std::is_same<DayTimeIntervalType, To>::value &&
!std::is_same<MonthDayNanoIntervalType, To>::value,
- Status>::type
-CastImpl(const NumericScalar<From>& from, TemporalScalar<To>* to) {
- to->value = static_cast<typename To::c_type>(from.value);
- return Status::OK();
+ Result<std::shared_ptr<Scalar>>>::type
+CastImpl(const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// temporal to numeric
-template <typename From, typename To>
-typename std::enable_if<std::is_base_of<TemporalType, From>::value &&
+template <typename To, typename From>
Review Comment:
Not really. As this PR makes `BaseBinaryScalar::value` immutable, it is required to accordingly change the `CastImplVisitior` to create the scalar with a calculated `value` instead of create-scalar-then-assign the `value`. To do this, all `CastImpl` specializations should accept an explicit `To` type parameter as it is not able to deduce the type from an input argument. And this specific line of change is to make the template parameter have the same shape as others.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2084577090
The added test `TestConcurrentFillFromScalar` is failing on the emscripten build (https://github.com/ursacomputing/crossbow/actions/runs/8871520273/job/24354620994)
From a quick look at the diff here, I _think_ it just needs some compile guards because the test requires threads to be available, like:
```
#ifndef ARROW_ENABLE_THREADING
GTEST_SKIP() << "Test requires threading support";
#endif
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2085629038
> One question though (I'm not familiar with emscripten): is emscripten's threading compilable but not runnable?
We have a compile flag to disable threading (`ARROW_ENABLE_THREADING`) which is disabled for the builds for emscripten (https://arrow.apache.org/docs/dev/developers/cpp/emscripten.html)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalars with scratch space immutable [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2041320144
The current approach, i.e., immutable `value`, has some corner cases uncovered with `is_valid` being mutable.
Say, a scalar instance is created with a valid value. Later setting its `is_valid` to `false` is fine because any attempt of using this scalar won't actually use the scratch space content. However it is not the case for the reverse: setting `is_valid` to `true` for an initially invalid scalar instance will leave the scratch space content inconsistent.
One way around it could be leveraging `Scalar::Validate` or `Scalar::ValidateFull` to detect such inconsistency, supposing user code should call these method aggressively to check the scalar.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1558699322
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
Oh, I wanted to make `FillScalarScratchSpace` able to accept arbitrary number of arguments of arbitrary types, meanwhile do the copy type-safely.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1561394385
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
Addressed. But my curiosity remains :)
Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503000334
##########
cpp/src/arrow/array/data.cc:
##########
@@ -297,8 +299,10 @@ std::pair<BufferSpan, BufferSpan> OffsetsAndSizesForScalar(uint8_t* scratch_spac
offset_type value_size) {
auto* offsets = scratch_space;
auto* sizes = scratch_space + sizeof(offset_type);
- reinterpret_cast<offset_type*>(offsets)[0] = 0;
- reinterpret_cast<offset_type*>(sizes)[0] = value_size;
+ reinterpret_cast<std::atomic<offset_type>*>(offsets)->store(0,
+ std::memory_order_relaxed);
+ reinterpret_cast<std::atomic<offset_type>*>(sizes)->store(
+ static_cast<offset_type>(value_size), std::memory_order_relaxed);
Review Comment:
here too
```suggestion
auto* offsets = reinterpret_cast<std::atomic<offset_type>*>(scratch_space);
auto* sizes = offsets + 1;
offsets[0].store(0, std::memory_order_relaxed);
sizes[0].store(value_size, std::memory_order_relaxed);
```
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
+
+// GH-40069: race condition when filling the scratch space of a scalar in parallel.
Review Comment:
Does this test reproduce the thread sanitizer failure on the thread sanitizer CI job?
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
+
+// GH-40069: race condition when filling the scratch space of a scalar in parallel.
+TYPED_TEST(TestScalarScratchSpace, ParallelFillScratchSpace) {
+ using ScalarType = typename TypeTraits<TypeParam>::ScalarType;
+
Review Comment:
Instead of using a test template, it should be simpler (especially for the nested types) to give a list of scalars which should be tested:
```c++
for (auto scalar : {
ScalarFromJSON(utf8(), R"("test data")"),
ScalarFromJSON(utf8_view(), R"("test data")"),
ScalarFromJSON(list(int8()), "[1, 2, 3]"),
ScalarFromJSON(list_view(int8()), "[1, 2, 3]"),
}) {
...
}
```
##########
cpp/src/arrow/array/data.cc:
##########
@@ -286,8 +286,10 @@ namespace {
template <typename offset_type>
BufferSpan OffsetsForScalar(uint8_t* scratch_space, offset_type value_size) {
auto* offsets = reinterpret_cast<offset_type*>(scratch_space);
- offsets[0] = 0;
- offsets[1] = static_cast<offset_type>(value_size);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[0])
+ ->store(0, std::memory_order_relaxed);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[1])
+ ->store(static_cast<offset_type>(value_size), std::memory_order_relaxed);
Review Comment:
I think we can reduce the number of reinterpret casts here
```suggestion
auto* offsets = reinterpret_cast<std::atomic<offset_type>*>(scratch_space);
offsets[0].store(0, std::memory_order_relaxed);
offsets[1].store(value_size, std::memory_order_relaxed);
```
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
Review Comment:
This doesn't touch the List/ListView cases, please also test those
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2045830319
Could you rebase to see if the CI failures have been fixed?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503078133
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,56 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
Review Comment:
This is to save my local work temporarily. No worry, I'll still simplify the case as suggested and get rid of the typed test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503065582
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
+
+// GH-40069: race condition when filling the scratch space of a scalar in parallel.
Review Comment:
I didn't see a TSAN CI job in current job list. Either I'm not sure how to trigger one.
But I confirm that the `ctest` command failed in my local, built with `-DARROW_USE_TSAN=ON -DARROW_JEMALLOC=OFF -DARROW_MIMALLOC=OFF`.
What's interesting, though I'm not exactly sure why, is that gtest actually reported PASSED because the TSAN error is just WARNING printed to stderr. But the test program did aborted (returned non-zero code) and ctest considered it failed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1967308890
Just a concern. Will this approach break the API compatibility? Since `BaseBinaryScalar::value` is already a public API and there might already have been user code that is assigning to it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2043091771
Without early feedback, I just go ahead and now get the PR ready. The description has been updated accordingly. @pitrou @bkietz could you help to take a look? Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1561342145
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
`FillScalarScratchSpaceHelper` doesn't need to write arbitrary arguments, and actually there's a risk of undefined behavior as written since you might `reinterpret_cast<int32_t*>(scratch_space + offset/*=1*/)`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576582339
##########
cpp/src/arrow/scalar.cc:
##########
@@ -969,58 +1058,72 @@ std::shared_ptr<Buffer> FormatToBuffer(Formatter&& formatter, const ScalarType&
}
// error fallback
-Status CastImpl(const Scalar& from, Scalar* to) {
+template <typename To>
+Result<std::shared_ptr<Scalar>> CastImpl(const Scalar& from,
+ std::shared_ptr<DataType> to_type) {
return Status::NotImplemented("casting scalars of type ", *from.type, " to type ",
- *to->type);
+ *to_type);
}
// numeric to numeric
-template <typename From, typename To>
-Status CastImpl(const NumericScalar<From>& from, NumericScalar<To>* to) {
- to->value = static_cast<typename To::c_type>(from.value);
- return Status::OK();
+template <typename To, typename From>
+enable_if_number<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// numeric to boolean
-template <typename T>
-Status CastImpl(const NumericScalar<T>& from, BooleanScalar* to) {
- constexpr auto zero = static_cast<typename T::c_type>(0);
- to->value = from.value != zero;
- return Status::OK();
+template <typename To, typename From>
+enable_if_boolean<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ constexpr auto zero = static_cast<typename From::c_type>(0);
+ return std::make_shared<BooleanScalar>(from.value != zero, std::move(to_type));
}
// boolean to numeric
-template <typename T>
-Status CastImpl(const BooleanScalar& from, NumericScalar<T>* to) {
- to->value = static_cast<typename T::c_type>(from.value);
- return Status::OK();
+template <typename To>
+enable_if_number<To, Result<std::shared_ptr<Scalar>>> CastImpl(
+ const BooleanScalar& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// numeric to temporal
-template <typename From, typename To>
+template <typename To, typename From>
typename std::enable_if<std::is_base_of<TemporalType, To>::value &&
!std::is_same<DayTimeIntervalType, To>::value &&
!std::is_same<MonthDayNanoIntervalType, To>::value,
- Status>::type
-CastImpl(const NumericScalar<From>& from, TemporalScalar<To>* to) {
- to->value = static_cast<typename To::c_type>(from.value);
- return Status::OK();
+ Result<std::shared_ptr<Scalar>>>::type
+CastImpl(const NumericScalar<From>& from, std::shared_ptr<DataType> to_type) {
+ using ToScalar = typename TypeTraits<To>::ScalarType;
+ return std::make_shared<ToScalar>(static_cast<typename To::c_type>(from.value),
+ std::move(to_type));
}
// temporal to numeric
-template <typename From, typename To>
-typename std::enable_if<std::is_base_of<TemporalType, From>::value &&
+template <typename To, typename From>
Review Comment:
Is this a fixing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503181247
##########
cpp/src/arrow/array/data.cc:
##########
@@ -286,8 +286,10 @@ namespace {
template <typename offset_type>
BufferSpan OffsetsForScalar(uint8_t* scratch_space, offset_type value_size) {
auto* offsets = reinterpret_cast<offset_type*>(scratch_space);
- offsets[0] = 0;
- offsets[1] = static_cast<offset_type>(value_size);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[0])
+ ->store(0, std::memory_order_relaxed);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[1])
+ ->store(static_cast<offset_type>(value_size), std::memory_order_relaxed);
Review Comment:
Well it's true that some platforms need a mutex alongside the payload. If it's necessary to support those, we should include a `util::Mutex` in `ArraySpanFillFromScalarScratchSpace` and lock it whenever it's necessary to mutate the scratch space.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503513550
##########
cpp/src/arrow/array/data.cc:
##########
@@ -286,8 +286,10 @@ namespace {
template <typename offset_type>
BufferSpan OffsetsForScalar(uint8_t* scratch_space, offset_type value_size) {
auto* offsets = reinterpret_cast<offset_type*>(scratch_space);
- offsets[0] = 0;
- offsets[1] = static_cast<offset_type>(value_size);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[0])
+ ->store(0, std::memory_order_relaxed);
+ reinterpret_cast<std::atomic<offset_type>*>(&offsets[1])
+ ->store(static_cast<offset_type>(value_size), std::memory_order_relaxed);
Review Comment:
You are right. The casts could be reduced. After a second thought, I think even with extra casts, the result would be wrong if the size of atomic is not the same as the payload. I tend to assume we are not going to support platforms that need a mutex for atomic 64. So I'll just reduce the casts. Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1964494091
:warning: GitHub issue #40069 **has been automatically assigned in GitHub** to PR creator.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1558108543
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
I think this could be simplified to:
```c++
template <typename T, size_t N>
void WriteArray(void *d, T const (&arr)[N]) {
static_assert(sizeof(arr) <= internal::kScalarScratchSpaceSize);
std::memcpy(d, arr, sizeof(arr));
}
void BinaryScalar::FillScratchSpace() {
WriteArray(scratch_space_,
{int32_t(0), value ? static_cast<int32_t>(value->size()) : int32_t(0)});
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-2046329393
Thanks @bkietz for looking! I've rebased as you requested. Let's see the CI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576515068
##########
cpp/src/arrow/scalar.cc:
##########
@@ -542,6 +542,21 @@ struct ScalarValidateImpl {
}
};
+template <size_t offset, typename T, typename... Args>
+void FillScalarScratchSpaceHelper(uint8_t* scratch_space, T first, Args... args) {
+ static_assert(offset + sizeof(T) <= internal::kScalarScratchSpaceSize);
+ *reinterpret_cast<T*>(scratch_space + offset) = first;
+ if constexpr (sizeof...(args) > 0) {
+ FillScalarScratchSpaceHelper<offset + sizeof(T)>(scratch_space,
+ std::forward<Args>(args)...);
+ }
+}
+
+template <typename... Args>
+void FillScalarScratchSpace(Args... args) {
+ FillScalarScratchSpaceHelper<0>(std::forward<Args>(args)...);
+}
+
Review Comment:
The UB I'm referring to is violation of fundamental alignment requirements [(basic.align#1)](https://timsong-cpp.github.io/cppwp/n4659/basic.align#1): `int32_t` can only be allocated on a four byte boundary and it is known at compile time that `reinterpret_cast<int32_t*>(scratch_space + offset/*=1*/)` is not so aligned.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576570166
##########
cpp/src/arrow/scalar.h:
##########
@@ -245,41 +250,51 @@ struct ARROW_EXPORT DoubleScalar : public NumericScalar<DoubleType> {
using NumericScalar<DoubleType>::NumericScalar;
};
-struct ARROW_EXPORT BaseBinaryScalar
- : public internal::PrimitiveScalarBase,
- private internal::ArraySpanFillFromScalarScratchSpace {
- using internal::PrimitiveScalarBase::PrimitiveScalarBase;
+struct ARROW_EXPORT BaseBinaryScalar : public internal::PrimitiveScalarBase {
using ValueType = std::shared_ptr<Buffer>;
- std::shared_ptr<Buffer> value;
+ // The value is not supposed to be modified after construction, because subclasses have
+ // a scratch space whose content need to be kept consistent with the value. It is also
+ // the user of this class's responsibility to ensure that the buffer is not written to
+ // accidentally.
+ const std::shared_ptr<Buffer> value = NULLPTR;
Review Comment:
so this actually means `const std::shared_ptr<const Buffer> value = NULLPTR;`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Make scalar scratch space immutable after initialization [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1576588469
##########
cpp/src/arrow/scalar.h:
##########
@@ -245,41 +250,51 @@ struct ARROW_EXPORT DoubleScalar : public NumericScalar<DoubleType> {
using NumericScalar<DoubleType>::NumericScalar;
};
-struct ARROW_EXPORT BaseBinaryScalar
- : public internal::PrimitiveScalarBase,
- private internal::ArraySpanFillFromScalarScratchSpace {
- using internal::PrimitiveScalarBase::PrimitiveScalarBase;
+struct ARROW_EXPORT BaseBinaryScalar : public internal::PrimitiveScalarBase {
using ValueType = std::shared_ptr<Buffer>;
- std::shared_ptr<Buffer> value;
+ // The value is not supposed to be modified after construction, because subclasses have
+ // a scratch space whose content need to be kept consistent with the value. It is also
+ // the user of this class's responsibility to ensure that the buffer is not written to
+ // accidentally.
+ const std::shared_ptr<Buffer> value = NULLPTR;
Review Comment:
No, the `Buffer` in `const std::shared_ptr<Buffer>` is non-const, meaning one can mutate the content of the pointing buffer. But as the comment says
> It is also the user of this class's responsibility to ensure that the buffer is not written to accidentally.
I'm sort of following the style as what `BufferSpan` is doing:
https://github.com/apache/arrow/blob/d2f140dd7540420aad7685e035f887b0cc9baf91/cpp/src/arrow/array/data.h#L375-L377
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on code in PR #40237:
URL: https://github.com/apache/arrow/pull/40237#discussion_r1503035242
##########
cpp/src/arrow/scalar_test.cc:
##########
@@ -1984,4 +1985,41 @@ TEST_F(TestExtensionScalar, ValidateErrors) {
AssertValidationFails(scalar);
}
+template <typename T>
+class TestScalarScratchSpace : public ::testing::Test {
+ public:
+ TestScalarScratchSpace() = default;
+};
+
+TYPED_TEST_SUITE(TestScalarScratchSpace, BaseBinaryOrBinaryViewLikeArrowTypes);
Review Comment:
Sure. I was planning to do so before turning this PR to ready status.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1966155493
Hi @bkietz , I had some bad news about the current approach.
The current approach did prevent WW race by using atomic stores. The reading, however, is using plane memory load, thus leads to RW race (observable by TSAN, at this line https://github.com/apache/arrow/pull/40237/files#diff-5af65f049e302f76671ecbfc17b0f9c522ec94be291b2bcb442fa565d3607736R2006). Given that the reading is through `ArraySpan` and spread all over the compute component, it is not feasible to change every read to atomic load.
So I have to turn to an alternative approach drafted in #40260, using DCLP which could prevent both WW and RW race conditions. Would you step to #40260 to take a look? (It's of course in draft and lacks tests of many other scalar types, but it would be nice to make agreement on the approach first)
Once we have an agreement on the new approach, I'll close this PR and move on with the other.
cc @pitrou
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1967189304
> why is a ArraySpan or BufferSpan being initialized concurrently?
The problem is we want FillFromScalar to act like copying a constant value into the ArraySpan. It would not be an error for multiple threads to copy a `const int` into a local `int`. However FillFromScalar mutates the constant scalar from which we're copying.
One way to resolve this without sync would be setting up the offset scratch space when the scalar is constructed. IIRC we didn't do that in the first place because there are places where we assign to `BinaryScalar::value`, which would invalidate the offsets. Cleaning those up would be annoying but is more in line with Scalar behaving as an immutable value
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-40069: [C++] Use relaxed atomic store to write scalar scratch space [arrow]
Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #40237:
URL: https://github.com/apache/arrow/pull/40237#issuecomment-1967296300
OK, l can check how much work is needed to move to the immutable-after-instantiation approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org