From 241b24fb2467e3547cffa7c1056bd42f7952dd9f Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Mon, 29 Aug 2022 18:26:45 +0000 Subject: GC unused RawBuf functionality --- searchlib/CMakeLists.txt | 1 - searchlib/src/tests/prettyfloat/.gitignore | 4 -- searchlib/src/tests/prettyfloat/CMakeLists.txt | 8 --- searchlib/src/tests/prettyfloat/prettyfloat.cpp | 31 --------- searchlib/src/tests/util/rawbuf_test.cpp | 78 +--------------------- searchlib/src/vespa/searchlib/common/hitrank.h | 1 - searchlib/src/vespa/searchlib/util/rawbuf.cpp | 66 +----------------- searchlib/src/vespa/searchlib/util/rawbuf.h | 89 +++++++------------------ 8 files changed, 29 insertions(+), 249 deletions(-) delete mode 100644 searchlib/src/tests/prettyfloat/.gitignore delete mode 100644 searchlib/src/tests/prettyfloat/CMakeLists.txt delete mode 100644 searchlib/src/tests/prettyfloat/prettyfloat.cpp diff --git a/searchlib/CMakeLists.txt b/searchlib/CMakeLists.txt index f9b6271b47b..f39018846a0 100644 --- a/searchlib/CMakeLists.txt +++ b/searchlib/CMakeLists.txt @@ -186,7 +186,6 @@ vespa_define_module( src/tests/nearsearch src/tests/postinglistbm src/tests/predicate - src/tests/prettyfloat src/tests/query src/tests/queryeval src/tests/queryeval/blueprint diff --git a/searchlib/src/tests/prettyfloat/.gitignore b/searchlib/src/tests/prettyfloat/.gitignore deleted file mode 100644 index bf0327f3372..00000000000 --- a/searchlib/src/tests/prettyfloat/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.depend -Makefile -prettyfloat_test -searchlib_prettyfloat_test_app diff --git a/searchlib/src/tests/prettyfloat/CMakeLists.txt b/searchlib/src/tests/prettyfloat/CMakeLists.txt deleted file mode 100644 index 907b7661800..00000000000 --- a/searchlib/src/tests/prettyfloat/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(searchlib_prettyfloat_test_app TEST - SOURCES - prettyfloat.cpp - DEPENDS - searchlib -) -vespa_add_test(NAME searchlib_prettyfloat_test_app COMMAND searchlib_prettyfloat_test_app) diff --git a/searchlib/src/tests/prettyfloat/prettyfloat.cpp b/searchlib/src/tests/prettyfloat/prettyfloat.cpp deleted file mode 100644 index a84ac59c964..00000000000 --- a/searchlib/src/tests/prettyfloat/prettyfloat.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include -LOG_SETUP("prettyfloat_test"); -#include -#include -#include - -using namespace search; - -TEST_SETUP(Test); - -int -Test::Main() -{ - TEST_INIT("prettyfloat_test"); - { - RawBuf buf(5000); - SignedHitRank rank = 10; - buf.addSignedHitRank(rank); - *buf.GetWritableFillPos() = '\0'; - EXPECT_EQUAL(std::string("10"), buf.GetDrainPos()); - } - { - RawBuf buf(5000); - HitRank rank = 10; - buf.addHitRank(rank); - *buf.GetWritableFillPos() = '\0'; - EXPECT_EQUAL(std::string("10"), buf.GetDrainPos()); - } - TEST_DONE(); -} diff --git a/searchlib/src/tests/util/rawbuf_test.cpp b/searchlib/src/tests/util/rawbuf_test.cpp index ca7221d0240..663dde3ef45 100644 --- a/searchlib/src/tests/util/rawbuf_test.cpp +++ b/searchlib/src/tests/util/rawbuf_test.cpp @@ -1,10 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -// Unit tests for rawbuf. #include #include #include -#include #include LOG_SETUP("rawbuf_test"); @@ -18,40 +16,6 @@ string getString(const RawBuf &buf) { return string(buf.GetDrainPos(), buf.GetUsedLen()); } -TEST("require that rawbuf can append text") { - RawBuf buf(10); - buf += "foo"; - buf += "bar"; - EXPECT_EQUAL("foobar", getString(buf)); -} - -TEST("require that rawbuf expands when appended beyond size") { - RawBuf buf(4); - buf += "foo"; - EXPECT_EQUAL(1u, buf.GetFreeLen()); - buf += "bar"; - EXPECT_EQUAL(2u, buf.GetFreeLen()); - EXPECT_EQUAL("foobar", getString(buf)); -} - -TEST("require that a rawbuf can be appended to another") { - RawBuf buf1(10); - RawBuf buf2(10); - buf1 += "foo"; - buf2 += "bar"; - buf1 += buf2; - EXPECT_EQUAL("foobar", getString(buf1)); -} - -TEST("require that rawbufs can be tested for equality") { - RawBuf buf1(10); - RawBuf buf2(10); - buf1 += "foo"; - buf2 += "bar"; - EXPECT_TRUE(buf1 == buf1); - EXPECT_FALSE(buf1 == buf2); -} - template void checkAddNum(void (RawBuf::*addNum)(T, size_t, char), size_t num, size_t fieldw, char fill, const string &expected) { @@ -79,18 +43,6 @@ TEST("require that rawbuf can add numbers in decimal") { checkAddNum(&RawBuf::addNum64, -1, 4, '0', "00-1"); } -TEST("require that rawbuf can add hitrank") { - RawBuf buf(10); - buf.addHitRank(HitRank(4.2)); - EXPECT_EQUAL("4.2", getString(buf)); -} - -TEST("require that rawbuf can add signedhitrank") { - RawBuf buf(10); - buf.addHitRank(SignedHitRank(-4.213)); - EXPECT_EQUAL("-4.213", getString(buf)); -} - TEST("require that rawbuf can append data of known length") { RawBuf buf(10); const string data("foo bar baz qux quux"); @@ -98,33 +50,15 @@ TEST("require that rawbuf can append data of known length") { EXPECT_EQUAL(data, getString(buf)); } -TEST("require that rawbuf can be truncated shorter and longer") { - RawBuf buf(10); - buf += "foobarbaz"; - buf.truncate(3); - buf += "qux"; - buf.truncate(9); - EXPECT_EQUAL("fooquxbaz", getString(buf)); -} - TEST("require that prealloc makes enough room") { RawBuf buf(10); - buf += "foo"; + buf.append("foo"); EXPECT_EQUAL(7u, buf.GetFreeLen()); buf.preAlloc(100); EXPECT_EQUAL("foo", getString(buf)); EXPECT_LESS_EQUAL(100u, buf.GetFreeLen()); } -TEST("require that compact discards drained data") { - RawBuf buf(10); - buf += "foobar"; - buf.Drain(3); - buf.Compact(); - buf.Fill(3); - EXPECT_EQUAL("barbar", getString(buf)); -} - TEST("require that reusing a buffer that has grown 4x will alloc new buffer") { RawBuf buf(10); buf.preAlloc(100); @@ -135,7 +69,7 @@ TEST("require that reusing a buffer that has grown 4x will alloc new buffer") { TEST("require that various length and position information can be found.") { RawBuf buf(30); - buf += "foo bar baz qux quux corge"; + buf.append("foo bar baz qux quux corge"); buf.Drain(7); EXPECT_EQUAL(7u, buf.GetDrainLen()); EXPECT_EQUAL(19u, buf.GetUsedLen()); @@ -143,14 +77,6 @@ TEST("require that various length and position information can be found.") { EXPECT_EQUAL(4u, buf.GetFreeLen()); } -TEST("require that rawbuf can 'putToInet' 16-bit numbers") { - RawBuf buf(1); - buf.Put16ToInet(0x1234); - EXPECT_EQUAL(2, buf.GetFillPos() - buf.GetDrainPos()); - EXPECT_EQUAL(0x12, (int) buf.GetDrainPos()[0] & 0xff); - EXPECT_EQUAL(0x34, (int) buf.GetDrainPos()[1] & 0xff); -} - TEST("require that rawbuf can 'putToInet' 32-bit numbers") { RawBuf buf(1); buf.PutToInet(0x12345678); diff --git a/searchlib/src/vespa/searchlib/common/hitrank.h b/searchlib/src/vespa/searchlib/common/hitrank.h index 652d431193b..092855878c2 100644 --- a/searchlib/src/vespa/searchlib/common/hitrank.h +++ b/searchlib/src/vespa/searchlib/common/hitrank.h @@ -7,7 +7,6 @@ namespace search { typedef double HitRank; -typedef double SignedHitRank; constexpr HitRank default_rank_value = -HUGE_VAL; constexpr HitRank zero_rank_value = 0.0; diff --git a/searchlib/src/vespa/searchlib/util/rawbuf.cpp b/searchlib/src/vespa/searchlib/util/rawbuf.cpp index d64d85f2f1d..2ce9d52b2ce 100644 --- a/searchlib/src/vespa/searchlib/util/rawbuf.cpp +++ b/searchlib/src/vespa/searchlib/util/rawbuf.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace search { @@ -130,19 +130,6 @@ RawBuf::preAlloc(size_t len) assert(static_cast(_bufEnd -_bufFillPos) >= len); } - -void -RawBuf::Compact() -{ - if (_bufDrainPos == _bufStart) - return; - if (_bufFillPos != _bufDrainPos) - memmove(_bufStart, _bufDrainPos, _bufFillPos - _bufDrainPos); - _bufFillPos -= (_bufDrainPos - _bufStart); - _bufDrainPos = _bufStart; -} - - void RawBuf::Reuse() { @@ -163,7 +150,7 @@ RawBuf::Reuse() void -RawBuf::operator+=(const char *src) +RawBuf::append(const char *src) { while (*src) { char *cachedBufFillPos = _bufFillPos; @@ -176,37 +163,6 @@ RawBuf::operator+=(const char *src) } } - -void -RawBuf::operator+=(const RawBuf& buffer) -{ - size_t nbytes = buffer.GetUsedLen(); - if (nbytes == 0) - return; - - while (GetFreeLen() < nbytes) - expandBuf(nbytes); - memcpy(_bufFillPos, buffer._bufDrainPos, nbytes); - _bufFillPos += nbytes; -} - - -bool -RawBuf::operator==(const RawBuf &buffer) const -{ - size_t nbytes = buffer.GetUsedLen(); - if (nbytes != GetUsedLen()) - return false; - - const char *p, *t; - for (p=_bufDrainPos, t=buffer._bufDrainPos; p<_bufFillPos; p++, t++) { - if (*p != *t) - return false; - } - - return true; -} - /** * Append the value of param 'num' to the buffer, as a decimal * number right adjusted in a field of width 'fieldw', remaining @@ -299,24 +255,6 @@ RawBuf::addNum64(int64_t num, size_t fieldw, char fill) _bufFillPos = cachedBufFillPos; } - -void -RawBuf::addHitRank(HitRank num) -{ - char buf1[100]; - snprintf(buf1, sizeof(buf1), "%g", static_cast(num)); - append(buf1, strlen(buf1)); -} - - -void -RawBuf::addSignedHitRank(SignedHitRank num) -{ - char buf1[100]; - snprintf(buf1, sizeof(buf1), "%g", static_cast(num)); - append(buf1, strlen(buf1)); -} - void RawBuf::ensureSizeInternal(size_t size) { expandBuf(size); diff --git a/searchlib/src/vespa/searchlib/util/rawbuf.h b/searchlib/src/vespa/searchlib/util/rawbuf.h index 258e3da88ed..a79e35be6c7 100644 --- a/searchlib/src/vespa/searchlib/util/rawbuf.h +++ b/searchlib/src/vespa/searchlib/util/rawbuf.h @@ -2,11 +2,8 @@ #pragma once -#include #include -#include - -class FastOS_FileInterface; +#include namespace search { /** @@ -27,38 +24,48 @@ private: size_t _initialSize; void ensureSizeInternal(size_t size); + void expandBuf(size_t needlen); + /** + * Convert unsigned int.s 'src', to interNet highendian order, at 'dst' + * or _bufFillPos. Update or return ref to next char after those filled in. + */ + static unsigned char* ToInet(uint32_t src, unsigned char* dst) { + *(dst + 3) = src; // The least significant 8 bits + src >>= 8; // of 'src' are stored. + *(dst + 2) = src; + src >>= 8; + *(dst + 1) = src; + src >>= 8; + *dst = src; + return dst + 4; + }; + static unsigned char* ToInet(uint64_t src, unsigned char* dst) { + ToInet(static_cast(src >> 32), dst); + ToInet(static_cast(src & 0xffffffffull), dst + 4); + return dst + 8; + }; public: RawBuf(const RawBuf &) = delete; RawBuf& operator=(const RawBuf &) = delete; explicit RawBuf(size_t size); // malloc-s given size, assigns to _bufStart ~RawBuf(); // Frees _bufStart, i.e. the char[]. - void operator+=(const char *src); - void operator+=(const RawBuf& buffer); - bool operator==(const RawBuf &buffer) const; void addNum(size_t num, size_t fieldw, char fill); void addNum32(int32_t num, size_t fieldw, char fill); void addNum64(int64_t num, size_t fieldw, char fill); - void addHitRank(HitRank num); - void addSignedHitRank(SignedHitRank num); - void append(const void *data, size_t len); + void append(const char *data); void append(uint8_t byte); void appendCompressedPositiveNumber(uint64_t n); void appendCompressedNumber(int64_t n); - void expandBuf(size_t needlen); size_t GetFreeLen() const { return _bufEnd - _bufFillPos; } size_t GetDrainLen() const { return _bufDrainPos - _bufStart; } const char *GetDrainPos() const { return _bufDrainPos; } const char *GetFillPos() const { return _bufFillPos; } - char * GetWritableFillPos() const { return _bufFillPos; } char * GetWritableFillPos(size_t len) { preAlloc(len); return _bufFillPos; } - void truncate(size_t offset) { _bufFillPos = _bufDrainPos + offset; } void preAlloc(size_t len); // Ensure room for 'len' more bytes. - size_t readFile(FastOS_FileInterface &file, size_t maxlen); void reset() { _bufDrainPos = _bufFillPos = _bufStart; } - void Compact(); void Reuse(); size_t GetUsedAndDrainLen() const { return _bufFillPos - _bufStart; } size_t GetUsedLen() const { return _bufFillPos - _bufDrainPos; } @@ -71,63 +78,17 @@ public: } } - /** - * Convert from interNet highendian order at 'src', to unsigned integers - */ - static uint16_t InetTo16(const unsigned char *src) { - return (static_cast(*src) << 8) + *(src + 1); - }; - - static uint32_t InetTo32(const unsigned char* src) { - return (((((static_cast(*src) << 8) + *(src + 1)) << 8) - + *(src + 2)) << 8) + *(src + 3); - }; - - /** - * Convert unsigned int.s 'src', to interNet highendian order, at 'dst' - * or _bufFillPos. Update or return ref to next char after those filled in. - */ - static unsigned char* ToInet(uint16_t src, unsigned char* dst) { - *(dst + 1) = static_cast(src); // The least significant 8 bits - src >>= 8; // of 'src' are stored. - *dst = static_cast(src); - return dst + 2; - }; - void Put16ToInet(uint16_t src) { - ensureSize(2); - _bufFillPos = reinterpret_cast - (ToInet(src, - reinterpret_cast(_bufFillPos))); - }; - static unsigned char* ToInet(uint32_t src, unsigned char* dst) { - *(dst + 3) = src; // The least significant 8 bits - src >>= 8; // of 'src' are stored. - *(dst + 2) = src; - src >>= 8; - *(dst + 1) = src; - src >>= 8; - *dst = src; - return dst + 4; - }; void PutToInet(uint32_t src) { ensureSize(4); - _bufFillPos = reinterpret_cast - (ToInet(src, - reinterpret_cast(_bufFillPos))); + _bufFillPos = reinterpret_cast(ToInet(src,reinterpret_cast(_bufFillPos))); }; - static unsigned char* ToInet(uint64_t src, unsigned char* dst) { - ToInet(static_cast(src >> 32), dst); - ToInet(static_cast(src & 0xffffffffull), dst + 4); - return dst + 8; - }; void Put64ToInet(uint64_t src) { ensureSize(8); - _bufFillPos = reinterpret_cast - (ToInet(src, - reinterpret_cast(_bufFillPos))); + _bufFillPos = reinterpret_cast(ToInet(src,reinterpret_cast(_bufFillPos))); }; + }; } -- cgit v1.2.3