Skip to content

Commit 201f522

Browse files
committed
Remove leveldb::port::kLittleEndian.
Clang 10 includes the optimizations described in https://bugs.llvm.org/show_bug.cgi?id=41761. This means that the platform-independent implementations of {Decode,Encode}Fixed{32,64}() compile to one instruction on the most recent Clang and GCC. PiperOrigin-RevId: 306330166
1 parent ba369dd commit 201f522

File tree

5 files changed

+4
-61
lines changed

5 files changed

+4
-61
lines changed

CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
3434
option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
3535
option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
3636

37-
include(TestBigEndian)
38-
test_big_endian(LEVELDB_IS_BIG_ENDIAN)
39-
4037
include(CheckIncludeFile)
4138
check_include_file("unistd.h" HAVE_UNISTD_H)
4239

port/port_config.h.in

-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,4 @@
3030
#cmakedefine01 HAVE_SNAPPY
3131
#endif // !defined(HAVE_SNAPPY)
3232

33-
// Define to 1 if your processor stores words with the most significant byte
34-
// first (like Motorola and SPARC, unlike Intel and VAX).
35-
#if !defined(LEVELDB_IS_BIG_ENDIAN)
36-
#cmakedefine01 LEVELDB_IS_BIG_ENDIAN
37-
#endif // !defined(LEVELDB_IS_BIG_ENDIAN)
38-
3933
#endif // STORAGE_LEVELDB_PORT_PORT_CONFIG_H_

port/port_example.h

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ namespace port {
1818
// TODO(jorlow): Many of these belong more in the environment class rather than
1919
// here. We should try moving them and see if it affects perf.
2020

21-
// The following boolean constant must be true on a little-endian machine
22-
// and false otherwise.
23-
static const bool kLittleEndian = true /* or some other expression */;
24-
2521
// ------------------ Threading -------------------
2622

2723
// A Mutex represents an exclusive lock.

port/port_stdcxx.h

-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
namespace leveldb {
4242
namespace port {
4343

44-
static const bool kLittleEndian = !LEVELDB_IS_BIG_ENDIAN;
45-
4644
class CondVar;
4745

4846
// Thinly wraps std::mutex.

util/coding.h

+4-46
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,13 @@ int VarintLength(uint64_t v);
4848
char* EncodeVarint32(char* dst, uint32_t value);
4949
char* EncodeVarint64(char* dst, uint64_t value);
5050

51-
// TODO(costan): Remove port::kLittleEndian and the fast paths based on
52-
// std::memcpy when clang learns to optimize the generic code, as
53-
// described in https://bugs.llvm.org/show_bug.cgi?id=41761
54-
//
55-
// The platform-independent code in DecodeFixed{32,64}() gets optimized to mov
56-
// on x86 and ldr on ARM64, by both clang and gcc. However, only gcc optimizes
57-
// the platform-independent code in EncodeFixed{32,64}() to mov / str.
58-
5951
// Lower-level versions of Put... that write directly into a character buffer
6052
// REQUIRES: dst has enough space for the value being written
6153

6254
inline void EncodeFixed32(char* dst, uint32_t value) {
6355
uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
6456

65-
if (port::kLittleEndian) {
66-
// Fast path for little-endian CPUs. All major compilers optimize this to a
67-
// single mov (x86_64) / str (ARM) instruction.
68-
std::memcpy(buffer, &value, sizeof(uint32_t));
69-
return;
70-
}
71-
72-
// Platform-independent code.
73-
// Currently, only gcc optimizes this to a single mov / str instruction.
57+
// Recent clang and gcc optimize this to a single mov / str instruction.
7458
buffer[0] = static_cast<uint8_t>(value);
7559
buffer[1] = static_cast<uint8_t>(value >> 8);
7660
buffer[2] = static_cast<uint8_t>(value >> 16);
@@ -80,15 +64,7 @@ inline void EncodeFixed32(char* dst, uint32_t value) {
8064
inline void EncodeFixed64(char* dst, uint64_t value) {
8165
uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
8266

83-
if (port::kLittleEndian) {
84-
// Fast path for little-endian CPUs. All major compilers optimize this to a
85-
// single mov (x86_64) / str (ARM) instruction.
86-
std::memcpy(buffer, &value, sizeof(uint64_t));
87-
return;
88-
}
89-
90-
// Platform-independent code.
91-
// Currently, only gcc optimizes this to a single mov / str instruction.
67+
// Recent clang and gcc optimize this to a single mov / str instruction.
9268
buffer[0] = static_cast<uint8_t>(value);
9369
buffer[1] = static_cast<uint8_t>(value >> 8);
9470
buffer[2] = static_cast<uint8_t>(value >> 16);
@@ -105,16 +81,7 @@ inline void EncodeFixed64(char* dst, uint64_t value) {
10581
inline uint32_t DecodeFixed32(const char* ptr) {
10682
const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
10783

108-
if (port::kLittleEndian) {
109-
// Fast path for little-endian CPUs. All major compilers optimize this to a
110-
// single mov (x86_64) / ldr (ARM) instruction.
111-
uint32_t result;
112-
std::memcpy(&result, buffer, sizeof(uint32_t));
113-
return result;
114-
}
115-
116-
// Platform-independent code.
117-
// Clang and gcc optimize this to a single mov / ldr instruction.
84+
// Recent clang and gcc optimize this to a single mov / ldr instruction.
11885
return (static_cast<uint32_t>(buffer[0])) |
11986
(static_cast<uint32_t>(buffer[1]) << 8) |
12087
(static_cast<uint32_t>(buffer[2]) << 16) |
@@ -124,16 +91,7 @@ inline uint32_t DecodeFixed32(const char* ptr) {
12491
inline uint64_t DecodeFixed64(const char* ptr) {
12592
const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
12693

127-
if (port::kLittleEndian) {
128-
// Fast path for little-endian CPUs. All major compilers optimize this to a
129-
// single mov (x86_64) / ldr (ARM) instruction.
130-
uint64_t result;
131-
std::memcpy(&result, buffer, sizeof(uint64_t));
132-
return result;
133-
}
134-
135-
// Platform-independent code.
136-
// Clang and gcc optimize this to a single mov / ldr instruction.
94+
// Recent clang and gcc optimize this to a single mov / ldr instruction.
13795
return (static_cast<uint64_t>(buffer[0])) |
13896
(static_cast<uint64_t>(buffer[1]) << 8) |
13997
(static_cast<uint64_t>(buffer[2]) << 16) |

0 commit comments

Comments
 (0)