mongory 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/ext/mongory_ext/extconf.rb +15 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
  10. data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
  11. data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
  12. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
  15. data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
  16. data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
  20. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
  22. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
  23. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
  27. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
  28. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
  30. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
  31. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
  32. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
  33. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
  34. data/ext/mongory_ext/mongory_ext.c +26 -4
  35. data/ext/mongory_ext/mongory_ext_internal.h +15 -3
  36. data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
  37. data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
  38. data/ext/mongory_ext/rb_mongory_value.c +124 -51
  39. data/lib/mongory/converters/abstract_converter.rb +5 -5
  40. data/lib/mongory/converters/converted.rb +0 -4
  41. data/lib/mongory/converters/key_converter.rb +13 -36
  42. data/lib/mongory/converters/value_converter.rb +4 -4
  43. data/lib/mongory/matchers.rb +8 -7
  44. data/lib/mongory/query_builder.rb +9 -0
  45. data/lib/mongory/utils/context.rb +0 -6
  46. data/lib/mongory/utils/singleton_builder.rb +1 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +0 -1
  49. data/script/bench.rb +77 -4
  50. data/script/coverage.rb +84 -0
  51. metadata +7 -7
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -28,24 +28,46 @@ rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void) {
28
28
  memcpy(&pool->base, base, sizeof(mongory_memory_pool));
29
29
  pool->owner = NULL;
30
30
  pool->next_scratch = NULL;
31
+ pool->scratch_refs_ptr = NULL;
32
+ pool->reported_bytes = 0;
31
33
  free(base);
32
34
 
33
35
  return pool;
34
36
  }
35
37
 
36
- bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message) {
37
- if (pool->error) {
38
- const char *msg = pool->error->message;
38
+ void rb_mongory_memory_pool_free(rb_mongory_memory_pool_t *pool) {
39
+ if (!pool) {
40
+ return;
41
+ }
42
+ if (pool->reported_bytes > 0) {
43
+ size_t prev = pool->reported_bytes;
44
+ pool->reported_bytes = 0;
45
+ rb_gc_adjust_memory_usage(-(ssize_t)prev);
46
+ }
47
+ pool->base.free(&pool->base);
48
+ }
49
+
50
+ void rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message) {
51
+ if (pool && pool->error) {
52
+ const char *msg = pool->error->message ? pool->error->message : "Unknown error";
39
53
  pool->error = NULL;
40
54
  rb_raise(rb_mongory_e_type_error, "%s: %s", error_message, msg);
41
55
  }
42
- return false;
43
56
  }
44
57
 
45
58
  void Init_mongory_ext(void) {
46
59
  // Initialize mongory core
47
60
  mongory_init();
48
61
 
62
+ rb_gc_register_address(&rb_mongory_m_mongory);
63
+ rb_gc_register_address(&rb_mongory_m_matchers);
64
+ rb_gc_register_address(&rb_mongory_c_matcher);
65
+ rb_gc_register_address(&rb_mongory_c_matcher_context);
66
+ rb_gc_register_address(&rb_mongory_e_error);
67
+ rb_gc_register_address(&rb_mongory_e_type_error);
68
+ rb_gc_register_address(&rb_mongory_data_converter);
69
+ rb_gc_register_address(&rb_mongory_condition_converter);
70
+
49
71
  // Define modules and classes
50
72
  rb_mongory_m_mongory = rb_define_module("Mongory");
51
73
  rb_mongory_m_matchers = rb_define_module_under(rb_mongory_m_mongory, "Matchers");
@@ -9,6 +9,8 @@
9
9
  #include "mongory-core.h"
10
10
  #include <ruby.h>
11
11
  #include <ruby/encoding.h>
12
+ #include <stdbool.h>
13
+ #include <string.h>
12
14
 
13
15
  // Shared Ruby module, class, error, and converter references initialized in Init_mongory_ext
14
16
  extern VALUE rb_mongory_m_mongory;
@@ -32,12 +34,15 @@ typedef struct rb_mongory_matcher_t {
32
34
  mongory_table *symbol_map;
33
35
  mongory_array *mark_list;
34
36
  VALUE ctx;
37
+ bool tracing;
35
38
  } rb_mongory_matcher_t;
36
39
 
37
40
  struct rb_mongory_memory_pool_t {
38
41
  mongory_memory_pool base;
39
42
  rb_mongory_matcher_t *owner;
40
43
  rb_mongory_memory_pool_t *next_scratch;
44
+ VALUE *scratch_refs_ptr;
45
+ size_t reported_bytes;
41
46
  };
42
47
 
43
48
  typedef struct rb_mongory_table_t {
@@ -49,12 +54,20 @@ typedef struct rb_mongory_table_t {
49
54
  typedef struct rb_mongory_array_t {
50
55
  mongory_array base;
51
56
  VALUE rb_array;
52
- rb_mongory_matcher_t *owner;
53
57
  } rb_mongory_array_t;
54
58
 
59
+ static inline char *rb_mongory_pool_strdup(mongory_memory_pool *pool, const char *str) {
60
+ if (!pool || !str) return NULL;
61
+ size_t len = strlen(str);
62
+ char *copy = (char *)MG_ALLOC(pool, len + 1);
63
+ if (copy) memcpy(copy, str, len + 1);
64
+ return copy;
65
+ }
66
+
55
67
  // Pool & error helpers (mongory_ext.c)
56
68
  rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void);
57
- bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
69
+ void rb_mongory_memory_pool_free(rb_mongory_memory_pool_t *pool);
70
+ void rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
58
71
 
59
72
  // Matcher registration (rb_mongory_matcher.c)
60
73
  void rb_mongory_init_matcher(void);
@@ -64,7 +77,6 @@ mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_valu
64
77
  mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value);
65
78
  mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash);
66
79
  mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array);
