hyperscan
hyperscan copied to clipboard
Session-aware-allocation supporting with hyperscan
hi, everybody
I am using hyperscan do some serious heavy-load query works in c++. For the best performance, our project hire session-aware-memory-allocation technology, just like Google's Arena.
pls see Protobuf's Arena Document.
I noticed that hyperscan library support use customized allocation function. from
hs_set_allocator
but this API was not designed for our scenairo, we cannot passing a c++ object's member funtion as c-style-function-ptr, so I need an another parameter just like a void*, then I can pass the Arena object ptr into the stateless global allocation function.
hs_error_t hs_set_allocator(void*(size_t, void*) alloc_func, void* ctx, void(void*, void*) free_func, void* ctx)
I hope u guys like this idea, and implement it, it can really boost hyperscan in some heavy-load quering services.
I think you can use the following workaround to overcome the limitation:
void * instance_to_assign = nullptr;
void * my_alloc_impl(void * ptr, std::size_t sz){
/* your instance-aware allocator call here */
}
void* my_alloc(std::size_t sz){
thread_local void * instance = instance_to_assign;
return my_alloc_impl(instance, sz);
}
struct my_allocator{};
int main(void) {
auto my_alloc_instance = new my_allocator();
instance_to_assign= (void*)my_alloc_instance;
// Call once to init the thread-local variable in the function
my_alloc(0);
hs_set_allocator(&my_alloc);
}
thread-local really works for this case, and I have to use multiple allocator instances within single thread. I wonder why hyperscan do not support more flexible hs_set_allocator.
Sorry we don't have plans for it currently.