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 end
20122012
20132013 def type_is_pointer(t)
2014+ if is_nullable_type(t) == 1
2015+ return type_is_pointer(base_type(t))
2016+ end
20142017 if t == "int_array"
20152018 return 1
20162019 end
@@ -2043,6 +2046,41 @@
20432046 0
20442047 end
20452048
2049+ def is_nullable_type(t)
2050+ if t.length > 1 && t[t.length - 1] == "?"
2051+ return 1
2052+ end
2053+ 0
2054+ end
2055+
2056+ def base_type(t)
2057+ if t.length > 1 && t[t.length - 1] == "?"
2058+ return t[0, t.length - 1]
2059+ end
2060+ t
2061+ end
2062+
2063+ def is_nullable_pointer_type(t)
2064+ # Pointer types that can represent nil as NULL
2065+ bt = base_type(t)
2066+ if bt == "string" || bt == "mutable_str"
2067+ return 1
2068+ end
2069+ if bt == "int_array" || bt == "str_array" || bt == "float_array"
2070+ return 1
2071+ end
2072+ if bt == "str_int_hash" || bt == "str_str_hash"
2073+ return 1
2074+ end
2075+ if bt == "stringio" || bt == "lambda" || bt == "poly_array"
2076+ return 1
2077+ end
2078+ if is_obj_type(bt) == 1
2079+ return 1
2080+ end
2081+ 0
2082+ end
2083+
20462084 def is_value_type_obj(t)
20472085 if is_obj_type(t) == 1
20482086 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) == 1
2098+ return c_type(base_type(t))
2099+ end
20592100 if t == "range"
20602101 return "sp_Range"
20612102 end
@@ -2122,6 +2163,9 @@
21222163 end
21232164
21242165 def c_default_val(t)
2166+ if is_nullable_type(t) == 1
2167+ return "NULL"
2168+ end
21252169 if t == "range"
21262170 return "((sp_Range){0,0})"
21272171 end
@@ -5584,9 +5628,20 @@
55845628 end
55855629 if idx >= 0
55865630 if types[idx] != at
5587- if types[idx] != "poly"
5588- types[idx] = "poly"
5589- @needs_rb_value = 1
5631+ old = types[idx]
5632+ if old != "poly"
5633+ if at == "nil" && is_nullable_pointer_type(old) == 1
5634+ # T + nil → T? (nullable)
5635+ if old[old.length - 1] != "?"
5636+ types[idx] = old + "?"
5637+ end
5638+ elsif old == "nil" && is_nullable_pointer_type(at) == 1
5639+ # nil + T → T? (nullable)
5640+ types[idx] = at + "?"
5641+ else
5642+ types[idx] = "poly"
5643+ @needs_rb_value = 1
5644+ end
55905645 end
55915646 end
55925647 else
@@ -8139,6 +8194,12 @@
81398194 if at != "int"
81408195 if types[ki] == "int"
81418196 types[ki] = at
8197+ elsif at == "nil" && is_nullable_pointer_type(types[ki]) == 1
8198+ if types[ki][types[ki].length - 1] != "?"
8199+ types[ki] = types[ki] + "?"
8200+ end
8201+ elsif types[ki] == "nil" && is_nullable_pointer_type(at) == 1
8202+ types[ki] = at + "?"
81428203 else
81438204 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 end
10958+ if is_nullable_type(recv_type) == 1
10959+ return "(" + rc + " == NULL)"
10960+ end
1089710961 if type_is_pointer(recv_type) == 1
1089810962 return "(" + rc + " == NULL)"
1089910963 end
@@ -11627,9 +11691,14 @@
1162711691 end
1162811692 return
1162911693 end
11630- 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) == 1
11696+ emit(" lv_" + lname + " = NULL;")
11697+ else
11698+ val = compile_expr(@nd_expression[nid])
11699+ emit(" lv_" + lname + " = " + val + ";")
11700+ end
11701+ set_var_type(lname, rhs_t)
1163311702 return
1163411703 end
1163511704 if t == "LocalVariableOperatorWriteNode"
@@ -13415,7 +13484,7 @@
1341513484 if at == "float"
1341613485 emit(" printf(\"%g" + bsl_n + "\", " + val + ");")
1341713486 else
13418- 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 else
1342113490 if at == "bool"