File size: 3,326 Bytes
684db41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <gtest/gtest.h>

#include <chrono>

#include "kernel/rtlil.h"
#include "kernel/yosys.h"

YOSYS_NAMESPACE_BEGIN

namespace {

std::vector<IdString> bench_refs(TwinePool &twines, int count)
{
	IdString prefix = twines.add("$sortbench");
	std::vector<IdString> refs;
	refs.reserve(count);
	for (int i = 0; i < count; i++)
		refs.push_back(twines.add(TwineSpec::Suffix{prefix, stringf("$%08d", (count - i) * 7919 % count)}));
	return refs;
}

TEST(TwineSortTest, RenderedOrder)
{
	TwinePool twines;
	std::vector<IdString> refs = {
		twines.add("$c"),
		twines.add("$a"),
		twines.add("$b"),
	};
	std::sort(refs.begin(), refs.end(), RTLIL::sort_by_id_str(twines));
	EXPECT_EQ(twines.str(refs[0]), "$a");
	EXPECT_EQ(twines.str(refs[1]), "$b");
	EXPECT_EQ(twines.str(refs[2]), "$c");
}

TEST(TwineSortTest, PublicOrder)
{
	TwinePool twines;
	IdString pub = twines.add("\\same");
	IdString priv = pub.tag(false);

	ASSERT_EQ(twines.str(pub), "\\same");
	ASSERT_EQ(twines.str(priv), "same");

	RTLIL::sort_by_id_str less(twines);
	EXPECT_NE(less(pub, priv), less(priv, pub));
	EXPECT_TRUE(less(pub, priv));
}

TEST(TwineSortTest, WeakOrdering)
{
	TwinePool twines;
	std::vector<IdString> refs = bench_refs(twines, 500);
	RTLIL::sort_by_id_str less(twines);
	std::sort(refs.begin(), refs.end(), less);
	for (size_t i = 1; i < refs.size(); i++)
		ASSERT_FALSE(less(refs[i], refs[i - 1])) << "at " << i;
}

TEST(TwineSortTest, GeneralWalk)
{
	TwinePool twines;
	std::vector<IdString> refs;

	IdString a = twines.add("$alpha");
	IdString b = twines.add("\\alpha");
	IdString c = twines.add("$alphabet");
	refs.insert(refs.end(), {a, b, c});

	IdString deep = a;
	for (int i = 0; i < 12; i++) {
		deep = twines.add(TwineSpec::Suffix{deep, stringf(".lvl%d", i)});
		refs.push_back(deep);
		refs.push_back(deep.tag(true));
	}

	IdString other = twines.add("$alpha.");
	for (int i = 0; i < 12; i++) {
		other = twines.add(TwineSpec::Suffix{other, stringf("lvl%d.", i)});
		refs.push_back(other);
	}

	RTLIL::sort_by_id_str less(twines);
	for (IdString x : refs)
		for (IdString y : refs) {
			bool want = twines.str(x) < twines.str(y);
			EXPECT_EQ(less(x, y), want)
				<< twines.str(x) << " vs " << twines.str(y);
		}
}

TEST(TwineSortTest, BenchmarkSort)
{
	constexpr int kNames = 50000;

	TwinePool twines;
	std::vector<IdString> refs = bench_refs(twines, kNames);

	auto t0 = std::chrono::steady_clock::now();
	std::sort(refs.begin(), refs.end(), RTLIL::sort_by_id_str(twines));
	auto t1 = std::chrono::steady_clock::now();

	size_t flattens = 0;
	std::vector<IdString> naive = bench_refs(twines, kNames);
	auto t2 = std::chrono::steady_clock::now();
	std::sort(naive.begin(), naive.end(), [&](IdString a, IdString b) {
		flattens += 2;
		return twines.str(a) < twines.str(b);
	});
	auto t3 = std::chrono::steady_clock::now();

	auto ms = [](auto a, auto b) {
		return std::chrono::duration_cast<std::chrono::microseconds>(b - a).count() / 1000.0;
	};
	std::cerr << "[ BENCH    ] " << kNames << " refs: walked " << ms(t0, t1)
		  << " ms vs per-comparison " << ms(t2, t3) << " ms ("
		  << flattens << " flattens vs " << kNames << ")\n";
	RecordProperty("walked_ms", std::to_string(ms(t0, t1)));
	RecordProperty("naive_ms", std::to_string(ms(t2, t3)));

	EXPECT_LT(ms(t0, t1), ms(t2, t3));
}

} // namespace

YOSYS_NAMESPACE_END