Skip to content

Commit

Permalink
refactor : Change C-style casts to C++-style (Part 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-pandit committed Nov 28, 2024
1 parent 1ce3c7a commit c7ff69e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
14 changes: 7 additions & 7 deletions velox/vector/VectorSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum class Encoding : int8_t {

template <typename T>
void write(const T& value, std::ostream& out) {
out.write((char*)&value, sizeof(T));
out.write(reinterpret_cast<const char*>(&value), sizeof(T));
}

template <>
Expand All @@ -53,7 +53,7 @@ void write<std::string>(const std::string& value, std::ostream& out) {
template <typename T>
T read(std::istream& in) {
T value;
in.read((char*)&value, sizeof(T));
in.read(reinterpret_cast<char*>(&value), sizeof(T));
return value;
}

Expand All @@ -78,16 +78,16 @@ void writeEncoding(VectorEncoding::Simple encoding, std::ostream& out) {
case VectorEncoding::Simple::ROW:
case VectorEncoding::Simple::ARRAY:
case VectorEncoding::Simple::MAP:
write<int32_t>((int8_t)Encoding::kFlat, out);
write<int32_t>(static_cast<int8_t>(Encoding::kFlat), out);
return;
case VectorEncoding::Simple::CONSTANT:
write<int32_t>((int8_t)Encoding::kConstant, out);
write<int32_t>(static_cast<int8_t>(Encoding::kConstant), out);
return;
case VectorEncoding::Simple::DICTIONARY:
write<int32_t>((int8_t)Encoding::kDictionary, out);
write<int32_t>(static_cast<int8_t>(Encoding::kDictionary), out);
return;
case VectorEncoding::Simple::LAZY:
write<int32_t>((int8_t)Encoding::kLazy, out);
write<int32_t>(static_cast<int8_t>(Encoding::kLazy), out);
return;
default:
VELOX_UNSUPPORTED("Unsupported encoding: {}", mapSimpleToName(encoding));
Expand Down Expand Up @@ -275,7 +275,7 @@ void writeScalarConstant(const BaseVector& vector, std::ostream& out) {
using T = typename TypeTraits<kind>::NativeType;

auto value = vector.as<ConstantVector<T>>()->valueAt(0);
out.write((const char*)&value, sizeof(T));
out.write(reinterpret_cast<const char*>(&value), sizeof(T));

if constexpr (std::is_same_v<T, StringView>) {
if (!value.isInline()) {
Expand Down
4 changes: 3 additions & 1 deletion velox/vector/VectorStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ RowVectorPtr IOBufToRowVector(

for (const auto& range : ioBuf) {
ranges.emplace_back(ByteRange{
const_cast<uint8_t*>(range.data()), (int32_t)range.size(), 0});
const_cast<uint8_t*>(range.data()),
static_cast<int32_t>(range.size()),
0});
}

auto byteStream = std::make_unique<BufferInputStream>(std::move(ranges));
Expand Down
29 changes: 16 additions & 13 deletions velox/vector/arrow/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class VeloxToArrowBridgeHolder {
}

const void** getArrowBuffers() {
return (const void**)&(buffers_[0]);
return reinterpret_cast<const void**>(&(buffers_[0]));
}

// Allocates space for `numChildren` ArrowArray pointers.
Expand Down Expand Up @@ -564,7 +564,8 @@ VectorPtr createStringFlatVectorFromUtf8View(
"Expecting three or more buffers as input for string view types.");

// The last C data buffer stores buffer sizes
auto* bufferSizes = (uint64_t*)arrowArray.buffers[num_buffers - 1];
auto* bufferSizes =
reinterpret_cast<const uint64_t*>(arrowArray.buffers[num_buffers - 1]);
std::vector<BufferPtr> stringViewBuffers(num_buffers - 3);

// Skipping buffer_id = 0 (nulls buffer) and buffer_id = 1 (values buffer)
Expand All @@ -582,13 +583,15 @@ VectorPtr createStringFlatVectorFromUtf8View(
// buffer-index, 4-byte buffer-offset] to 16-byte Velox StringView [4-byte
// length, 4-byte prefix, 8-byte buffer-ptr]
for (int32_t idx_64 = 0; idx_64 < arrowArray.length; ++idx_64) {
auto* view = (uint32_t*)(&((uint64_t*)arrowArray.buffers[1])[2 * idx_64]);
rawStringViews[2 * idx_64] = *(uint64_t*)view;
auto* view = reinterpret_cast<const uint32_t*>(&(
reinterpret_cast<const uint64_t*>(arrowArray.buffers[1]))[2 * idx_64]);
rawStringViews[2 * idx_64] = *reinterpret_cast<const uint64_t*>(view);
if (view[0] > 12)
rawStringViews[2 * idx_64 + 1] =
(uint64_t)arrowArray.buffers[2 + view[2]] + view[3];
reinterpret_cast<uint64_t>(arrowArray.buffers[2 + view[2]]) + view[3];
else
rawStringViews[2 * idx_64 + 1] = *(uint64_t*)&view[2];
rawStringViews[2 * idx_64 + 1] =
*reinterpret_cast<const uint64_t*>(&view[2]);
}

return std::make_shared<FlatVector<StringView>>(
Expand Down Expand Up @@ -777,18 +780,18 @@ void exportViews(
stringBufferVec.begin(),
stringBufferVec.end(),
[&out](const auto& lhs, const auto& rhs) {
return ((uint64_t*)&out.buffers[2])[lhs] <
((uint64_t*)&out.buffers[2])[rhs];
return reinterpret_cast<uint64_t*>(&out.buffers[2])[lhs] <
reinterpret_cast<uint64_t*>(&out.buffers[2])[rhs];
});

auto utf8Views = (uint64_t*)out.buffers[1];
auto utf8Views = (uint64_t*)(out.buffers[1]);
int32_t bufferIdxCache = 0;
uint64_t bufferAddrCache = 0;

rows.apply([&](vector_size_t i) {
auto view = (uint32_t*)&utf8Views[2 * i];
auto view = reinterpret_cast<uint32_t*>(&utf8Views[2 * i]);
if (!vec.isNullAt(i) && view[0] > 12) {
uint64_t currAddr = *(uint64_t*)&view[2];
uint64_t currAddr = *reinterpret_cast<const uint64_t*>(&view[2]);
// 2. Search for correct index with the buffer-pointer as key. Cache the
// found buffer's address and index in bufferAddrCache and bufferIdxCache
// respectively
Expand All @@ -800,9 +803,9 @@ void exportViews(
stringBufferVec.end(),
currAddr,
[&out](const auto& lhs, const auto& rhs) {
return lhs < ((uint64_t*)&out.buffers[2])[rhs];
return lhs < (reinterpret_cast<uint64_t*>(&out.buffers[2]))[rhs];
}));
bufferAddrCache = ((uint64_t*)&out.buffers[2])[*it];
bufferAddrCache = (reinterpret_cast<uint64_t*>(&out.buffers[2]))[*it];
bufferIdxCache = *it;
}
view[2] = bufferIdxCache;
Expand Down
4 changes: 3 additions & 1 deletion velox/vector/fuzzer/examples/EncodingGeneratorExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ int main() {
}

std::cout << "Probability of constant encoding = "
<< (double)numConst / ((double)numConst + (double)numDict) << "\n";
<< static_cast<double>(numConst) /
static_cast<double>(numConst + numDict)
<< "\n";

return 0;
}

0 comments on commit c7ff69e

Please sign in to comment.