YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/fast_varint.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_FAST_VARINT_H
15
#define YB_UTIL_FAST_VARINT_H
16
17
#include <string>
18
19
#include <glog/logging.h>
20
21
#include "yb/util/cast.h"
22
#include "yb/util/slice.h"
23
#include "yb/util/status.h"
24
25
namespace yb {
26
namespace util {
27
28
constexpr size_t kMaxVarIntBufferSize = 16;
29
30
// Computes the number of bytes needed to represent the given number as a signed VarInt.
31
int SignedPositiveVarIntLength(uint64_t v);
32
33
void FastEncodeSignedVarInt(int64_t v, uint8_t *dest, size_t *size);
34
std::string FastEncodeSignedVarIntToStr(int64_t v);
35
36
template <class Buffer>
37
820M
void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) {
38
820M
  char buf[kMaxVarIntBufferSize];
39
820M
  size_t len = 0;
40
820M
  FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len);
41
820M
  DCHECK_LE(len, 10);
42
820M
  dest->append(buf, len);
43
820M
}
void yb::util::FastAppendSignedVarIntToBuffer<yb::ByteBuffer<64ul> >(long long, yb::ByteBuffer<64ul>*)
Line
Count
Source
37
820M
void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) {
38
820M
  char buf[kMaxVarIntBufferSize];
39
820M
  size_t len = 0;
40
820M
  FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len);
41
820M
  DCHECK_LE(len, 10);
42
820M
  dest->append(buf, len);
43
820M
}
void yb::util::FastAppendSignedVarIntToBuffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(long long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Line
Count
Source
37
482k
void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) {
38
482k
  char buf[kMaxVarIntBufferSize];
39
482k
  size_t len = 0;
40
482k
  FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len);
41
482k
  DCHECK_LE(len, 10);
42
482k
  dest->append(buf, len);
43
482k
}
44
45
// Returns status, decoded value and size consumed from source.
46
// Might use effective performance optimization that reads before src, but not before
47
// read_allowed_from.
48
CHECKED_STATUS FastDecodeSignedVarInt(
49
    const uint8_t* src, size_t src_size, const uint8_t* read_allowed_from, int64_t* v,
50
    size_t* decoded_size);
51
52
inline CHECKED_STATUS FastDecodeSignedVarInt(
53
    const char* src, size_t src_size, const char* read_allowed_from, int64_t* v,
54
370k
    size_t* decoded_size) {
55
370k
  return FastDecodeSignedVarInt(
56
370k
      to_uchar_ptr(src), src_size, to_uchar_ptr(read_allowed_from), v,
57
370k
      decoded_size);
58
370k
}
59
60
// WARNING:
61
// FastDecodeSignedVarIntUnsafe functions below are optimized for performance, but require from
62
// caller to guarantee that we can read some bytes (up to 7) before src.
63
64
// Consumes decoded part of the slice.
65
Result<int64_t> FastDecodeSignedVarIntUnsafe(Slice* slice);
66
CHECKED_STATUS FastDecodeSignedVarIntUnsafe(const uint8_t* src,
67
                                      size_t src_size,
68
                                      int64_t* v,
69
                                      size_t* decoded_size);
70
71
// The same as FastDecodeSignedVarIntUnsafe but takes a regular char pointer.
72
inline CHECKED_STATUS FastDecodeSignedVarIntUnsafe(
73
0
    const char* src, size_t src_size, int64_t* v, size_t* decoded_size) {
74
0
  return FastDecodeSignedVarIntUnsafe(to_uchar_ptr(src), src_size, v, decoded_size);
75
0
}
76
77
CHECKED_STATUS FastDecodeSignedVarIntUnsafe(
78
    const std::string& encoded, int64_t* v, size_t* decoded_size);
79
80
// Encoding a "descending VarInt" is simply decoding -v as a VarInt.
81
1.80G
inline char* FastEncodeDescendingSignedVarInt(int64_t v, char *buf) {
82
1.80G
  size_t size = 0;
83
1.80G
  FastEncodeSignedVarInt(-v, to_uchar_ptr(buf), &size);
84
1.80G
  return buf + size;
85
1.80G
}
86
87
inline void FastEncodeDescendingSignedVarInt(int64_t v, std::string *dest) {
88
  char buf[kMaxVarIntBufferSize];
89
  auto* end = FastEncodeDescendingSignedVarInt(v, buf);
90
  dest->append(buf, end);
91
}
92
93
// Decode a "descending VarInt" encoded by FastEncodeDescendingVarInt.
94
CHECKED_STATUS FastDecodeDescendingSignedVarIntUnsafe(Slice *slice, int64_t *dest);
95
Result<int64_t> FastDecodeDescendingSignedVarIntUnsafe(Slice* slice);
96
97
size_t UnsignedVarIntLength(uint64_t v);
98
void FastAppendUnsignedVarIntToStr(uint64_t v, std::string* dest);
99
void FastEncodeUnsignedVarInt(uint64_t v, uint8_t *dest, size_t *size);
100
CHECKED_STATUS FastDecodeUnsignedVarInt(
101
    const uint8_t* src, size_t src_size, uint64_t* v, size_t* decoded_size);
102
Result<uint64_t> FastDecodeUnsignedVarInt(Slice* slice);
103
Result<uint64_t> FastDecodeUnsignedVarInt(const Slice& slice);
104
105
}  // namespace util
106
}  // namespace yb
107
108
#endif  // YB_UTIL_FAST_VARINT_H