From e588e4b0dc2e3b728772d0aed2b23dec62893fef Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 20 Jan 2023 23:00:14 +0800 Subject: [PATCH] improved scratch space --- engine/utils.py | 2 +- header.cxx | 1 + reconstruct/ast.py | 10 ++++++---- server/aggregations.h | 4 +--- server/gc.h | 8 ++++---- server/libaquery.cpp | 1 + server/vector_type.hpp | 20 +++++++++++++------- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/engine/utils.py b/engine/utils.py index 8e65fcd..59b1309 100644 --- a/engine/utils.py +++ b/engine/utils.py @@ -157,4 +157,4 @@ def get_innermost(sl): elif sl and type(sl) is list: return get_innermost(sl[0]) else: - return sl \ No newline at end of file + return sl diff --git a/header.cxx b/header.cxx index 612b4a9..73bdce1 100644 --- a/header.cxx +++ b/header.cxx @@ -5,6 +5,7 @@ #include "./server/gc.h" __AQEXPORT__(void) __AQ_Init_GC__(Context* cxt) { GC::gc_handle = static_cast(cxt->gc); + GC::scratch_space = nullptr; } #else // __AQ_USE_THREADEDGC__ diff --git a/reconstruct/ast.py b/reconstruct/ast.py index 31b3939..a0dbe7b 100644 --- a/reconstruct/ast.py +++ b/reconstruct/ast.py @@ -4,8 +4,8 @@ from enum import Enum, auto from typing import Dict, List, Optional, Set, Tuple, Union from engine.types import * -from engine.utils import (base62alp, base62uuid, enlist, get_innermost, - get_legal_name) +from engine.utils import (base62alp, base62uuid, enlist, + get_innermost, get_legal_name) from reconstruct.storage import ColRef, Context, TableInfo class ast_node: @@ -599,7 +599,7 @@ class groupby_c(ast_node): self.context.emitc(f'{c}.reserve({self.group}.size());') if col_tovec[i]: # and type(col_types[i]) is VectorT: typename : Types = col_types[i] # .inner_type - self.context.emitc(f'auto buf_{c} = static_cast<{typename.cname} *>(malloc({self.total_sz} * sizeof({typename.cname})));') + self.context.emitc(f'auto buf_{c} = static_cast<{typename.cname} *>(calloc({self.total_sz}, sizeof({typename.cname})));') tovec_columns.add(c) self.arr_len = 'arrlen_' + base62uuid(3) self.arr_values = 'arrvals_' + base62uuid(3) @@ -617,7 +617,8 @@ class groupby_c(ast_node): f'[{preproc_scanner_it}]);' ) preproc_scanner.finalize() - + + self.context.emitc(f'GC::scratch_space = GC::gc_handle ? &(GC::gc_handle->scratch) : nullptr;') # gscanner = scan(self, self.group, loop_style = 'for_each') gscanner = scan(self, self.arr_len) key_var = 'key_'+base62uuid(7) @@ -683,6 +684,7 @@ class groupby_c(ast_node): gscanner.add(f'{ce[0]}.emplace_back({get_var_names_ex(ex)});\n') gscanner.finalize() + self.context.emitc(f'GC::scratch_space = nullptr;') self.datasource.groupinfo = None diff --git a/server/aggregations.h b/server/aggregations.h index a51c011..5b67e03 100644 --- a/server/aggregations.h +++ b/server/aggregations.h @@ -13,7 +13,7 @@ size_t count(const VT& v) { } template -constexpr static inline size_t count(const T&) { return 1; } +constexpr static size_t count(const T&) { return 1; } // TODO: Specializations for dt/str/none template class VT> @@ -441,14 +441,12 @@ inline decayed_t aggnext(const VT& arr) { template class VT> T last(const VT& arr) { if (!arr.size) return 0; - const uint32_t& len = arr.size; return arr[arr.size - 1]; } template class VT> T first(const VT& arr) { if (!arr.size) return 0; - const uint32_t& len = arr.size; return arr[0]; } diff --git a/server/gc.h b/server/gc.h index 45c827e..0ba5f1d 100644 --- a/server/gc.h +++ b/server/gc.h @@ -1,4 +1,5 @@ #pragma once +#include class ScratchSpace { public: @@ -35,10 +36,8 @@ public: #ifndef __AQ_USE_THREADEDGC__ -#include class GC { -private:; - +private: size_t max_slots, interval, forced_clean, forceclean_timer = 0; @@ -53,7 +52,6 @@ private:; std::atomic current_size; volatile bool lock; using gc_deallocator_t = void (*)(void*); - ScratchSpace scratch; // maybe use volatile std::thread::id instead protected: void acquire_lock(); @@ -64,6 +62,7 @@ protected: void terminate_daemon(); public: + ScratchSpace scratch; void reg(void* v, uint32_t sz = 0xffffffff, void(*f)(void*) = free ); @@ -92,6 +91,7 @@ public: } static GC* gc_handle; + static ScratchSpace *scratch_space; template static inline gc_deallocator_t _delete(T*) { return [](void* v){ diff --git a/server/libaquery.cpp b/server/libaquery.cpp index fc8f5dc..b59f658 100644 --- a/server/libaquery.cpp +++ b/server/libaquery.cpp @@ -468,6 +468,7 @@ void GC::reg(void* v, uint32_t sz, void(*f)(void*)) { //~ 40ns expected v. free #endif inline GC* GC::gc_handle = nullptr; +inline ScratchSpace* GC::scratch_space = nullptr; void ScratchSpace::init(size_t initial_capacity) { ret = nullptr; diff --git a/server/vector_type.hpp b/server/vector_type.hpp index e0c68ff..caa0f33 100644 --- a/server/vector_type.hpp +++ b/server/vector_type.hpp @@ -49,20 +49,25 @@ public: this->container = vt.container; // puts("move"); vt.size = vt.capacity = 0; - vt.container = 0; + vt.container = nullptr; } public: _Ty* container; uint32_t size, capacity; typedef _Ty* iterator_t; typedef std::conditional_t(), astring_view, _Ty> value_t; - vector_type(const uint32_t& size) : size(size), capacity(size) { + explicit vector_type(const uint32_t& size) : size(size), capacity(size) { + if (GC::scratch_space != nullptr) { + [[likely]] + container = (_Ty*)GC::scratch_space->alloc(size * sizeof(_Ty)); + } container = (_Ty*)malloc(size * sizeof(_Ty)); // TODO: calloc for objects. } - constexpr vector_type(std::initializer_list<_Ty> _l) { + explicit constexpr vector_type(std::initializer_list<_Ty> _l) { size = capacity = _l.size(); - _Ty* _container = this->container = (_Ty*)malloc(sizeof(_Ty) * _l.size()); + this->container = (_Ty*)malloc(sizeof(_Ty) * capacity); + _Ty* _container = this->container; for (const auto& l : _l) { *(_container++) = l; } @@ -80,8 +85,9 @@ public: constexpr vector_type(vector_type<_Ty>&& vt) noexcept : capacity(0) { _move(std::move(vt)); } - vector_type(vectortype_cstorage vt) noexcept : capacity(vt.capacity), size(vt.size), container((_Ty*)vt.container) { - out(10); + explicit vector_type(vectortype_cstorage vt) noexcept : + capacity(vt.capacity), size(vt.size), container((_Ty*)vt.container) { + // out(10); }; // size >= capacity ==> readonly vector constexpr vector_type(const uint32_t size, void* data) : @@ -499,7 +505,7 @@ public: } inline void hashtable_push(Key&& k, uint32_t i){ - reversemap[i] = ankerl::unordered_dense::set::hashtable_push(std::forward(k)); + reversemap[i] = ankerl::unordered_dense::set::hashtable_push(std::move(k)); ++ht_base[reversemap[i]]; }