carray 3.0.6 → 3.1.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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -22,14 +22,13 @@
22
22
  other producers emit). A record's member names, which Ruby's format
23
23
  cannot carry, are CARecord#memory_view_field_names.
24
24
  - See docs/interop/MemoryViewFormat.md for the format-string contract.
25
- - See devel/DESIGN_MemoryView.md for the design rationale.
25
+ - See guides/devel/18_memory_view_protocol.md for the design.
26
26
  - See docs/interop/MemoryView.md for the user-facing MV API.
27
27
 
28
28
  ---------------------------------------------------------------------------- */
29
29
 
30
30
  #include "carray.h"
31
31
  #include "ca_obj_face.h" /* ca_strip_face for Face producers */
32
- #include "carray_internal.h"
33
32
  #include "ruby/memory_view.h"
34
33
  #include "ruby/st.h"
35
34
  #include "ruby/thread_native.h"
@@ -51,7 +50,6 @@
51
50
 
52
51
  typedef struct {
53
52
  const char *class_name;
54
- const char *const_name;
55
53
  int id; /* -1 until resolved at Init */
56
54
  VALUE klass; /* Qnil until resolved at Init */
57
55
  int strategy; /* one of ca_mv_strategy_t (filled in lookup table) */
@@ -81,23 +79,23 @@ typedef enum {
81
79
  /* Resolved at Init_carray_memory_view. Runtime obj_type ids are stored
82
80
  here so ca_mv_reject_reason_for can name the class it refuses. */
83
81
  static ca_mv_runtime_type_t ca_mv_runtime_types[CA_MV_NUM_RUNTIME_OBJ_TYPES] = {
84
- { "CAFarray", "CA_OBJ_FARRAY", -1, Qnil, /* CA_MV_FARRAY */ 4 },
85
- { "CATranspose", "CA_OBJ_TRANSPOSE", -1, Qnil, /* CA_MV_TRANS */ 5 },
86
- { "CAGrid", "CA_OBJ_GRID", -1, Qnil, /* CA_MV_ATTACH */ 7 },
87
- { "CAShift", "CA_OBJ_SHIFT", -1, Qnil, /* CA_MV_ATTACH */ 7 },
88
- { "CAWindow", "CA_OBJ_WINDOW", -1, Qnil, /* CA_MV_ATTACH */ 7 },
89
- { "CAFake", "CA_OBJ_FAKE", -1, Qnil, /* CA_MV_ATTACH */ 7 },
82
+ { "CAFarray", -1, Qnil, /* CA_MV_FARRAY */ 4 },
83
+ { "CATranspose", -1, Qnil, /* CA_MV_TRANS */ 5 },
84
+ { "CAGrid", -1, Qnil, /* CA_MV_ATTACH */ 7 },
85
+ { "CAShift", -1, Qnil, /* CA_MV_ATTACH */ 7 },
86
+ { "CAWindow", -1, Qnil, /* CA_MV_ATTACH */ 7 },
87
+ { "CAFake", -1, Qnil, /* CA_MV_ATTACH */ 7 },
90
88
  /* CAField is a CAStride subclass: zero-copy strided export via
91
89
  parent->ptr + base_offset, no materialise. */
92
- { "CAField", "CA_OBJ_FIELD", -1, Qnil, /* CA_MV_BLOCK */ 3 },
93
- { "CAReduce", "CA_OBJ_REDUCE", -1, Qnil, /* CA_MV_ATTACH */ 7 },
94
- { "CABitarray", "CA_OBJ_BITARRAY", -1, Qnil, /* CA_MV_REJECT */ 0 },
95
- { "CABitfield", "CA_OBJ_BITFIELD", -1, Qnil, /* CA_MV_REJECT */ 0 },
90
+ { "CAField", -1, Qnil, /* CA_MV_BLOCK */ 3 },
91
+ { "CAReduce", -1, Qnil, /* CA_MV_ATTACH */ 7 },
92
+ { "CABitarray", -1, Qnil, /* CA_MV_REJECT */ 0 },
93
+ { "CABitfield", -1, Qnil, /* CA_MV_REJECT */ 0 },
96
94
  /* CARecord: composite Face wrapping a CA_FIXLEN entity 1:1
97
95
  (ca->ptr aliases parent->ptr). ATTACH strategy
98
96
  is the conservative choice — same as CAFake; storage layout is
99
97
  identical to parent so direct export would also work in principle. */
100
- { "CARecord", "CA_OBJ_RECORD", -1, Qnil, /* CA_MV_ATTACH */ 7 },
98
+ { "CARecord", -1, Qnil, /* CA_MV_ATTACH */ 7 },
101
99
  };
102
100
 
103
101
  /* ---------------- format / data_type mapping ----------------
@@ -204,7 +202,8 @@ ca_mv_record_member_spec (VALUE info, char *out, size_t outlen,
204
202
 
205
203
  The result is frozen and cached on the data_class. Only the main Ractor
206
204
  may set an instance variable on a class, so another Ractor builds the
207
- format each time until the main one has cached it. It is an ordinary
205
+ format each time until the main one has cached it; so does every Ractor
206
+ for a frozen data_class, which cannot take the instance variable. It is an ordinary
208
207
  String that compaction can move, so a view takes a copy of its bytes
209
208
  rather than its pointer (ca_mv_get). */
210
209
  static VALUE
@@ -247,11 +246,15 @@ ca_mv_struct_format_for_data_class (VALUE data_class)
247
246
  }
248
247
 
249
248
  rb_obj_freeze(buf);
250
- if (ca_in_main_ractor()) rb_ivar_set(data_class, iv, buf);
249
+ if ( ca_in_main_ractor() && ! OBJ_FROZEN(data_class) ) {
250
+ rb_ivar_set(data_class, iv, buf);
251
+ }
251
252
  return buf;
252
253
 
253
254
  unsupported:
254
- if (ca_in_main_ractor()) rb_ivar_set(data_class, iv, Qfalse);
255
+ if ( ca_in_main_ractor() && ! OBJ_FROZEN(data_class) ) {
256
+ rb_ivar_set(data_class, iv, Qfalse);
257
+ }
255
258
  return Qnil;
256
259
  }
