Skip to content

Commit

Permalink
feat: introduce specialized API to write fixed length data rapidly (#…
Browse files Browse the repository at this point in the history
…5181)

close #5183
  • Loading branch information
SchrodingerZhu authored Jun 24, 2022
1 parent dab31a5 commit 73e708c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dbms/src/Flash/Coprocessor/TiDBColumn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename T>
void encodeLittleEndian(const T & value, WriteBuffer & ss)
{
auto v = toLittleEndian(value);
ss.write(reinterpret_cast<const char *>(&v), sizeof(v));
ss.template writeFixed<T>(&v);
}

TiDBColumn::TiDBColumn(Int8 element_len_)
Expand Down Expand Up @@ -141,10 +141,10 @@ void TiDBColumn::append(const TiDBDecimal & decimal)
encodeLittleEndian<UInt8>(decimal.digits_int, *data);
encodeLittleEndian<UInt8>(decimal.digits_frac, *data);
encodeLittleEndian<UInt8>(decimal.result_frac, *data);
encodeLittleEndian<UInt8>((UInt8)decimal.negative, *data);
for (int i = 0; i < MAX_WORD_BUF_LEN; i++)
encodeLittleEndian<UInt8>(static_cast<UInt8>(decimal.negative), *data);
for (int i : decimal.word_buf)
{
encodeLittleEndian<Int32>(decimal.word_buf[i], *data);
encodeLittleEndian<Int32>(i, *data);
}
finishAppendFixed();
}
Expand Down
18 changes: 18 additions & 0 deletions dbms/src/IO/WriteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ class WriteBuffer : public BufferBase
}
}

template <class T>
__attribute__((always_inline)) void writeFixed(const T * __restrict from)
{
if (likely(working_buffer.end() - pos >= static_cast<ptrdiff_t>(sizeof(T))))
{
tiflash_compiler_builtin_memcpy(pos, from, sizeof(T));
pos += sizeof(T);
}
else
{
[&]() __attribute__((noinline))
{
write(reinterpret_cast<const char *>(from), sizeof(T));
}
();
}
}


inline void write(char x)
{
Expand Down

0 comments on commit 73e708c

Please sign in to comment.