Skip to content

Commit

Permalink
Add stubs for abseil flags.
Browse files Browse the repository at this point in the history
This CL also removes support for using the gflags library to modify the
flags.

PiperOrigin-RevId: 361583626
  • Loading branch information
pwnall committed Mar 8, 2021
1 parent 80a2a10 commit 5e7c14b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 70 deletions.
8 changes: 1 addition & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ include(CheckSymbolExists)
check_symbol_exists("mmap" "sys/mman.h" HAVE_FUNC_MMAP)
check_symbol_exists("sysconf" "unistd.h" HAVE_FUNC_SYSCONF)

find_package(Gflags QUIET)
if(GFLAGS_FOUND OR GFLAGS_TARGET)
set(HAVE_GFLAGS 1)
endif(GFLAGS_FOUND OR GFLAGS_TARGET)

configure_file(
"cmake/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
Expand Down Expand Up @@ -268,7 +263,7 @@ if(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)
# Test files include snappy-test.h, HAVE_CONFIG_H must be defined.
target_compile_definitions(snappy_test_support PUBLIC -DHAVE_CONFIG_H)

target_link_libraries(snappy_test_support snappy ${GFLAGS_LIBRARIES})
target_link_libraries(snappy_test_support snappy)

if(HAVE_LIBZ)
target_link_libraries(snappy_test_support z)
Expand All @@ -283,7 +278,6 @@ if(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)
target_include_directories(snappy_test_support
BEFORE PUBLIC
"${PROJECT_SOURCE_DIR}"
"${GFLAGS_INCLUDE_DIRS}"
)
endif(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)

Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ should provide a reasonably balanced starting point for benchmarking. (Note that
baddata[1-3].snappy are not intended as benchmarks; they are used to verify
correctness in the presence of corrupted data in the unit test.)

The gflags library for handling of command-line flags is used if it's installed.
You can find it at

https://gflags.github.io/gflags/


Contact
=======
Expand Down
3 changes: 0 additions & 3 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
/* Define to 1 if you have a definition for sysconf() in <unistd.h>. */
#cmakedefine HAVE_FUNC_SYSCONF 1

/* Define to 1 to use the gflags package for command-line parsing. */
#cmakedefine HAVE_GFLAGS 1

/* Define to 1 if you have the `lzo2' library (-llzo2). */
#cmakedefine HAVE_LIBLZO2 1

Expand Down
17 changes: 9 additions & 8 deletions snappy-stubs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE
#endif

// This is only used for recomputing the tag byte table used during
// decompression; for simplicity we just remove it from the open-source
// version (anyone who wants to regenerate it can just do the call
// themselves within main()).
#define DEFINE_bool(flag_name, default_value, description) \
bool FLAGS_ ## flag_name = default_value
#define DECLARE_bool(flag_name) \
extern bool FLAGS_ ## flag_name
// Stubbed version of ABSL_FLAG.
//
// In the open source version, flags can only be changed at compile time.
#define SNAPPY_FLAG(flag_type, flag_name, default_value, help) \
flag_type FLAGS_ ## flag_name = default_value

namespace snappy {

// Stubbed version of absl::GetFlag().
template <typename T>
inline T GetFlag(T flag) { return flag; }

static const uint32_t kuint32max = std::numeric_limits<uint32_t>::max();
static const int64_t kint64max = std::numeric_limits<int64_t>::max();

Expand Down
19 changes: 0 additions & 19 deletions snappy-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,8 @@
#include <windows.h>
#endif

#ifdef HAVE_GFLAGS

#include <gflags/gflags.h>

// This is tricky; both gflags and Google Test want to look at the command line
// arguments. Google Test seems to be the most happy with unknown arguments,
// though, so we call it first and hope for the best.
#define InitGoogle(argv0, argc, argv, remove_flags) \
google::ParseCommandLineFlags(argc, argv, remove_flags);

#else

// If we don't have the gflags package installed, these can only be
// changed at compile time.
#define DEFINE_int32(flag_name, default_value, description) \
static int FLAGS_ ## flag_name = default_value;

#define InitGoogle(argv0, argc, argv, remove_flags) ((void)(0))

#endif

#ifdef HAVE_LIBZ
#include "zlib.h"
#endif
Expand Down
52 changes: 28 additions & 24 deletions snappy_test_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <random>
#include <string>
Expand All @@ -41,23 +42,24 @@
#include "snappy.h"
#include "snappy_test_data.h"

DEFINE_int32(start_len, -1,
"Starting prefix size for testing (-1: just full file contents)");
DEFINE_int32(end_len, -1,
"Starting prefix size for testing (-1: just full file contents)");
DEFINE_int32(bytes, 10485760,
"How many bytes to compress/uncompress per file for timing");
SNAPPY_FLAG(int32_t, start_len, -1,
"Starting prefix size for testing (-1: just full file contents)");
SNAPPY_FLAG(int32_t, end_len, -1,
"Starting prefix size for testing (-1: just full file contents)");
SNAPPY_FLAG(int32_t, bytes, 10485760,
"How many bytes to compress/uncompress per file for timing");

DEFINE_bool(zlib, true,
SNAPPY_FLAG(bool, zlib, true,
"Run zlib compression (http://www.zlib.net)");
DEFINE_bool(lzo, true,
SNAPPY_FLAG(bool, lzo, true,
"Run LZO compression (http://www.oberhumer.com/opensource/lzo/)");
DEFINE_bool(lz4, true, "Run LZ4 compression (https://github.com/lz4/lz4)");
DEFINE_bool(snappy, true, "Run snappy compression");
SNAPPY_FLAG(bool, lz4, true,
"Run LZ4 compression (https://github.com/lz4/lz4)");
SNAPPY_FLAG(bool, snappy, true, "Run snappy compression");

DEFINE_bool(write_compressed, false,
SNAPPY_FLAG(bool, write_compressed, false,
"Write compressed versions of each file to <file>.comp");
DEFINE_bool(write_uncompressed, false,
SNAPPY_FLAG(bool, write_uncompressed, false,
"Write uncompressed versions of each file to <file>.uncomp");

namespace snappy {
Expand Down Expand Up @@ -416,25 +418,27 @@ void MeasureFile(const char* fname) {
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
std::printf("%-40s :\n", fname);

int start_len = (absl::GetFlag(FLAGS_start_len) < 0)
int start_len = (snappy::GetFlag(FLAGS_start_len) < 0)
? fullinput.size()
: absl::GetFlag(FLAGS_start_len);
: snappy::GetFlag(FLAGS_start_len);
int end_len = fullinput.size();
if (absl::GetFlag(FLAGS_end_len) >= 0) {
end_len = std::min<int>(fullinput.size(), absl::GetFlag(FLAGS_end_len));
if (snappy::GetFlag(FLAGS_end_len) >= 0) {
end_len = std::min<int>(fullinput.size(), snappy::GetFlag(FLAGS_end_len));
}
for (int len = start_len; len <= end_len; ++len) {
const char* const input = fullinput.data();
int repeats = (absl::GetFlag(FLAGS_bytes) + len) / (len + 1);
if (absl::GetFlag(FLAGS_zlib))
int repeats = (snappy::GetFlag(FLAGS_bytes) + len) / (len + 1);
if (snappy::GetFlag(FLAGS_zlib))
Measure(input, len, ZLIB, repeats, 1024 << 10);
if (absl::GetFlag(FLAGS_lzo)) Measure(input, len, LZO, repeats, 1024 << 10);
if (absl::GetFlag(FLAGS_lz4)) Measure(input, len, LZ4, repeats, 1024 << 10);
if (absl::GetFlag(FLAGS_snappy))
if (snappy::GetFlag(FLAGS_lzo))
Measure(input, len, LZO, repeats, 1024 << 10);
if (snappy::GetFlag(FLAGS_lz4))
Measure(input, len, LZ4, repeats, 1024 << 10);
if (snappy::GetFlag(FLAGS_snappy))
Measure(input, len, SNAPPY, repeats, 4096 << 10);

// For block-size based measurements
if (0 && absl::GetFlag(FLAGS_snappy)) {
if (0 && snappy::GetFlag(FLAGS_snappy)) {
Measure(input, len, SNAPPY, repeats, 8<<10);
Measure(input, len, SNAPPY, repeats, 16<<10);
Measure(input, len, SNAPPY, repeats, 32<<10);
Expand All @@ -453,9 +457,9 @@ int main(int argc, char** argv) {
InitGoogle(argv[0], &argc, &argv, true);

for (int arg = 1; arg < argc; ++arg) {
if (absl::GetFlag(FLAGS_write_compressed)) {
if (snappy::GetFlag(FLAGS_write_compressed)) {
snappy::CompressFile(argv[arg]);
} else if (absl::GetFlag(FLAGS_write_uncompressed)) {
} else if (snappy::GetFlag(FLAGS_write_uncompressed)) {
snappy::UncompressFile(argv[arg]);
} else {
snappy::MeasureFile(argv[arg]);
Expand Down
8 changes: 4 additions & 4 deletions snappy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "snappy.h"
#include "snappy_test_data.h"

DEFINE_bool(snappy_dump_decompression_table, false,
SNAPPY_FLAG(bool, snappy_dump_decompression_table, false,
"If true, we print the decompression table during tests.");

namespace snappy {
Expand Down Expand Up @@ -461,7 +461,7 @@ TEST(Snappy, MaxBlowup) {
}

TEST(Snappy, RandomData) {
std::minstd_rand0 rng(absl::GetFlag(FLAGS_test_random_seed));
std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed));
std::uniform_int_distribution<int> uniform_0_to_3(0, 3);
std::uniform_int_distribution<int> uniform_0_to_8(0, 8);
std::uniform_int_distribution<int> uniform_byte(0, 255);
Expand Down Expand Up @@ -834,7 +834,7 @@ TEST(Snappy, FindMatchLength) {
TEST(Snappy, FindMatchLengthRandom) {
constexpr int kNumTrials = 10000;
constexpr int kTypicalLength = 10;
std::minstd_rand0 rng(absl::GetFlag(FLAGS_test_random_seed));
std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed));
std::uniform_int_distribution<int> uniform_byte(0, 255);
std::bernoulli_distribution one_in_two(1.0 / 2);
std::bernoulli_distribution one_in_typical_length(1.0 / kTypicalLength);
Expand Down Expand Up @@ -938,7 +938,7 @@ TEST(Snappy, VerifyCharTable) {
EXPECT_NE(0xffff, dst[i]) << "Did not assign byte " << i;
}

if (absl::GetFlag(FLAGS_snappy_dump_decompression_table)) {
if (snappy::GetFlag(FLAGS_snappy_dump_decompression_table)) {
std::printf("static const uint16_t char_table[256] = {\n ");
for (int i = 0; i < 256; ++i) {
std::printf("0x%04x%s",
Expand Down

0 comments on commit 5e7c14b

Please sign in to comment.