257
260
 
@@ -449,6 +452,22 @@ ca_mv_probe_data_type (VALUE obj)
449
452
  return data_type;
450
453
  }
451
454
 
455
+ /* True when format is a struct of several members that exactly describes an
456
+ item of item_size bytes. A consumer takes such items as raw bytes: the
457
+ format names no members to rebuild a record from. */
458
+ static bool
459
+ ca_mv_format_is_struct (const char *format, ssize_t item_size)
460
+ {
461
+ rb_memory_view_item_component_t *members = NULL;
462
+ size_t n_members = 0;
463
+ const char *err = NULL;
464
+ ssize_t size;
465
+ if ( format == NULL ) return false;
466
+ size = rb_memory_view_parse_item_format(format, &members, &n_members, &err);
467
+ if ( members != NULL ) xfree(members);
468
+ return size == item_size && n_members >= 2;
469
+ }
470
+
452
471
  /* Per-element byte width for consumer-side wrap/from CArray allocation.
453
472
  For numeric types it is `ca_sizeof[data_type]`; for CA_FIXLEN it is the
454
473
  MemoryView item_size, since fixlen has no compile-time element width. */
@@ -462,12 +481,12 @@ ca_mv_carray_bytes_for (int8_t data_type, ssize_t item_size)
462
481
  /* ---------------- helpers ---------------- */
463
482
 
464
483
  static int
