Skip to content

Commit

Permalink
Merge pull request #24166 from vespa-engine/balder/remove-unused-func…
Browse files Browse the repository at this point in the history
…tionality

GC unused functionality.
  • Loading branch information
baldersheim authored Sep 21, 2022
2 parents c808ea4 + 4782501 commit 1040425
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 225 deletions.
12 changes: 3 additions & 9 deletions searchlib/src/tests/query/stackdumpquerycreator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ using namespace search::query;

namespace {

template <typename T>
void append(RawBuf &buf, T i) {
buf.preAlloc(sizeof(T));
buf.PutToInet(i);
}

void appendString(RawBuf &buf, const string &s) {
buf.preAlloc(sizeof(uint32_t) + s.size());
buf.appendCompressedPositiveNumber(s.size());
Expand All @@ -50,7 +44,7 @@ TEST("requireThatTooLargeNumTermIsTreatedAsFloat") {
SimpleQueryStackDumpIterator query_stack(vespalib::stringref(buf.GetDrainPos(), buf.GetUsedLen()));
Node::UP node = StackDumpQueryCreator<SimpleQueryNodeTypes>::create(query_stack);
ASSERT_TRUE(node.get());
NumberTerm *term = dynamic_cast<NumberTerm *>(node.get());
auto *term = dynamic_cast<NumberTerm *>(node.get());
ASSERT_TRUE(term);
EXPECT_EQUAL(term_string, term->getTerm());
}
Expand All @@ -65,7 +59,7 @@ TEST("requireThatTooLargeFloatNumTermIsTreatedAsFloat") {
Node::UP node =
StackDumpQueryCreator<SimpleQueryNodeTypes>::create(query_stack);
ASSERT_TRUE(node.get());
NumberTerm *term = dynamic_cast<NumberTerm *>(node.get());
auto *term = dynamic_cast<NumberTerm *>(node.get());
ASSERT_TRUE(term);
EXPECT_EQUAL(term_string, term->getTerm());
}
Expand Down Expand Up @@ -97,7 +91,7 @@ TEST("require that PredicateQueryItem stack dump item can be read") {
Node::UP node =
StackDumpQueryCreator<SimpleQueryNodeTypes>::create(query_stack);
ASSERT_TRUE(node.get());
PredicateQuery *p = dynamic_cast<PredicateQuery *>(node.get());
auto *p = dynamic_cast<PredicateQuery *>(node.get());
ASSERT_TRUE(p);
const PredicateQueryTerm &term = *p->getTerm();
ASSERT_EQUAL(2u, term.getFeatures().size());
Expand Down
53 changes: 3 additions & 50 deletions searchlib/src/tests/util/rawbuf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace search;
namespace {

string getString(const RawBuf &buf) {
return string(buf.GetDrainPos(), buf.GetUsedLen());
return {buf.GetDrainPos(), buf.GetUsedLen()};
}

template <typename T>
Expand All @@ -24,25 +24,6 @@ void checkAddNum(void (RawBuf::*addNum)(T, size_t, char), size_t num,
EXPECT_EQUAL(expected, getString(buf));
}

TEST("require that rawbuf can add numbers in decimal") {
checkAddNum(&RawBuf::addNum, 0, 4, 'x', "xxx0");
checkAddNum(&RawBuf::addNum, 42, 4, '0', "0042");
checkAddNum(&RawBuf::addNum, 12345678901234, 4, '0', "12345678901234");
checkAddNum(&RawBuf::addNum, -1, 4, '0', "18446744073709551615");

checkAddNum(&RawBuf::addNum32, 0, 4, 'x', "xxx0");
checkAddNum(&RawBuf::addNum32, 42, 4, '0', "0042");
checkAddNum(&RawBuf::addNum32, 1234567890, 4, '0', "1234567890");
checkAddNum(&RawBuf::addNum32, -1, 0, '0', "-1");
checkAddNum(&RawBuf::addNum32, -1, 4, '0', "00-1");

checkAddNum(&RawBuf::addNum64, 0, 4, 'x', "xxx0");
checkAddNum(&RawBuf::addNum64, 42, 4, '0', "0042");
checkAddNum(&RawBuf::addNum64, 12345678901234, 4, '0', "12345678901234");
checkAddNum(&RawBuf::addNum64, -1, 0, '0', "-1");
checkAddNum(&RawBuf::addNum64, -1, 4, '0', "00-1");
}

TEST("require that rawbuf can append data of known length") {
RawBuf buf(10);
const string data("foo bar baz qux quux");
Expand All @@ -52,45 +33,17 @@ TEST("require that rawbuf can append data of known length") {

TEST("require that prealloc makes enough room") {
RawBuf buf(10);
buf.append("foo");
buf.append("foo", 3);
EXPECT_EQUAL(7u, buf.GetFreeLen());
buf.preAlloc(100);
EXPECT_EQUAL("foo", getString(buf));
EXPECT_LESS_EQUAL(100u, buf.GetFreeLen());
}

TEST("require that reusing a buffer that has grown 4x will alloc new buffer") {
RawBuf buf(10);
buf.preAlloc(100);
EXPECT_LESS_EQUAL(100u, buf.GetFreeLen());
buf.Reuse();
EXPECT_EQUAL(10u, buf.GetFreeLen());
}

TEST("require that various length and position information can be found.") {
RawBuf buf(30);
buf.append("foo bar baz qux quux corge");
buf.Drain(7);
EXPECT_EQUAL(7u, buf.GetDrainLen());
EXPECT_EQUAL(19u, buf.GetUsedLen());
EXPECT_EQUAL(26u, buf.GetUsedAndDrainLen());
EXPECT_EQUAL(4u, buf.GetFreeLen());
}

TEST("require that rawbuf can 'putToInet' 32-bit numbers") {
RawBuf buf(1);
buf.PutToInet(0x12345678);
EXPECT_EQUAL(4, buf.GetFillPos() - buf.GetDrainPos());
EXPECT_EQUAL(0x12, (int) buf.GetDrainPos()[0] & 0xff);
EXPECT_EQUAL(0x34, (int) buf.GetDrainPos()[1] & 0xff);
EXPECT_EQUAL(0x56, (int) buf.GetDrainPos()[2] & 0xff);
EXPECT_EQUAL(0x78, (int) buf.GetDrainPos()[3] & 0xff);
}

TEST("require that rawbuf can 'putToInet' 64-bit numbers") {
RawBuf buf(1);
buf.Put64ToInet(0x123456789abcdef0ULL);
EXPECT_EQUAL(8, buf.GetFillPos() - buf.GetDrainPos());
EXPECT_EQUAL(8ul, buf.GetUsedLen());
EXPECT_EQUAL(0x12, (int) buf.GetDrainPos()[0] & 0xff);
EXPECT_EQUAL(0x34, (int) buf.GetDrainPos()[1] & 0xff);
EXPECT_EQUAL(0x56, (int) buf.GetDrainPos()[2] & 0xff);
Expand Down
5 changes: 0 additions & 5 deletions searchlib/src/vespa/searchlib/query/tree/stackdumpcreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class QueryNodeConverter : public QueryVisitor {
_buf.appendCompressedNumber(n);
}

void appendInt(uint32_t i) {
_buf.preAlloc(sizeof(uint32_t));
_buf.PutToInet(i);
}

void appendLong(uint64_t l) {
_buf.preAlloc(sizeof(uint64_t));
_buf.Put64ToInet(l);
Expand Down
147 changes: 3 additions & 144 deletions searchlib/src/vespa/searchlib/util/rawbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ RawBuf::RawBuf(size_t size)
: _bufStart(nullptr),
_bufEnd(nullptr),
_bufFillPos(nullptr),
_bufDrainPos(nullptr),
_initialBufStart(nullptr),
_initialSize(size)
_bufDrainPos(nullptr)
{
if (size > 0) {
_bufStart = static_cast<char *>(malloc(size));
Expand All @@ -25,8 +23,7 @@ RawBuf::RawBuf(size_t size)

RawBuf::~RawBuf()
{
if (_bufStart != _initialBufStart)
free(_bufStart);
free(_bufStart);
}


Expand All @@ -48,8 +45,7 @@ RawBuf::expandBuf(size_t needlen)
memcpy(nbuf, _bufDrainPos, _bufFillPos - _bufDrainPos);
_bufFillPos = _bufFillPos - _bufDrainPos + nbuf;
_bufDrainPos = nbuf;
if (_bufStart != _initialBufStart)
free(_bufStart);
free(_bufStart);
_bufStart = nbuf;
_bufEnd = _bufStart + size;
}
Expand Down Expand Up @@ -92,18 +88,6 @@ RawBuf::appendCompressedNumber(int64_t n)
_bufFillPos += vespalib::compress::Integer::compress(n, _bufFillPos);
}

/**
* Free 'len' bytes from the start of the contents. (These
* have presumably been written or read.)
*/
void
RawBuf::Drain(size_t len)
{
_bufDrainPos += len;
if (_bufDrainPos == _bufFillPos)
reset();
}


/**
* Compact any free space from the beginning of the buffer, by
Expand All @@ -130,131 +114,6 @@ RawBuf::preAlloc(size_t len)
assert(static_cast<size_t>(_bufEnd -_bufFillPos) >= len);
}

void
RawBuf::Reuse()
{
if (static_cast<size_t>(_bufEnd - _bufStart) > _initialSize * 4) {
free(_bufStart);
if (_initialSize > 0) {
if (_initialBufStart != nullptr)
_bufStart = _initialBufStart;
else
_bufStart = static_cast<char *>(malloc(_initialSize));
assert(_bufStart != nullptr);
} else
_bufStart = nullptr;
_bufEnd = _bufStart + _initialSize;
}
_bufDrainPos = _bufFillPos = _bufStart;
}


void
RawBuf::append(const char *src)
{
while (*src) {
char *cachedBufFillPos = _bufFillPos;
const char *cachedBufEnd = _bufEnd;
while (cachedBufFillPos < cachedBufEnd && *src)
*cachedBufFillPos++ = *src++;
_bufFillPos = cachedBufFillPos;
if (_bufFillPos >= _bufEnd)
expandBuf(1);
}
}

/**
* Append the value of param 'num' to the buffer, as a decimal
* number right adjusted in a field of width 'fieldw', remaining
* space filled with 'fill' characters.
*/
void
RawBuf::addNum(size_t num, size_t fieldw, char fill)
{
char buf1[20];
char *p = buf1;
do {
*p++ = '0' + (num % 10);
num /= 10;
} while (num != 0);
size_t plen = p - buf1;
size_t wantlen = fieldw;
if (plen > wantlen)
wantlen = plen;
if (_bufFillPos + wantlen >= _bufEnd)
expandBuf(wantlen);
char *cachedBufFillPos = _bufFillPos;
while (plen < wantlen) {
*cachedBufFillPos++ = fill;
wantlen--;
}
while (p > buf1) {
*cachedBufFillPos++ = *--p;
}
_bufFillPos = cachedBufFillPos;
}


void
RawBuf::addNum32(int32_t num, size_t fieldw, char fill)
{
char buf1[11];
uint32_t unum = num >= 0 ? num : -num;
char *p = buf1;
do {
*p++ = '0' + (unum % 10);
unum /= 10;
} while (unum != 0);
if (num < 0)
*p++ = '-';
size_t plen = p - buf1;
size_t wantlen = fieldw;
if (plen > wantlen)
wantlen = plen;
if (_bufFillPos + wantlen >= _bufEnd)
expandBuf(wantlen);
char *cachedBufFillPos = _bufFillPos;
while (plen < wantlen) {
*cachedBufFillPos++ = fill;
wantlen--;
}
while (p > buf1) {
*cachedBufFillPos++ = *--p;
}
_bufFillPos = cachedBufFillPos;
}



void
RawBuf::addNum64(int64_t num, size_t fieldw, char fill)
{
char buf1[21];
uint64_t unum = num >= 0 ? num : -num;
char *p = buf1;
do {
*p++ = '0' + (unum % 10);
unum /= 10;
} while (unum != 0);
if (num < 0)
*p++ = '-';
size_t plen = p - buf1;
size_t wantlen = fieldw;
if (plen > wantlen)
wantlen = plen;
if (_bufFillPos + wantlen >= _bufEnd)
expandBuf(wantlen);
char *cachedBufFillPos = _bufFillPos;
while (plen < wantlen) {
*cachedBufFillPos++ = fill;
wantlen--;
}
while (p > buf1) {
*cachedBufFillPos++ = *--p;
}
_bufFillPos = cachedBufFillPos;
}

void
RawBuf::ensureSizeInternal(size_t size) {
expandBuf(size);
Expand Down
17 changes: 0 additions & 17 deletions searchlib/src/vespa/searchlib/util/rawbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class RawBuf
char* _bufEnd; // ref. to byte after last in buffer (don't mo)
char* _bufFillPos; // ref. to byte where next should be put in
char* _bufDrainPos; // ref. to next byte to take out of buffer
char* _initialBufStart;
size_t _initialSize;

void ensureSizeInternal(size_t size);
void expandBuf(size_t needlen);
Expand Down Expand Up @@ -50,26 +48,16 @@ class RawBuf
explicit RawBuf(size_t size); // malloc-s given size, assigns to _bufStart
~RawBuf(); // Frees _bufStart, i.e. the char[].

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 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);
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(size_t len) { preAlloc(len); return _bufFillPos; }
void preAlloc(size_t len); // Ensure room for 'len' more bytes.
void reset() { _bufDrainPos = _bufFillPos = _bufStart; }
void Reuse();
size_t GetUsedAndDrainLen() const { return _bufFillPos - _bufStart; }
size_t GetUsedLen() const { return _bufFillPos - _bufDrainPos; }
void Drain(size_t len); // Adjust drain pos.
void Fill(size_t len) { _bufFillPos += len; }

void ensureSize(size_t size) {
Expand All @@ -78,11 +66,6 @@ class RawBuf
}
}

void PutToInet(uint32_t src) {
ensureSize(4);
_bufFillPos = reinterpret_cast<char *>(ToInet(src,reinterpret_cast<unsigned char*>(_bufFillPos)));
};

void Put64ToInet(uint64_t src) {
ensureSize(8);
_bufFillPos = reinterpret_cast<char *>(ToInet(src,reinterpret_cast<unsigned char*>(_bufFillPos)));
Expand Down

0 comments on commit 1040425

Please sign in to comment.