Commit 183a8010

183a80101d37Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): Array#tally for sym_array

str_array#tally already shipped (sp_StrArray_tally lives next to sp_StrArray in sp_runtime.h). Add the symbol-array variant via a new sp_SymArray_tally helper that emits a sym_int_hash mapping each unique symbol element to its occurrence count.

The helper is emitted alongside sp_SymIntHash (in emit_sym_int_hash_runtime, gated on @needs_sym_int_hash) rather than in lib/sp_runtime.h, because sp_SymIntHash is one of the on-demand-emitted hash variants, not a baseline runtime type. sym_array storage is sp_IntArray under the covers, so the helper takes sp_IntArray * and casts each element to sp_sym for the hash key:

static sp_SymIntHash *sp_SymArray_tally(sp_IntArray *a) {
  sp_SymIntHash *h = sp_SymIntHash_new();
  for (mrb_int i = 0; i < a->len; i++) {
    sp_sym k = (sp_sym)a->data[a->start + i];
    mrb_int c = sp_SymIntHash_has_key(h, k) ? sp_SymIntHash_get(h, k) : 0;
    sp_SymIntHash_set(h, k, c + 1);
  }
  return h;
}

compile_call_expr's int_array/sym_array dispatch arm now branches on recv_type — only sym_array routes to sp_SymArray_tally; int_array falls through (would need a new int_int_hash variant which doesn't exist yet). The "tally" return-type rule in infer_method_name_type likewise branches: sym_array receivers infer sym_int_hash, others keep the existing str_int_hash default.

scan_features' new tally arm sets @needs_sym_int_hash when the receiver is sym_array, so emit_sym_int_hash_runtime fires and the typedef + sp_SymArray_tally helper land in the generated C ahead of any call site.

Test exercises:

  • repeated symbols across a 6-element array, asserting count for each distinct key
  • single-element tally
  • all-same-element tally (count == array length)
  • tally.length matches the unique-element count
  • has_key? confirms presence and absence

Out of scope: int_array#tally, float_array#tally — both would need new int_int_hash / float_int_hash variants. Listed as the same family but deferred here.

Bootstrap: gen2.c == gen3.c. Tests: 212 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
183a80101d37
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -2584,6 +2584,9 @@
25842584 return "int"
25852585 end
25862586 if mname == "tally"
2587+ if recv >= 0 && infer_type(recv) == "sym_array"
2588+ return "sym_int_hash"
2589+ end
25872590 return "str_int_hash"
25882591 end
25892592 if mname == "values"
@@ -8392,6 +8395,14 @@
83928395 end
83938396 end
83948397 end
8398+ # `sym_array.tally` returns a sym_int_hash. The runtime helper
8399+ # sp_SymArray_tally lives next to the sp_SymIntHash typedef which
8400+ # is gated on @needs_sym_int_hash, so flag the dependency.
8401+ if mname == "tally"
8402+ if @nd_receiver[nid] >= 0 && infer_type(@nd_receiver[nid]) == "sym_array"
8403+ @needs_sym_int_hash = 1
8404+ end
8405+ end
83958406 # Methods that need string helpers only when receiver is string
83968407 if mname == "+" || mname == "*" || mname == "reverse"
83978408 if @nd_receiver[nid] >= 0
@@ -9960,6 +9971,13 @@
99609971 emit_raw("static mrb_bool sp_SymIntHash_has_key(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")
99619972 emit_raw("static mrb_int sp_SymIntHash_length(sp_SymIntHash*h){return h->len;}")
99629973 emit_raw("static void sp_SymIntHash_delete(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")
9974+ # sym_array.tally — emitted alongside sp_SymIntHash because the
9975+ # helper depends on the typedef. sym_array storage is sp_IntArray
9976+ # (sym ids stored as mrb_int); cast each element to sp_sym for the
9977+ # hash key. sp_SymIntHash_get returns 0 for missing keys (line 10176)
9978+ # so the has_key? guard is redundant — drop it for two lookups per
9979+ # element instead of three.
9980+ emit_raw("static sp_SymIntHash*sp_SymArray_tally(sp_IntArray*a){sp_SymIntHash*h=sp_SymIntHash_new();for(mrb_int i=0;i<a->len;i++){sp_sym k=(sp_sym)a->data[a->start+i];sp_SymIntHash_set(h,k,sp_SymIntHash_get(h,k)+1);}return h;}")
99639981 emit_raw("")
99649982 end
99659983
@@ -17040,6 +17058,12 @@
1704017058 end
1704117059 return "sp_IntArray_sort(" + rc + ")"
1704217060 end
17061+ # tally: sym_array only — int_array would need an int_int_hash
17062+ # variant which doesn't exist yet. Result is sym_int_hash.
17063+ if mname == "tally" && recv_type == "sym_array"
17064+ @needs_sym_int_hash = 1
17065+ return "sp_SymArray_tally(" + rc + ")"
17066+ end
1704317067 if mname == "first"
1704417068 return "sp_IntArray_get(" + rc + ", 0)"
1704517069 end