67
- void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value);
68
80
  VALUE rb_mongory_cache_fetch_string(rb_mongory_matcher_t *owner, const char *key);
69
81
  VALUE rb_mongory_cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key);
70
82
 
@@ -3,25 +3,42 @@
3
3
  * @brief Bridges mongory-core regex, value converter, and custom matcher hooks to Ruby.
4
4
  */
5
5
  #include "mongory_ext_internal.h"
6
- #include <string.h>
6
+ #include "mongory-core/src/foundations/memory_pool_private.h"
7
7
 
8
8
  // Replaces a string pattern with its compiled Regexp so later matches reuse it,
9
- // and keeps the Regexp alive for the GC.
9
+ // keeping `pattern->origin` unchanged while marking the cached Regexp in `owner->mark_list`.
10
10
  static VALUE rb_mongory_regex_cache(mongory_memory_pool *pool, mongory_value *pattern, VALUE rb_re) {
11
+ rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
12
+ rb_mongory_matcher_t *owner = rb_pool ? rb_pool->owner : NULL;
13
+ if (owner && owner->pool) {
14
+ mongory_memory_pool_lock(owner->pool);
15
+ }
11
16
  if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
12
- return (VALUE)pattern->data.regex;
17
+ VALUE cached = (VALUE)pattern->data.regex;
18
+ if (owner && owner->pool) {
19
+ mongory_memory_pool_unlock(owner->pool);
20
+ }
21
+ return cached;
13
22
  }
14
23
  mongory_memory_pool *pattern_pool = pattern->pool;
24
+ void *orig_origin = pattern->origin;
15
25
  mongory_value *temp_value = mongory_value_wrap_regex(pool, (void *)rb_re);
16
26
  if (temp_value) {
17
27
  memcpy(pattern, temp_value, sizeof(mongory_value));
18
28
  pattern->pool = pattern_pool;
19
- pattern->origin = (void *)rb_re;
20
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
21
- if (rb_pool->owner) {
22
- rb_pool->owner->mark_list->push(rb_pool->owner->mark_list, pattern);
29
+ pattern->origin = orig_origin;
30
+ if (owner && owner->pool && owner->mark_list) {
31
+ mongory_value *mark_entry = mongory_value_wrap_u(owner->pool, NULL);
32
+ if (mark_entry) {
33
+ mark_entry->origin = (void *)rb_re;
34
+ owner->mark_list->push(owner->mark_list, mark_entry);
35
+ }
23
36
  }
24
37
  }
38
+ if (owner && owner->pool) {
39
+ mongory_memory_pool_unlock(owner->pool);
40
+ }
41
+ RB_GC_GUARD(rb_re);
25
42
  return rb_re;
26
43
  }
