From 9caee2819fb284831c1cca2448820a52043c15f5 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 9 Sep 2022 20:42:04 +0800 Subject: [PATCH] libaquery: optimizations that reduces vector copy. --- msc-plugin/msc-plugin.vcxproj | 8 ++++---- reconstruct/ast.py | 2 +- server/server.cpp | 22 +++------------------- server/server.sln | 1 - server/table.h | 22 +++++++++++++++++++--- server/vector_type.hpp | 25 +++++++++++++++++++------ tests/funcs.a | 2 +- tests/joins.a | 6 +++--- tests/udf3.a | 3 ++- 9 files changed, 52 insertions(+), 39 deletions(-) diff --git a/msc-plugin/msc-plugin.vcxproj b/msc-plugin/msc-plugin.vcxproj index 9dc1824..95da320 100644 --- a/msc-plugin/msc-plugin.vcxproj +++ b/msc-plugin/msc-plugin.vcxproj @@ -30,14 +30,14 @@ DynamicLibrary true - v142 + v143 Unicode false DynamicLibrary false - v142 + v143 true Unicode true @@ -45,7 +45,7 @@ DynamicLibrary true - v142 + v143 Unicode false false @@ -53,7 +53,7 @@ DynamicLibrary false - v142 + v143 true Unicode true diff --git a/reconstruct/ast.py b/reconstruct/ast.py index d94b6ca..cfefac6 100644 --- a/reconstruct/ast.py +++ b/reconstruct/ast.py @@ -930,7 +930,7 @@ class udf(ast_node): cname = get_legal_name(args) self.var_table[args] = cname self.args.append(cname) - front = [*self.code_list, ', '.join([f'auto {a}' for a in self.args])] + front = [*self.code_list, ', '.join([f'const auto& {a}' for a in self.args])] self.code_list = [] self.with_storage = False diff --git a/server/server.cpp b/server/server.cpp index 193eb9e..56a1dbe 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -278,25 +278,9 @@ int test_main() printf("sibal: %p %p\n", aa, bb); const char* qs[]= { - "SELECT MIN(3)-MAX(2);", - "CREATE TABLE stocks(timestamp INT, price INT);", - "INSERT INTO stocks VALUES(1, 15);;", - "INSERT INTO stocks VALUES(2,19); ", - "INSERT INTO stocks VALUES(3,16);", - "INSERT INTO stocks VALUES(4,17);", - "INSERT INTO stocks VALUES(5,15);", - "INSERT INTO stocks VALUES(6,13);", - "INSERT INTO stocks VALUES(7,5);", - "INSERT INTO stocks VALUES(8,8);", - "INSERT INTO stocks VALUES(9,7);", - "INSERT INTO stocks VALUES(10,13);", - "INSERT INTO stocks VALUES(11,11);", - "INSERT INTO stocks VALUES(12,14);", - "INSERT INTO stocks VALUES(13,10);", - "INSERT INTO stocks VALUES(14,5);", - "INSERT INTO stocks VALUES(15,2);", - "INSERT INTO stocks VALUES(16,5);", - "SELECT price, timestamp FROM stocks WHERE (((price - timestamp) > 1) AND (NOT ((price * timestamp) < 100))) ;", + "CREATE TABLE test1(a INT, b INT, c INT, d INT);", + "COPY OFFSET 2 INTO test1 FROM 'W:/gg/AQuery++/data/test.csv' ON SERVER USING DELIMITERS ',';", + "SELECT sum(a), b, c, d FROM test1 GROUP BY c, b, d ORDER BY b ;", }; n_recv = sizeof(qs)/(sizeof (char*)); n_recvd = const_cast(qs); diff --git a/server/server.sln b/server/server.sln index 8275e9d..d1680fa 100644 --- a/server/server.sln +++ b/server/server.sln @@ -93,4 +93,3 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {572EA821-8162-4161-9AC2-464C79F08B47} EndGlobalSection -EndGlobal diff --git a/server/table.h b/server/table.h index 5bf1341..d2bbf88 100644 --- a/server/table.h +++ b/server/table.h @@ -32,6 +32,8 @@ public: typedef ColRef<_Ty> Decayed_t; const char* name; types::Type_t ty = types::Type_t::ERROR; + ColRef(const ColRef<_Ty>& vt) : vector_type<_Ty>(vt) {} + ColRef(ColRef<_Ty>&& vt) : vector_type<_Ty>(std::move(vt)) {} ColRef() : vector_type<_Ty>(0), name("") {} ColRef(const uint32_t& size, const char* name = "") : vector_type<_Ty>(size), name(name) {} ColRef(const char* name) : name(name) {} @@ -62,10 +64,22 @@ public: } ColRef(const char* name, types::Type_t ty) : name(name), ty(ty) {} using vector_type<_Ty>::operator[]; - using vector_type<_Ty>::operator=; + //using vector_type<_Ty>::operator=; using vector_type<_Ty>::subvec; using vector_type<_Ty>::subvec_memcpy; using vector_type<_Ty>::subvec_deep; + ColRef<_Ty>& operator= (const _Ty& vt){ + vector_type<_Ty>::operator=(vt); + return *this; + } + ColRef<_Ty>& operator =(const ColRef<_Ty>& vt) { + vector_type<_Ty>::operator=(vt); + return *this; + } + ColRef<_Ty>& operator =(ColRef<_Ty>&& vt) { + vector_type<_Ty>::operator=(std::move(vt)); + return *this; + } ColView<_Ty> operator [](const vector_type&idxs) const { return ColView<_Ty>(*this, idxs); } @@ -100,6 +114,8 @@ public: void* monetdb_get_col(); }; +template<> +class ColRef : public ColRef{}; template class ColView { @@ -151,11 +167,11 @@ public: ret[i] = orig[idxs[i]]; return ret; } - ColView<_Ty> subvec(uint32_t start, uint32_t end) { + ColView<_Ty> subvec(uint32_t start, uint32_t end) const { uint32_t len = end - start; return ColView<_Ty>(orig, idxs.subvec(start, end)); } - ColRef<_Ty> subvec_deep(uint32_t start, uint32_t end) { + ColRef<_Ty> subvec_deep(uint32_t start, uint32_t end) const{ uint32_t len = end - start; ColRef<_Ty> subvec(len); for (uint32_t i = 0; i < len; ++i) diff --git a/server/vector_type.hpp b/server/vector_type.hpp index 378ed01..e782c93 100644 --- a/server/vector_type.hpp +++ b/server/vector_type.hpp @@ -29,9 +29,14 @@ public: this->size = vt.size; this->capacity = vt.capacity; - this->container = (_Ty*)malloc(size * sizeof(_Ty)); - - memcpy(container, vt.container, sizeof(_Ty) * size); + if (capacity) { + puts("copy"); + this->container = (_Ty*)malloc(size * sizeof(_Ty)); + memcpy(container, vt.container, sizeof(_Ty) * size); + } + else { + this->container = vt.container; + } } void inline _move(vector_type<_Ty>&& vt) { if (capacity > 0) free(container); @@ -39,7 +44,7 @@ public: this->size = vt.size; this->capacity = vt.capacity; this->container = vt.container; - + puts("move"); vt.size = vt.capacity = 0; vt.container = 0; } @@ -60,7 +65,7 @@ public: } constexpr vector_type() noexcept : size(0), capacity(0), container(0) {}; constexpr vector_type(_Ty* container, uint32_t len) noexcept : size(len), capacity(0), container(container) {}; - constexpr vector_type(const vector_type<_Ty>& vt) noexcept : capacity(0) { + constexpr explicit vector_type(const vector_type<_Ty>& vt) noexcept : capacity(0) { _copy(vt); } constexpr vector_type(vector_type<_Ty>&& vt) noexcept : capacity(0) { @@ -107,18 +112,26 @@ public: return *this; } - void emplace_back(_Ty _val) { + inline void grow() { if (size >= capacity) { // geometric growth uint32_t new_capacity = size + 1 + (size >> 1); _Ty* n_container = (_Ty*)malloc(new_capacity * sizeof(_Ty)); memcpy(n_container, container, sizeof(_Ty) * size); + memset(n_container + size, 0, sizeof(_Ty) * (new_capacity - size)); if (capacity) free(container); container = n_container; capacity = new_capacity; } + } + void emplace_back(const _Ty& _val) { + grow(); container[size++] = _val; } + void emplace_back(_Ty&& _val) { + grow(); + container[size++] = std::move(_val); + } iterator_t erase(iterator_t _it) { #ifdef DEBUG // Do bound checks diff --git a/tests/funcs.a b/tests/funcs.a index 817efdd..65316ce 100644 --- a/tests/funcs.a +++ b/tests/funcs.a @@ -22,4 +22,4 @@ FIELDS TERMINATED BY "," SELECT pairCorr(c, b) * d, sum(a), b FROM test1 group by c,b,d -order by b ASC \ No newline at end of file +order by b ASC diff --git a/tests/joins.a b/tests/joins.a index 3510b6f..525a46f 100644 --- a/tests/joins.a +++ b/tests/joins.a @@ -24,11 +24,11 @@ LOAD DATA INFILE "data/test.csv" INTO TABLE tt FIELDS TERMINATED BY "," -CREATE TABLE sale(Mont INT, sales INT) +CREATE TABLE sale1(Mont INT, sales INT) LOAD DATA INFILE "data/moving_avg.csv" -INTO TABLE sale +INTO TABLE sale1 FIELDS TERMINATED BY "," -select sd(a) + sales from tt, sale where tt.a = sale.Mont +select sd(a) + sales from tt, sale1 where tt.a = sale1.Mont diff --git a/tests/udf3.a b/tests/udf3.a index b345f0d..fb78cb7 100644 --- a/tests/udf3.a +++ b/tests/udf3.a @@ -1,6 +1,6 @@ -AGGREGATION FUNCTION covariances2(x, y, w){ +AGGREGATION FUNCTION covariances2(x, y, win){ xmeans := 0.; ymeans := 0.; l := _builtin_len; @@ -11,6 +11,7 @@ AGGREGATION FUNCTION covariances2(x, y, w){ ymeans := y[0]; _builtin_ret[0] := 0.; } + w := win; if (w > l) w := l; for (i := 1, j:= 0; i < w; i := i+1) {