Permalink
Browse files

Add HyperBuilder#load

Object method version of HyperBuilder.load.  Will lower memory usage
if you create and tear down a lot of objects between GC runs.
  • Loading branch information...
1 parent 1ec374f commit 00ad8a9e892917654b59e2973830cbd2d2badd42 @natemueller natemueller committed Feb 27, 2013
Showing with 10 additions and 8 deletions.
  1. +10 −8 ext/hyperloglog.cpp
View
18 ext/hyperloglog.cpp
@@ -92,12 +92,9 @@ extern "C" VALUE hyperbuilder_new(VALUE klass, VALUE bits) {
return Data_Wrap_Struct(klass, 0, hyperbuilder_free, builder);
}
-extern "C" VALUE hyperbuilder_load(VALUE klass, VALUE bits, VALUE serialized) {
- HyperBuilder *builder = ALLOC(HyperBuilder);
-
- builder->bits = FIX2INT(bits);
- builder->registerCount = static_cast<uword32>(pow(2, FIX2INT(bits)));
- builder->registers = new BoolArray<uword64>(static_cast<size_t>( (floor(builder->registerCount / 12) + 1) * 64) );
+extern "C" VALUE hyperbuilder_obj_load(VALUE self, VALUE serialized) {
+ HyperBuilder *builder;
+ Data_Get_Struct(self, HyperBuilder, builder);
EWAHBoolArray<uword64> *registers = new EWAHBoolArray<uword64>();
stringstream ss;
@@ -110,8 +107,12 @@ extern "C" VALUE hyperbuilder_load(VALUE klass, VALUE bits, VALUE serialized) {
hyperbuilder_set_register(builder->registers, r, estimatorValue);
}
- free(registers);
- return Data_Wrap_Struct(klass, 0, hyperbuilder_free, builder);
+ delete(registers);
+ return self;
+}
+
+extern "C" VALUE hyperbuilder_load(VALUE klass, VALUE bits, VALUE serialized) {
+ return hyperbuilder_obj_load(hyperbuilder_new(klass, bits), serialized);
}
extern "C" VALUE hyperbuilder_offer(VALUE self, VALUE item) {
@@ -349,6 +350,7 @@ extern "C" void Init_hyperloglog() {
rbHyperBuilder = rb_define_class("HyperBuilder", rb_cObject);
rb_define_singleton_method(rbHyperBuilder, "new", (ruby_method*) &hyperbuilder_new, 1);
rb_define_singleton_method(rbHyperBuilder, "load", (ruby_method*) &hyperbuilder_load, 2);
+ rb_define_method(rbHyperBuilder, "load", (ruby_method*) &hyperbuilder_obj_load, 1);
rb_define_method(rbHyperBuilder, "offer", (ruby_method*) &hyperbuilder_offer, 1);
rb_define_method(rbHyperBuilder, "reset", (ruby_method*) &hyperbuilder_reset, 0);

0 comments on commit 00ad8a9

Please sign in to comment.