27
44
 
@@ -35,7 +52,7 @@ static bool rb_mongory_regex_match_adapter(mongory_memory_pool *pool, mongory_va
35
52
 
36
53
  if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
37
54
  rb_re = (VALUE)pattern->data.regex;
38
- } else if (pattern->type == MONGORY_TYPE_STRING) {
55
+ } else if (pattern->type == MONGORY_TYPE_STRING && pattern->origin && RB_TYPE_P((VALUE)pattern->origin, T_STRING)) {
39
56
  rb_re = rb_mongory_regex_cache(pool, pattern, rb_funcall(rb_cRegexp, rb_intern("new"), 1, (VALUE)pattern->origin));
40
57
  } else {
41
58
  return false;
@@ -54,7 +71,7 @@ static bool rb_mongory_regex_options_match_adapter(mongory_memory_pool *pool, mo
54
71
  if (!pattern || !value || value->type != MONGORY_TYPE_STRING) {
55
72
  return false;
56
73
  }
57
- if (pattern->type == MONGORY_TYPE_STRING) {
74
+ if (pattern->type == MONGORY_TYPE_STRING && pattern->origin && RB_TYPE_P((VALUE)pattern->origin, T_STRING)) {
58
75
  int flags = 0;
59
76
  for (const char *flag = options; *flag; flag++) {
60
77
  if (*flag == 'i') {
@@ -70,13 +87,15 @@ static bool rb_mongory_regex_options_match_adapter(mongory_memory_pool *pool, mo
70
87
  }
71
88
 
72
89
  static char *rb_mongory_regex_stringify_adapter(mongory_memory_pool *pool, mongory_value *pattern) {
73
- (void)pool;
74
- if (pattern->type != MONGORY_TYPE_REGEX) {
90
+ if (!pool || !pattern || pattern->type != MONGORY_TYPE_REGEX || !pattern->data.regex) {
75
91
  return NULL;
76
92
  }
77
93
  VALUE rb_re = (VALUE)pattern->data.regex;
78
94
  VALUE rb_str = rb_funcall(rb_re, rb_intern("inspect"), 0);
79
- return StringValueCStr(rb_str);
95
+ const char *cstr = StringValueCStr(rb_str);
96
+ char *copy = rb_mongory_pool_strdup(pool, cstr);
97
+ RB_GC_GUARD(rb_str);
98
+ return copy;
80
99
  }
81
100
 
82
101
  static mongory_matcher_custom_context *rb_mongory_custom_matcher_build(char *key, mongory_value *condition, void *ctx) {
@@ -102,25 +121,43 @@ static mongory_matcher_custom_context *rb_mongory_custom_matcher_build(char *key
102
121
  return NULL;
103
122
  }
104
123
  mongory_value *matcher_value = mongory_value_wrap_u(pool, NULL);
124
+ if (!matcher_value) {
125
+ return NULL;
126
+ }
105
127
  matcher_value->origin = (void *)matcher;
106
128
  owner->mark_list->push(owner->mark_list, matcher_value);
129
+ RB_GC_GUARD(matcher);
130
+
107
131
  VALUE class_name = rb_funcall(matcher_class, rb_intern("name"), 0);
132
+ const char *raw_name = (!NIL_P(class_name) && RB_TYPE_P(class_name, T_STRING)) ? StringValueCStr(class_name) : key;
133
+ char *copied_name = rb_mongory_pool_strdup(pool, raw_name);
134
+ RB_GC_GUARD(class_name);
135
+
108
136
  mongory_matcher_custom_context *return_ctx = MG_ALLOC_PTR(pool, mongory_matcher_custom_context);
109
137
  if (return_ctx == NULL) {
110
138
  return NULL;
111
139
  }
112
- return_ctx->name = StringValueCStr(class_name);
140
+ return_ctx->name = copied_name ? copied_name : key;
113
141
  return_ctx->external_matcher = (void *)matcher;
114
142
  return return_ctx;
115
143
  }
116
144
 
117
145
  static bool rb_mongory_custom_matcher_match(void *ruby_matcher, mongory_value *value) {
118
146
  VALUE matcher = (VALUE)ruby_matcher;
119
- VALUE match_result = rb_funcall(matcher, rb_intern("match?"), 1, value->origin);
147
+ VALUE arg = Qnil;
148
+ if (value != NULL) {
149
+ if (value->origin) {
150
+ arg = (VALUE)value->origin;
151
+ } else if (value->type == MONGORY_TYPE_INT) {
152
+ arg = LL2NUM(value->data.i);
153
+ }
154
+ }
155
+ VALUE match_result = rb_funcall(matcher, rb_intern("match?"), 1, arg);
120
156
  return RTEST(match_result);
121
157
  }
122
158
 
123
- static bool rb_mongory_custom_matcher_lookup(char *key) {
159
+ static bool rb_mongory_custom_matcher_lookup(char *key, void *extern_ctx) {
160
+ (void)extern_ctx;
124
161
  VALUE matcher_class = rb_funcall(rb_mongory_m_matchers, rb_intern("lookup"), 1, rb_str_new_cstr(key));
125
162
  return RTEST(matcher_class);
126
163
  }
@@ -130,10 +167,6 @@ void rb_mongory_register_adapters(void) {
130
167
  mongory_regex_options_func_set(rb_mongory_regex_options_match_adapter);
131
168
  mongory_regex_stringify_func_set(rb_mongory_regex_stringify_adapter);
132
169
 
133
- mongory_value_converter_deep_convert_set(rb_to_mongory_value_deep);
134
- mongory_value_converter_shallow_convert_set(rb_to_mongory_value_shallow);
135
- mongory_value_converter_recover_set(mongory_value_to_ruby);
136
-
137
170
  mongory_custom_matcher_match_func_set(rb_mongory_custom_matcher_match);
138
171
  mongory_custom_matcher_build_func_set(rb_mongory_custom_matcher_build);
139
172
  mongory_custom_matcher_lookup_func_set(rb_mongory_custom_matcher_lookup);
@@ -8,6 +8,7 @@
8
8
 
9
9
  static void rb_mongory_matcher_mark(void *ptr);
10
10
  static void rb_mongory_matcher_free(void *ptr);
11
+ static size_t rb_mongory_matcher_memsize(const void *ptr);
11
12
  static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv);
12
13
 
13
14
  static const rb_data_type_t rb_mongory_matcher_type = {
@@ -15,21 +16,53 @@ static const rb_data_type_t rb_mongory_matcher_type = {
15
16
  .function = {
16
17
  .dmark = rb_mongory_matcher_mark,
17
18
  .dfree = rb_mongory_matcher_free,
18
- .dsize = NULL,
19
+ .dsize = rb_mongory_matcher_memsize,
19
20
  },
20
21
  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
21
22
  };
22
23
 
24
+ static size_t rb_mongory_matcher_native_bytes(const rb_mongory_matcher_t *wrapper) {
25
+ if (!wrapper) return 0;
26
+ size_t total = sizeof(rb_mongory_matcher_t);
27
+ if (wrapper->pool) {
28
+ total += sizeof(rb_mongory_memory_pool_t) + mongory_memory_pool_total_bytes(wrapper->pool);
29
+ }
30
+ for (rb_mongory_memory_pool_t *s = wrapper->scratch_free_list; s != NULL; s = s->next_scratch) {
31
+ total += sizeof(rb_mongory_memory_pool_t) + mongory_memory_pool_total_bytes(&s->base);
32
+ }
33
+ return total;
34
+ }
35
+
36
+ static size_t rb_mongory_matcher_memsize(const void *ptr) {
37
+ return ptr ? rb_mongory_matcher_native_bytes((const rb_mongory_matcher_t *)ptr) : 0;
38
+ }
39
+
40
+ static inline void rb_mongory_pool_sync_gc_memory(rb_mongory_memory_pool_t *pool) {
41
+ if (!pool) return;
42
+ size_t current = mongory_memory_pool_total_bytes(&pool->base);
43
+ if (current != pool->reported_bytes) {
44
+ if (current > pool->reported_bytes) {
45
+ size_t diff = current - pool->reported_bytes;
46
+ pool->reported_bytes = current;
47
+ rb_gc_adjust_memory_usage((ssize_t)diff);
48
+ } else {
49
+ size_t diff = pool->reported_bytes - current;
50
+ pool->reported_bytes = current;
51
+ rb_gc_adjust_memory_usage(-(ssize_t)diff);
52
+ }
53
+ }
54
+ }
55
+
23
56
  static void rb_mongory_matcher_release_pools(rb_mongory_matcher_t *wrapper) {
24
57
  if (wrapper->pool) {
25
- wrapper->pool->free(wrapper->pool);
58
+ rb_mongory_memory_pool_free((rb_mongory_memory_pool_t *)wrapper->pool);
26
59
  wrapper->pool = NULL;
27
60
  }
28
61
  while (wrapper->scratch_free_list) {
29
62
  rb_mongory_memory_pool_t *scratch = wrapper->scratch_free_list;
30
63
  wrapper->scratch_free_list = scratch->next_scratch;
31
64
  scratch->next_scratch = NULL;
32
- scratch->base.free(&scratch->base);
65
+ rb_mongory_memory_pool_free(scratch);
33
66
  }
34
67
  wrapper->matcher = NULL;
35
68
  wrapper->condition = NULL;
@@ -86,8 +119,8 @@ static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
86
119
  rb_mongory_memory_pool_t *matcher_pool = rb_mongory_memory_pool_new();
87
120
  rb_mongory_memory_pool_t *scratch_pool = rb_mongory_memory_pool_new();
88
121
  if (!matcher_pool || !scratch_pool) {
89
- if (matcher_pool) matcher_pool->base.free(&matcher_pool->base);
90
- if (scratch_pool) scratch_pool->base.free(&scratch_pool->base);
122
+ if (matcher_pool) rb_mongory_memory_pool_free(matcher_pool);
123
+ if (scratch_pool) rb_mongory_memory_pool_free(scratch_pool);
91
124
  rb_raise(rb_mongory_e_error, "Failed to allocate matcher memory pools");
92
125
  }
93
126
  mongory_memory_pool *matcher_pool_base = &matcher_pool->base;
@@ -96,11 +129,15 @@ static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
96
129
  wrapper->string_map = mongory_table_new(matcher_pool_base);
97
130
  wrapper->symbol_map = mongory_table_new(matcher_pool_base);
98
131
  wrapper->mark_list = mongory_array_new(matcher_pool_base);
132
+ if (!wrapper->string_map || !wrapper->symbol_map || !wrapper->mark_list) {
133
+ rb_mongory_matcher_release_pools(wrapper);
134
+ rb_raise(rb_mongory_e_error, "Failed to allocate matcher tables");
135
+ }
99
136
  matcher_pool->owner = wrapper;
100
137
  scratch_pool->owner = wrapper;
101
138
  rb_mongory_matcher_parse_argv(wrapper, argc, argv);
102
139
 
103
- mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, wrapper->ctx);
140
+ mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, (void *)wrapper->ctx);
104
141
  if (matcher_pool_base->error || !matcher) {
105
142
  char err_msg[256];
106
143
  snprintf(err_msg, sizeof(err_msg), "%s",
@@ -112,6 +149,9 @@ static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
112
149
  }
113
150
 
114
151
  wrapper->matcher = matcher;
152
+ rb_mongory_pool_sync_gc_memory(matcher_pool);
153
+ rb_mongory_pool_sync_gc_memory(scratch_pool);
154
+ RB_GC_GUARD(self);
115
155
  return self;
116
156
  }
117
157
 
@@ -143,10 +183,12 @@ static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc,
143
183
  }
144
184
  VALUE converted_condition = rb_funcall(rb_mongory_condition_converter, rb_intern("convert"), 1, condition);
145
185
  self->condition = rb_to_mongory_value_deep(self->pool, converted_condition);
186
+ if (!self->condition) {
187
+ RB_GC_GUARD(converted_condition);
188
+ rb_raise(rb_mongory_e_error, "Failed to convert condition");
189
+ }
146
190
  self->mark_list->push(self->mark_list, self->condition);
147
- mongory_value *store_ctx = mongory_value_wrap_u(self->pool, (void *)self->ctx);
148
- store_ctx->origin = (void *)self->ctx;
149
- self->mark_list->push(self->mark_list, store_ctx);
191
+ RB_GC_GUARD(converted_condition);
150
192
  }
151
193
 
152
194
  typedef struct {
@@ -159,6 +201,7 @@ typedef struct {
159
201
  rb_mongory_matcher_t *wrapper;
160
202
  mongory_memory_pool *scratch_pool;
161
203
  VALUE data;
204
+ VALUE *refs_ptr;
162
205
  } rb_mongory_match_call_t;
163
206
 
164
207
  typedef struct {
@@ -166,6 +209,14 @@ typedef struct {
166
209
  mongory_memory_pool *scratch_pool;
167
210
  } rb_mongory_explain_call_t;
168
211
 
212
+ typedef struct {
213
+ rb_mongory_scratch_lease_t lease;
214
+ rb_mongory_matcher_t *wrapper;
215
+ VALUE data;
216
+ VALUE *refs_ptr;
217
+ bool trace_enabled;
218
+ } rb_mongory_trace_call_t;
219
+
169
220
  // Pops an idle scratch pool from wrapper->scratch_free_list, or allocates a new
170
221
  // reusable pool when re-entered concurrently; release resets and pushes it back.
171
222
  static rb_mongory_scratch_lease_t rb_mongory_scratch_acquire(rb_mongory_matcher_t *wrapper) {
@@ -196,13 +247,16 @@ static VALUE rb_mongory_scratch_release(VALUE arg) {
196
247
  return Qnil;
197
248
  }
198
249
  lease->scratch = NULL;
250
+ scratch->scratch_refs_ptr = NULL;
199
251
  if (lease->wrapper && lease->wrapper->pool == lease->owner_pool) {
200
252
  scratch->base.error = NULL;
201
253
  scratch->base.reset(&scratch->base);
202
254
  scratch->next_scratch = lease->wrapper->scratch_free_list;
203
255
  lease->wrapper->scratch_free_list = scratch;
256
+ rb_mongory_pool_sync_gc_memory(scratch);
257
+ rb_mongory_pool_sync_gc_memory((rb_mongory_memory_pool_t *)lease->wrapper->pool);
204
258
  } else {
205
- scratch->base.free(&scratch->base);
259
+ rb_mongory_memory_pool_free(scratch);
206
260
  }
207
261
  return Qnil;
208
262
  }
@@ -212,11 +266,10 @@ static VALUE rb_mongory_matcher_match_body(VALUE arg) {
212
266
  rb_mongory_matcher_t *self_wrapper = call->wrapper;
213
267
  mongory_matcher *matcher = self_wrapper->matcher;
214
268
  mongory_memory_pool *scratch_pool = call->scratch_pool;
269
+ ((rb_mongory_memory_pool_t *)scratch_pool)->scratch_refs_ptr = call->refs_ptr;
215
270
  mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, call->data);
216
271
 
217
- if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
218
- return Qnil;
219
- }
272
+ rb_mongory_error_handling(scratch_pool, "Match failed");
220
273
 
221
274
  bool result = mongory_matcher_match(matcher, data_value);
222
275
  return result ? Qtrue : Qfalse;
@@ -225,13 +278,19 @@ static VALUE rb_mongory_matcher_match_body(VALUE arg) {
225
278
  // Mongory::Matcher#match?(data)
226
279
  static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
227
280
  rb_mongory_matcher_t *self_wrapper = get_initialized_matcher(self);
281
+ if (self_wrapper->tracing) {
282
+ rb_raise(rb_mongory_e_error, "Cannot call match? re-entrantly while trace_tree is active on the same Matcher");
283
+ }
228
284
  rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(self_wrapper);
285
+ VALUE refs = Qnil;
229
286
  rb_mongory_match_call_t call = {
230
287
  .wrapper = self_wrapper,
231
288
  .scratch_pool = &lease.scratch->base,
232
289
  .data = data,
290
+ .refs_ptr = &refs,
233
291
  };
234
292
  VALUE result = rb_ensure(rb_mongory_matcher_match_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
293
+ RB_GC_GUARD(refs);
235
294
  RB_GC_GUARD(self);
236
295
  return result;
237
296
  }
@@ -368,33 +427,53 @@ static VALUE rb_mongory_matcher_explain(VALUE self) {
368
427
  return Qnil;
369
428
  }
370
429
 
371
- // Mongory::Matcher#trace_tree(data)
372
- static VALUE rb_mongory_matcher_trace_tree(VALUE self, VALUE data) {
373
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
374
- rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
375
- trace_pool->owner = wrapper;
376
- mongory_memory_pool *trace_pool_base = &trace_pool->base;
377
- mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool_base, data);
430
+ static VALUE rb_mongory_matcher_trace_tree_body(VALUE arg) {
431
+ rb_mongory_trace_call_t *call = (rb_mongory_trace_call_t *)arg;
432
+ mongory_memory_pool *trace_pool = &call->lease.scratch->base;
433
+ call->lease.scratch->scratch_refs_ptr = call->refs_ptr;
434
+ mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool, call->data);
435
+ rb_mongory_error_handling(trace_pool, "Trace failed");
378
436
 
379
- if (trace_pool_base->error) {
380
- char err_msg[256];
381
- snprintf(err_msg, sizeof(err_msg), "%s",
382
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
383
- trace_pool_base->free(trace_pool_base);
384
- rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
385
- }
437
+ call->wrapper->tracing = true;
438
+ call->trace_enabled = true;
439
+ mongory_value *tree = mongory_matcher_trace_tree(call->wrapper->matcher, data_value);
440
+ call->trace_enabled = false;
441
+ call->wrapper->tracing = false;
442
+ rb_mongory_error_handling(trace_pool, "Trace failed");
386
443
 
387
- mongory_value *tree = mongory_matcher_trace_tree(wrapper->matcher, data_value);
388
- if (trace_pool_base->error) {
389
- char err_msg[256];
390
- snprintf(err_msg, sizeof(err_msg), "%s",
391
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
392
- trace_pool_base->free(trace_pool_base);
393
- rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
444
+ return rb_mongory_tree_node_to_rb(tree);
445
+ }
446
+
447
+ static VALUE rb_mongory_matcher_trace_tree_ensure(VALUE arg) {
448
+ rb_mongory_trace_call_t *call = (rb_mongory_trace_call_t *)arg;
449
+ if (call->wrapper) {
450
+ if (call->trace_enabled && call->wrapper->matcher) {
451
+ mongory_matcher_disable_trace(call->wrapper->matcher);
452
+ call->trace_enabled = false;
453
+ }
454
+ call->wrapper->tracing = false;
394
455
  }
395
- VALUE result = rb_mongory_tree_node_to_rb(tree);
396
- trace_pool_base->free(trace_pool_base);
456
+ return rb_mongory_scratch_release((VALUE)&call->lease);
457
+ }
397
458
 
459
+ // Mongory::Matcher#trace_tree(data)
460
+ static VALUE rb_mongory_matcher_trace_tree(VALUE self, VALUE data) {
461
+ rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
462
+ if (wrapper->tracing) {
463
+ rb_raise(rb_mongory_e_error, "Cannot call trace_tree re-entrantly on the same Matcher");
464
+ }
465
+ VALUE refs = Qnil;
466
+ rb_mongory_trace_call_t call = {
467
+ .lease = rb_mongory_scratch_acquire(wrapper),
468
+ .wrapper = wrapper,
469
+ .data = data,
470
+ .refs_ptr = &refs,
471
+ .trace_enabled = false,
472
+ };
473
+ VALUE result = rb_ensure(rb_mongory_matcher_trace_tree_body, (VALUE)&call, rb_mongory_matcher_trace_tree_ensure,
474
+ (VALUE)&call);
475
+ RB_GC_GUARD(refs);
476
+ RB_GC_GUARD(self);
398
477
  return result;
399
478
  }
400
479