Commit 4318a52c
4318a52c401eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: nullable types (T|nil) avoid full poly overhead
When a variable is assigned both a pointer type and nil, use nullable type (e.g., string?) instead of poly (sp_RbVal tagged union). Pointer types naturally represent nil as NULL, so no boxing/unboxing is needed.
Supported: string?, obj_X?, int_array?, str_array?, and other pointer types. Non-pointer types (int?, float?) fall back to poly.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 4318a52c401e
- Parent
- cdc6bbf0e5d2
spinel_codegen.rbmodifiedView file
@@ -2011,6 +2011,9 @@
20112011
end20122012
20132013
def type_is_pointer(t)2014+
if is_nullable_type(t) == 12015+
return type_is_pointer(base_type(t))2016+
end20142017
if t == "int_array"20152018
return 120162019
end@@ -2043,6 +2046,41 @@
20432046
020442047
end20452048
2049+
def is_nullable_type(t)2050+
if t.length > 1 && t[t.length - 1] == "?"2051+
return 12052+
end2053+
02054+
end2055+
2056+
def base_type(t)2057+
if t.length > 1 && t[t.length - 1] == "?"2058+
return t[0, t.length - 1]2059+
end2060+
t2061+
end2062+
2063+
def is_nullable_pointer_type(t)2064+
# Pointer types that can represent nil as NULL2065+
bt = base_type(t)2066+
if bt == "string" || bt == "mutable_str"2067+
return 12068+
end2069+
if bt == "int_array" || bt == "str_array" || bt == "float_array"2070+
return 12071+
end2072+
if bt == "str_int_hash" || bt == "str_str_hash"2073+
return 12074+
end2075+
if bt == "stringio" || bt == "lambda" || bt == "poly_array"2076+
return 12077+
end2078+
if is_obj_type(bt) == 12079+
return 12080+
end2081+
02082+
end2083+
20462084
def is_value_type_obj(t)20472085
if is_obj_type(t) == 120482086
cname = t[4, t.length - 4]@@ -2056,6 +2094,9 @@
20562094
20572095
# ---- C type mapping ----20582096
def c_type(t)2097+
if is_nullable_type(t) == 12098+
return c_type(base_type(t))2099+
end20592100
if t == "range"20602101
return "sp_Range"20612102
end@@ -2122,6 +2163,9 @@
21222163
end21232164
21242165
def c_default_val(t)2166+
if is_nullable_type(t) == 12167+
return "NULL"2168+
end21252169
if t == "range"21262170
return "((sp_Range){0,0})"21272171
end@@ -5584,9 +5628,20 @@
55845628
end55855629
if idx >= 055865630
if types[idx] != at5587-
if types[idx] != "poly"5588-
types[idx] = "poly"5589-
@needs_rb_value = 15631+
old = types[idx]5632+
if old != "poly"5633+
if at == "nil" && is_nullable_pointer_type(old) == 15634+
# T + nil → T? (nullable)5635+
if old[old.length - 1] != "?"5636+
types[idx] = old + "?"5637+
end5638+
elsif old == "nil" && is_nullable_pointer_type(at) == 15639+
# nil + T → T? (nullable)5640+
types[idx] = at + "?"5641+
else5642+
types[idx] = "poly"5643+
@needs_rb_value = 15644+
end55905645
end55915646
end55925647
else@@ -8139,6 +8194,12 @@
81398194
if at != "int"81408195
if types[ki] == "int"81418196
types[ki] = at8197+
elsif at == "nil" && is_nullable_pointer_type(types[ki]) == 18198+
if types[ki][types[ki].length - 1] != "?"8199+
types[ki] = types[ki] + "?"8200+
end8201+
elsif types[ki] == "nil" && is_nullable_pointer_type(at) == 18202+
types[ki] = at + "?"81428203
else81438204
types[ki] = "poly"81448205
@needs_rb_value = 1@@ -10894,6 +10955,9 @@
1089410955
if recv_type == "poly"1089510956
return "sp_poly_nil_p(" + rc + ")"1089610957
end10958+
if is_nullable_type(recv_type) == 110959+
return "(" + rc + " == NULL)"10960+
end1089710961
if type_is_pointer(recv_type) == 11089810962
return "(" + rc + " == NULL)"1089910963
end@@ -11627,9 +11691,14 @@
1162711691
end1162811692
return1162911693
end11630-
val = compile_expr(@nd_expression[nid])11631-
emit(" lv_" + lname + " = " + val + ";")11632-
set_var_type(lname, infer_type(@nd_expression[nid]))11694+
rhs_t = infer_type(@nd_expression[nid])11695+
if rhs_t == "nil" && is_nullable_type(vt) == 111696+
emit(" lv_" + lname + " = NULL;")11697+
else11698+
val = compile_expr(@nd_expression[nid])11699+
emit(" lv_" + lname + " = " + val + ";")11700+
end11701+
set_var_type(lname, rhs_t)1163311702
return1163411703
end1163511704
if t == "LocalVariableOperatorWriteNode"@@ -13415,7 +13484,7 @@
1341513484
if at == "float"1341613485
emit(" printf(\"%g" + bsl_n + "\", " + val + ");")1341713486
else13418-
if at == "string"13487+
if at == "string" || at == "string?"1341913488
emit(" { const char *_ps = " + val + "; if (_ps) { fputs(_ps, stdout); if (!*_ps || _ps[strlen(_ps)-1] != '" + bsl_n + "') putchar('" + bsl_n + "'); } else putchar('" + bsl_n + "'); }")1342013489
else1342113490
if at == "bool"