465
- ca_mv_extract (VALUE obj, CArray **out)
484
+ ca_mv_extract (volatile VALUE obj, CArray **out)
466
485
  {
467
486
  if (! rb_typeddata_is_kind_of(obj, &carray_data_type)) {
468
487
  return 0;
469
488
  }
470
- *out = (CArray *) DATA_PTR(obj);
489
+ GetCArray(obj, *out);
471
490
  return 1;
472
491
  }
473
492
 
@@ -511,7 +530,7 @@ ca_mv_alloc_strides_contiguous (CArray *ca, ssize_t *shape, bool row_major)
511
530
 
512
531
  typedef struct {
513
532
  int depth; /* 0 = the view itself, 1 = parent, 2 = grand-, ... */
514
- const char *class_name; /* class name of the offending link */
533
+ VALUE klass; /* class of the offending link (nil: unknown) */
515
534
  const char *reason; /* short explanation */
516
535
  int face; /* refused because it is a Face: a copy is
517
536
  refused too, so no copy is suggested */
@@ -528,11 +547,10 @@ ca_mv_is_castride_family (CArray *ca)
528
547
  return ca_func[ca->obj_type].attach == ca_stride_func.attach;
529
548
  }
530
549
 
531
- static const char *
532
- ca_mv_class_name_of (CArray *ca)
550
+ static VALUE
551
+ ca_mv_class_of (CArray *ca)
533
552
  {
534
- VALUE klass = ca_class[ca->obj_type];
535
- return NIL_P(klass) ? "(unknown)" : rb_class2name(klass);
553
+ return ca_class[ca->obj_type];
536
554
  }
537
555
 
538
556
  static const char *
