Commit 62a3f823 authored by Igor Kushnir's avatar Igor Kushnir

Add benchmarks sensitive to DUChainReferenceCounting's performance

parent ec059fd9
/*
* This file is part of KDevelop
* Copyright 2012-2013 Milian Wolff <mail@milianw.de>
* Copyright 2020 Igor Kushnir <igorkuo@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
......@@ -24,9 +25,13 @@
#include <language/util/kdevhash.h>
#include <serialization/itemrepositoryregistry.h>
#include <serialization/indexedstring.h>
#include <tests/testhelpers.h>
#include <QTest>
#include <QStandardPaths>
#include <vector>
QTEST_GUILESS_MAIN(BenchIndexedString)
using namespace KDevelop;
......@@ -181,3 +186,59 @@ void BenchIndexedString::bench_qSet()
}
}
}
static std::vector<IndexedString> createIndexedStrings(std::size_t count)
{
std::vector<IndexedString> result;
// result.reserve(count) is called after verifying that count is not too great.
constexpr char first{33};
constexpr char last{127};
constexpr std::size_t dataSize{4};
std::size_t maxCount{1};
for (std::size_t i = 0; i < dataSize; ++i) {
maxCount *= (last - first);
}
// Subtract 1 to account for the fact that count is checked at the beginning
// of the innermost loop in order to support count == 0.
--maxCount;
QVERIFY_RETURN(count <= maxCount, result);
result.reserve(count);
char data[dataSize + 1] = {};
QCOMPARE_RETURN(data[dataSize], 0, result);
for (char a = first; a != last; ++a) {
data[0] = a;
for (char b = first; b != last; ++b) {
data[1] = b;
for (char c = first; c != last; ++c) {
data[2] = c;
for (char d = first; d != last; ++d) {
if (count-- == 0) {
return result;
}
data[3] = d;
result.emplace_back(data, dataSize);
}
}
}
}
Q_UNREACHABLE();
}
void BenchIndexedString::bench_create()
{
QBENCHMARK_ONCE {
createIndexedStrings(1'000'000);
}
}
void BenchIndexedString::bench_destroy()
{
auto strings = createIndexedStrings(10'000'000);
QBENCHMARK_ONCE {
strings = {};
}
}
......@@ -44,6 +44,9 @@ private Q_SLOTS:
void bench_kdevhash();
void bench_qSet();
void bench_create();
void bench_destroy();
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/bench_indexedstring");
};
......
......@@ -23,9 +23,12 @@
#include <serialization/itemrepository.h>
#include <serialization/indexedstring.h>
#include <serialization/referencecounting.h>
#include <algorithm>
#include <limits>
#include <random>
#include <vector>
#include <QTest>
#include <QStandardPaths>
......@@ -227,3 +230,42 @@ void BenchItemRepository::lookupValue()
}
}
}
void BenchItemRepository::shouldDoReferenceCounting_data()
{
QTest::addColumn<bool>("enableReferenceCounting");
QTest::newRow("disabled") << false;
QTest::newRow("enabled") << true;
}
void BenchItemRepository::shouldDoReferenceCounting()
{
using Type = unsigned;
static_assert(sizeof(Type) == sizeof(IndexedString),
"Type emulates inlined IndexedString and should be of the same size.");
constexpr std::size_t valueCount{100'000'000};
std::vector<Type> values(valueCount);
QFETCH(bool, enableReferenceCounting);
if (enableReferenceCounting) {
enableDUChainReferenceCounting(&values.front(), values.size() * sizeof(Type));
}
// NOTE: switching CountType from int to std::size_t slows down the "disabled" benchmark
// by 17% and the "enabled" benchmark by 5%, as does removing the static_cast<CountType> below!
using CountType = int;
CountType count{0};
static_assert(valueCount <= std::numeric_limits<CountType>::max());
QBENCHMARK_ONCE {
for (auto& v : values) {
count += shouldDoDUChainReferenceCounting(&v);
}
}
if (enableReferenceCounting) {
disableDUChainReferenceCounting(&values.front());
QCOMPARE(count, static_cast<CountType>(values.size()));
} else {
QCOMPARE(count, 0);
}
}
......@@ -40,6 +40,9 @@ private Q_SLOTS:
void lookupKey();
void lookupValue();
void shouldDoReferenceCounting_data();
void shouldDoReferenceCounting();
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/bench_itemrepository");
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment