SCUA 0.40.0 compiles more of the code real programs are made of. The
small helper functions that a game simulation or a file decoder calls
millions of times (a bounds test or a bit reader, say) now compile
whole, where before they ran in the slower first tier. On the programs
we measured, a game simulation runs 2.7 times as fast as on 0.39.0, and
a 12-megapixel progressive JPEG decodes in about 40% less time. Typed
code also gets easier to write: after you test an optional value for
nil, you can use it as its plain type. And a new guide in
the manual shows how to find where your own program spends its time.
#Real programs, measured
These programs were the yardstick for this release: a game simulation with many players, written with records, enums and typed functions, and the JPEG decoder in the @scua/image package, a pure-SCUA codec that is all bit reading and array work. CPU seconds on an Apple M4 Max, the same input, byte-identical output:
| program | 0.39.0 | 0.40.0 |
|---|---|---|
| game simulation, 500 players | 377 s | 138 s |
| JPEG decode, 12 MP, progressive, quality 100 | 5.13 s | 3.05 s |
| JPEG decode, quality 90, 4:2:0 | 1.01 s | 0.80 s |
Most of that comes from functions that now compile whole and from record checks that now take one step.
#Small functions now compile whole
SCUA's JIT has two tiers. The first compiles every hot function quickly. The second, the optimizing tier, compiles a function as a whole, keeps its values in registers and drops the checks it can prove unnecessary. Until now, many small functions never reached the second tier because they used one operation it didn't handle. In 0.40.0 these all compile in the optimizing tier:
- reading an array, a typed array such as
{ u16 }, or abytesvalue by index; - updating a field of the record a function was given, and reading a
boolfield; - returning nothing, returning
nil, or returning a record; %and//, bitwise operations and shifts;x == nilandx != nil;- reading a field of a module or a top-level constant, even one with many fields;
- calling a method on a record (
p.len2()); - a loop that leaves through
breakorreturnfrom inside anif, and a loop that makes several calls.
A function that holds more values at once than the processor has registers now compiles on x86-64 too, when it makes no calls. That was the one hot function in the JPEG decoder that compiled on Apple silicon and not on Intel and AMD machines.
None of this changes what a program does. The examples and the manual's code are run with the JIT on and off, and must print the same thing either way.
#Records with enums check in one step
A typed function checks each record it is given. SCUA already
remembered the layout of a record it had checked, so the next check is a
single comparison. That shortcut used to stop at a record with an enum
field, a tagged enum or an array field, and such records were checked
field by field on every call. Game state is full of exactly those
records: a player with a class: Class, an inventory with
items: { Item }. They now take the shortcut too, and on its
own that made the game simulation about twice as fast.
#Fewer nil workarounds in typed code
After you test an optional value for nil, the checker
now treats it as its plain type for as long as the test holds:
record Item { name: string, price: int, discount: int? }
fn price_of(it: Item) -> int
if it.discount == nil then return it.price end
return it.price - it.discount
end
print(price_of({ name = "lamp", price = 100, discount = 15 }), price_of({ name = "rug", price = 80 }))
$ scua prices.scua
85 80
0.39.0 refused the subtraction (cannot subtract a int?),
and the usual way round it was
it.price - (it.discount or 0). The test narrows inside
if x != nil then … end, after
if x == nil then return … end, in the else of
if x == nil, in the body of while x != nil,
and through and, or, not and
elseif. It works for a name and for a field of a name, such
as it.discount. Assigning to the value again ends it.
#Find out where your time goes
The manual has a new page, Tune a program for speed. It covers how to measure, how to tell time spent in your own code from time spent in the runtime, which data shapes are fast, and the habits that keep hot code compiled. One lesson from this release is in it: in a data-heavy program, much of the time goes on table reads, allocation and type checks, so the shape of your data matters as much as the compiler does.
--jit-stats is a good first step. It prints what the JIT
compiled and names each loop it couldn't compile, with the reason. It
now costs little on programs that run many partitions, and it sums the
figures across them, so you can leave it on for a realistic run.
#Upgrading: as becomes
parse
as(value, T)still compiles in 0.40.0, with a warning, and becomes an error in 0.41.0. Writeparse(value, T), which does exactly the same thing.- If a package has a function of its own called
parse, it warns that it hides the built-in whenever the package runs from its own folder. Renaming the function removes the warning.scua-pkg publish0.3.31 now reports any function that shares a built-in's name. - An enum value written with
//or%of a negative number (A = -7 // 2) is now -4, the same as the expression gives at run time.
The changelog lists everything else in 0.40.0.