Avoid bad code #4052

This commit is contained in:
Alexey Milovidov 2019-01-16 03:52:33 +03:00
parent e8871bc211
commit 78671d15bd

View File

@ -43,14 +43,17 @@ void compressDataForType(const char * source, UInt32 source_size, char * dest)
if (source_size % sizeof(T) != 0) if (source_size % sizeof(T) != 0)
throw Exception("Cannot delta compress, data size " + toString(source_size) + " is not aligned to " + toString(sizeof(T)), ErrorCodes::CANNOT_COMPRESS); throw Exception("Cannot delta compress, data size " + toString(source_size) + " is not aligned to " + toString(sizeof(T)), ErrorCodes::CANNOT_COMPRESS);
const auto * source_with_type = reinterpret_cast<const T *>(source); T prev_src{};
auto * dest_with_type = reinterpret_cast<T *>(dest); const char * source_end = source + source_size;
while (source < source_end)
{
T curr_src = unalignedLoad<T>(source);
unalignedStore(dest, curr_src - prev_src);
prev_src = curr_src;
if (source_size > 0) source += sizeof(T);
unalignedStore<T>(&dest_with_type[0], source_with_type[0]); dest += sizeof(T);
}
for (size_t dest_index = 1, dest_end = source_size / sizeof(T); dest_index < dest_end; ++dest_index)
unalignedStore<T>(&dest_with_type[dest_index], source_with_type[dest_index] - source_with_type[dest_index - 1]);
} }
template <typename T> template <typename T>
@ -59,14 +62,16 @@ void decompressDataForType(const char * source, UInt32 source_size, char * dest)
if (source_size % sizeof(T) != 0) if (source_size % sizeof(T) != 0)
throw Exception("Cannot delta decompress, data size " + toString(source_size) + " is not aligned to " + toString(sizeof(T)), ErrorCodes::CANNOT_DECOMPRESS); throw Exception("Cannot delta decompress, data size " + toString(source_size) + " is not aligned to " + toString(sizeof(T)), ErrorCodes::CANNOT_DECOMPRESS);
const auto * source_with_type = reinterpret_cast<const T *>(source); T accumulator{};
auto * dest_with_type = reinterpret_cast<T *>(dest); const char * source_end = source + source_size;
while (source < source_end)
{
accumulator += unalignedLoad<T>(source);
unalignedStore(dest, accumulator);
if (source_size > 0) source += sizeof(T);
unalignedStore<T>(&dest_with_type[0], source_with_type[0]); dest += sizeof(T);
}
for (size_t dest_index = 1, dest_end = source_size / sizeof(T); dest_index < dest_end; ++dest_index)
unalignedStore<T>(&dest_with_type[dest_index], source_with_type[dest_index] + dest_with_type[dest_index - 1]);
} }
} }