@@ -597,7 +615,7 @@ ca_mv_compose_to_entity (CArray *ca, ca_mv_reject_t *rej,
597
615
  if (ca_is_face(ca)) {
598
616
  if (! ca_face_memory_view_exportable(ca)) {
599
617
  rej->depth = 0;
600
- rej->class_name = ca_mv_class_name_of(ca);
618
+ rej->klass = ca_mv_class_of(ca);
601
619
  rej->reason = "a Face whose storage bytes do not carry its values "
602
620
  "without the Face; export its storage instead "
603
621
  "(e.g. .ticks, .codes)";
@@ -609,7 +627,7 @@ ca_mv_compose_to_entity (CArray *ca, ca_mv_reject_t *rej,
609
627
  if (ca_is_entity(ca)) {
610
628
  if (! ca_mv_is_core_entity(ca)) {
611
629
  rej->depth = 0;
612
- rej->class_name = ca_mv_class_name_of(ca);
630
+ rej->klass = ca_mv_class_of(ca);
613
631
  rej->reason = "installed outside the core; its bytes are not "
614
632
  "exported";
615
633
  return false;
@@ -620,7 +638,7 @@ ca_mv_compose_to_entity (CArray *ca, ca_mv_reject_t *rej,
620
638
  }
621
639
  if (!ca_mv_is_castride_family(ca)) {
622
640
  rej->depth = 0;
623
- rej->class_name = ca_mv_class_name_of(ca);
641
+ rej->klass = ca_mv_class_of(ca);
624
642
  rej->reason = ca_mv_reject_reason_for(ca);
625
643
  return false;
626
644
  }
@@ -628,7 +646,7 @@ ca_mv_compose_to_entity (CArray *ca, ca_mv_reject_t *rej,
628
646
  ca_stride_compose_to_root((CAStride *) ca, &root, out_strides, out_base);
629
647
  if (!ca_is_entity(root)) {
630
648
  rej->depth = -1;
631
- rej->class_name = ca_mv_class_name_of(root);
649
+ rej->klass = ca_mv_class_of(root);
632
650
  if (ca_mv_is_castride_family(root)) {
633
651
  rej->reason = "CAStride chain stride pattern does not compose "
634
652
  "linearly into entity (e.g. reshape across non-contig)";
@@ -639,7 +657,7 @@ ca_mv_compose_to_entity (CArray *ca, ca_mv_reject_t *rej,
639
657
  }
640
658
  if (! ca_mv_is_core_entity(root)) {
641
659
  rej->depth = -1;
642
- rej->class_name = ca_mv_class_name_of(root);
660
+ rej->klass = ca_mv_class_of(root);
643
661
  rej->reason = "installed outside the core; its bytes are not exported";
644
662
  return false;
645
663
  }
@@ -704,15 +722,17 @@ ca_mv_fixlen_format (ca_size_t bytes)
704
722
  {
705
723
  st_data_t found;
706
724
  char *fmt;
707
- rb_nativethread_lock_lock(&ca_mv_fixlen_formats_lock);
725
+ ca_native_lock_without_gvl(&ca_mv_fixlen_formats_lock);
708
726
  if ( st_lookup(ca_mv_fixlen_formats, (st_data_t) bytes, &found) ) {
709
727
  rb_nativethread_lock_unlock(&ca_mv_fixlen_formats_lock);
710
728
  return (const char *) found;
711
729
  }
712
730
  rb_nativethread_lock_unlock(&ca_mv_fixlen_formats_lock);
731
+ /* buffer: kept by the cache; freed here only when another thread stored it first */
713
732
  fmt = ALLOC_N(char, 24);
714
733
  snprintf(fmt, 24, "%llds", (long long) bytes);
715
- rb_nativethread_lock_lock(&ca_mv_fixlen_formats_lock);
734
+ /* lock: st_insert can run a GC, which does not take this lock */
735
+ ca_native_lock_without_gvl(&ca_mv_fixlen_formats_lock);
716
736
  if ( st_lookup(ca_mv_fixlen_formats, (st_data_t) bytes, &found) ) {
717
737
  xfree(fmt);
718
738
  fmt = (char *) found;
@@ -781,12 +801,12 @@ ca_mv_get (VALUE obj, rb_memory_view_t *view, int flags)
781
801
  private_data, freed by ca_mv_release. */
782
802
  if (ca->data_type == CA_FIXLEN && RTEST(rb_ca_has_data_class(obj))) {
783
803
  VALUE klass = rb_ca_data_class(obj);
784
- VALUE fmt = ca_mv_struct_format_for_data_class(klass);
804
+ volatile VALUE fmt = ca_mv_struct_format_for_data_class(klass);
785
805
  long len = RSTRING_LEN(fmt);
806
+ /* buffer: kept by the view; freed here only when the export fails */
786
807
  record_format = ALLOC_N(char, len + 1);
787
808
  memcpy(record_format, RSTRING_PTR(fmt), len);
788
809
  record_format[len] = '\0';
789
- RB_GC_GUARD(fmt);
790
810
  format = record_format;
791
811
  }
792
812
  else if (ca->data_type == CA_FIXLEN) {
@@ -824,6 +844,7 @@ ca_mv_get (VALUE obj, rb_memory_view_t *view, int flags)
824
844
  BUG). */
825
845
  int8_t k;
826
846
  shape = ca_mv_alloc_shape(ca);
847
+ /* buffer: kept by the view; freed here only when the export fails */
827
848
  strides = (ssize_t *) xmalloc(sizeof(ssize_t) * ca->ndim);
828
849
  for (k = 0; k < ca->ndim; k++) strides[k] = (ssize_t) composed_strides[k];
829
850
  data_ptr = resolved_entity->ptr + composed_base;
@@ -1045,7 +1066,7 @@ ca_mv_holder_read_geometry (ca_mv_imported_holder_t *h, ssize_t item_size,
1045
1066
 
1046
1067
  /* rank-check: a producer's rank, named by which MemoryView it is */
1047
1068
  if ( v->ndim < 0 || v->ndim > CA_RANK_MAX ) {
1048
- rb_raise(rb_eArgError, "%sMemoryView ndim (%ld) exceeds CArray CA_RANK_MAX (%d)",
1069
+ rb_raise(rb_eArgError, "%sMemoryView ndim (%ld) exceeds CArray::RANK_MAX (%d)",
1049
1070
  who, (long) v->ndim, CA_RANK_MAX);
1050
1071
  }
1051
1072
  if ( v->ndim == 0 ) {
@@ -1140,8 +1161,7 @@ ca_mv_holder_take_data (ca_mv_imported_holder_t *h, VALUE src,
1140
1161
  if ( data_type < 0 ) {
1141
1162
  rb_raise(rb_eArgError,
1142
1163
  "typeless MemoryView (format=NULL, %ld bytes); "
1143
- "specify the target data_type via data_type: kwarg or use a "
1144
- "concrete CArray::<Dtype> factory class",
1164
+ "give its data_type with data_type:",
1145
1165
  (long) v->byte_size);
1146
1166
  }
1147
1167
  if ( ca_mv_format_for(data_type) == NULL ) {
@@ -1171,6 +1191,9 @@ ca_mv_holder_take_data (ca_mv_imported_holder_t *h, VALUE src,
1171
1191
  }
1172
1192
 
1173
1193
  data_type = ca_mv_data_type_from_format(v->format, v->item_size);
1194
+ if ( data_type < 0 && ca_mv_format_is_struct(v->format, v->item_size) ) {
1195
+ data_type = CA_FIXLEN;
1196
+ }
1174
1197
  if ( data_type < 0 ) {
1175
1198
  rb_raise(rb_eArgError, "unsupported MemoryView format: %s (item_size=%ld)",
1176
1199
  v->format, (long) v->item_size);
@@ -1287,29 +1310,24 @@ ca_mv_check_bits (const ca_mv_imported_holder_t *h, const char *who)
1287
1310
 
1288
1311
  /* Extract the optional data_type: and mask: keywords from argv.
1289
1312
  Returns the resolved data_type (-1 if not specified) and fills
1290
- *out_obj / *out_mask. data_type: accepts symbols (:int32), integer
1291
- constants (CA_INT32), and data_type marker classes (CArray::Int32
1292
- etc.); marker classes are resolved via their `DataType` constant (set
1293
- up by lib/carray/construct.rb), since rb_ca_guess_type does not
1294
- natively recognise them. */
1313
+ *out_obj / *out_mask. data_type: accepts what rb_ca_guess_type
1314
+ accepts: a name (:int32, "int32") or a data type constant
1315
+ (CA_INT32). */
1295
1316
  static int8_t
1296
1317
  ca_mv_extract_type_and_mask_kwargs (int argc, VALUE *argv,
1297
1318
  VALUE *out_obj, VALUE *out_mask)
1298
1319
  {
1299
- volatile VALUE robj = Qnil, ropt = Qnil, rtype = Qnil, rmask = Qnil;
1320
+ VALUE robj = Qnil, ropt = Qnil, rtype = Qnil, rmask = Qnil;
1300
1321
  int8_t data_type = -1;
1301
1322
  ca_size_t bytes;
1302
- rb_scan_args(argc, argv, "1:", (VALUE *) &robj, (VALUE *) &ropt);
1323
+ rb_scan_args(argc, argv, "1:", &robj, &ropt);
1303
1324
  if (! NIL_P(ropt)) {
1304
1325
  rb_scan_options(ropt, "data_type,mask", &rtype, &rmask);
1305
1326
  }
1306
1327
  *out_obj = robj;
1307
1328
  *out_mask = rmask;
1308
1329
  if (! NIL_P(rtype)) {
1309
- if (RB_TYPE_P(rtype, T_CLASS) &&
1310
- rb_const_defined(rtype, rb_intern("DataType"))) {
1311
- rtype = rb_const_get(rtype, rb_intern("DataType"));
1312
- }
1330
+ ca_refuse_type_number(rtype);
1313
1331
  rb_ca_guess_type_and_bytes(rtype, INT2NUM(0), &data_type, &bytes);
1314
1332
  }
1315
1333
  return data_type;
@@ -1344,10 +1362,10 @@ ca_mv_wrap_body (VALUE arg)
1344
1362
  for a class would get something that is not it. */
1345
1363
  if ( a->target_class != rb_cCAWrap && ! d->contiguous ) {
1346
1364
  rb_raise(rb_eArgError,
1347
- "%s was requested, but this producer is strided, so the result "
1365
+ "%"PRIsVALUE" was requested, but this producer is strided, so the result "
1348
1366
  "would be a CAStride; pass a row-major contiguous producer, or "
1349
1367
  "use CArray.from_memory_view for a copy",
1350
- rb_class2name(a->target_class));
1368
+ a->target_class);
1351
1369
  }
1352
1370
 
1353
1371
  if ( m != NULL ) {
@@ -1473,8 +1491,6 @@ ca_mv_wrap (VALUE obj, VALUE mask_obj, VALUE target_class,
1473
1491
  ca_mv_holder_release(a.data);
1474
1492
  rb_jump_tag(tag);
1475
1493
  }
1476
- RB_GC_GUARD(a.data_holder_obj);
1477
- RB_GC_GUARD(a.mask_holder_obj);
1478
1494
  return result;
1479
1495
  }
1480
1496
 
@@ -1498,13 +1514,13 @@ rb_ca_s_wrap_memory_view (int argc, VALUE *argv, VALUE klass)
1498
1514
  VALUE obj, mask_obj, target_class;
1499
1515
  int8_t explicit_data_type =
1500
1516
  ca_mv_extract_type_and_mask_kwargs(argc, argv,
1501
- (VALUE *) &obj, (VALUE *) &mask_obj);
1517
+ &obj, &mask_obj);
1502
1518
 
1503
1519
  target_class = ( klass == rb_cCArray ) ? rb_cCAWrap : klass;
1504
1520
  if ( ! RTEST(rb_class_inherited_p(target_class, rb_cCAWrap)) ) {
1505
1521
  rb_raise(rb_eTypeError,
1506
- "wrap_memory_view builds a CAWrap; %s is not a subclass of it",
1507
- rb_class2name(target_class));
1522
+ "wrap_memory_view builds a CAWrap; %"PRIsVALUE" is not a subclass of it",
1523
+ target_class);
1508
1524
  }
1509
1525
  return ca_mv_wrap(obj, mask_obj, target_class, explicit_data_type, -1, 0);
1510
1526
  }
@@ -1580,19 +1596,17 @@ rb_ca_s_from_memory_view (int argc, VALUE *argv, VALUE klass)
1580
1596
  volatile VALUE result;
1581
1597
  int8_t explicit_data_type =
1582
1598
  ca_mv_extract_type_and_mask_kwargs(argc, argv,
1583
- (VALUE *) &obj, (VALUE *) &mask_obj);
1599
+ &obj, &mask_obj);
1584
1600
 
1585
1601
  if ( klass != rb_cCArray &&
1586
1602
  ! RTEST(rb_class_inherited_p(klass, rb_cCAWrap)) ) {
1587
1603
  rb_raise(rb_eTypeError,
1588
- "from_memory_view builds a CArray; %s cannot be returned from it",
1589
- rb_class2name(klass));
1604
+ "from_memory_view builds a CArray; %"PRIsVALUE" cannot be returned from it",
1605
+ klass);
1590
1606
  }
1591
1607
  ca_mv_import_args_init(&a, obj, mask_obj);
1592
1608
  a.explicit_data_type = explicit_data_type;
1593
1609
  result = rb_ensure(ca_mv_from_body, (VALUE) &a, ca_mv_from_release, (VALUE) &a);
1594
- RB_GC_GUARD(a.data_holder_obj);
1595
- RB_GC_GUARD(a.mask_holder_obj);
1596
1610
  return result;
1597
1611
  }
1598
1612
 
@@ -1642,12 +1656,12 @@ rb_ca_s_memory_view_reject_reason (VALUE klass, VALUE obj)
1642
1656
  else if (rej.depth == 1) where = "parent";
1643
1657
  else where = NULL; /* ancestor[N] */
1644
1658
  if (where) {
1645
- return rb_sprintf("%s is %s (%s); zero-copy wrap not possible.%s",
1646
- where, rej.class_name, rej.reason,
1659
+ return rb_sprintf("%s is %"PRIsVALUE" (%s); zero-copy wrap not possible.%s",
1660
+ where, NIL_P(rej.klass) ? rb_str_new_cstr("(unknown)") : rej.klass, rej.reason,
1647
1661
  rej.face ? "" : " Export arr.copy for a snapshot.");
1648
1662
  } else {
1649
- return rb_sprintf("ancestor[%d] is %s (%s); zero-copy wrap not possible.%s",
1650
- rej.depth - 1, rej.class_name, rej.reason,
1663
+ return rb_sprintf("ancestor[%d] is %"PRIsVALUE" (%s); zero-copy wrap not possible.%s",
1664
+ rej.depth - 1, NIL_P(rej.klass) ? rb_str_new_cstr("(unknown)") : rej.klass, rej.reason,
1651
1665
  rej.face ? "" : " Export arr.copy for a snapshot.");
1652
1666
  }
1653
1667
  }
@@ -1659,7 +1673,7 @@ rb_ca_s_memory_view_reject_reason (VALUE klass, VALUE obj)
1659
1673
  a producer that emits every synonym. Returns the CArray data_type
1660
1674
  integer on accept, or nil on reject. */
1661
1675
  static VALUE
1662
- rb_ca_s_parse_memory_view_format (VALUE klass, VALUE fmt_v, VALUE item_size_v)
1676
+ rb_ca_s_parse_memory_view_format (VALUE klass, volatile VALUE fmt_v, VALUE item_size_v)
1663
1677
  {
1664
1678
  const char *fmt = NIL_P(fmt_v) ? NULL : StringValueCStr(fmt_v);
1665
1679
  ssize_t item_size = (ssize_t) NUM2LL(item_size_v);
@@ -1732,19 +1746,19 @@ Init_carray_memory_view (void)
1732
1746
  rb_memory_view_register(rb_cCASelect, &ca_memory_view_entry);
1733
1747
  rb_memory_view_register(rb_cCARepeat, &ca_memory_view_entry);
1734
1748
 
1735
- /* Runtime-assigned classes: resolve via Ruby constants. */
1749
+ /* Runtime-assigned classes: resolve by class name. */
1736
1750
  for (int i = 0; i < CA_MV_NUM_RUNTIME_OBJ_TYPES; i++) {
1737
1751
  ca_mv_runtime_type_t *rt = &ca_mv_runtime_types[i];
1738
1752
  ID class_id = rb_intern(rt->class_name);
1739
- ID const_id = rb_intern(rt->const_name);
1740
1753
  if (rb_const_defined(rb_cObject, class_id)) {
1741
1754
  rt->klass = rb_const_get(rb_cObject, class_id);
1742
1755
  if (rt->strategy != CA_MV_REJECT) {
1743
1756
  rb_memory_view_register(rt->klass, &ca_memory_view_entry);
1744
1757
  }
1745
- }
1746
- if (rb_const_defined(rb_cObject, const_id)) {
1747
- rt->id = NUM2INT(rb_const_get(rb_cObject, const_id));
1758
+ /* The obj_type installed for this class. */
1759
+ for (int t = 0; t < CA_OBJ_TYPE_MAX; t++) {
1760
+ if (ca_class[t] == rt->klass) { rt->id = t; break; }
1761
+ }
1748
1762
  }
1749
1763
  }
1750
1764