Skip to content

Commit

Permalink
Merge pull request #23839 from vespa-engine/balder/gc-unused-rawbuf-code
Browse files Browse the repository at this point in the history
GC unused RawBuf functionality
  • Loading branch information
baldersheim authored Aug 29, 2022
2 parents 0f17d15 + 241b24f commit e451e22
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 249 deletions.
1 change: 0 additions & 1 deletion searchlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions searchlib/src/tests/prettyfloat/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions searchlib/src/tests/prettyfloat/CMakeLists.txt

This file was deleted.

31 changes: 0 additions & 31 deletions searchlib/src/tests/prettyfloat/prettyfloat.cpp

This file was deleted.

78 changes: 2 additions & 76 deletions searchlib/src/tests/util/rawbuf_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <vespa/searchlib/util/rawbuf.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/fastos/file.h>

#include <vespa/log/log.h>
LOG_SETUP("rawbuf_test");
Expand All @@ -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 <typename T>
void checkAddNum(void (RawBuf::*addNum)(T, size_t, char), size_t num,
size_t fieldw, char fill, const string &expected) {
Expand Down Expand Up @@ -79,52 +43,22 @@ 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");
buf.append(data.data(), data.size());
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);
Expand All @@ -135,22 +69,14 @@ 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());
EXPECT_EQUAL(26u, buf.GetUsedAndDrainLen());
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);
Expand Down
1 change: 0 additions & 1 deletion searchlib/src/vespa/searchlib/common/hitrank.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
66 changes: 2 additions & 64 deletions searchlib/src/vespa/searchlib/util/rawbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vespa/vespalib/util/compress.h>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cstdlib>

namespace search {

Expand Down Expand Up @@ -130,19 +130,6 @@ RawBuf::preAlloc(size_t len)
assert(static_cast<size_t>(_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()
{
Expand All @@ -163,7 +150,7 @@ RawBuf::Reuse()


void
RawBuf::operator+=(const char *src)
RawBuf::append(const char *src)
{
while (*src) {
char *cachedBufFillPos = _bufFillPos;
Expand All @@ -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
Expand Down Expand Up @@ -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<double>(num));
append(buf1, strlen(buf1));
}


void
RawBuf::addSignedHitRank(SignedHitRank num)
{
char buf1[100];
snprintf(buf1, sizeof(buf1), "%g", static_cast<double>(num));
append(buf1, strlen(buf1));
}

void
RawBuf::ensureSizeInternal(size_t size) {
expandBuf(size);
Expand Down
Loading

0 comments on commit e451e22

Please sign in to comment.