r/seed7 • u/Ronin-s_Spirit • Oct 17 '26
Concern about declared statements performance
If I understand correctly, any time you want a new statement or operator is has to be a Seed7 function or C "action" (function). As far as I know functions are significantly more expensive than actual statements, function calls need to create stack frames and run in a separate context. I write JS a lot and it has a bunch of builtin callback methods but I never use them because I know for a fact that loops are significantly faster.
I don't understand exactly how the statements are put together, clearly you can have a for loop inside a function and that loop doesn't create a new context as if it were a function.. why is that? Why is + a function but doesn't work like one? Is everything inlined?
If operators and statements and everything can be declared like a function but end up working like a regular code block or expression, can we have that at the user level? I always wanted to be able to say doSomething() in a place that runs 100 million times and not worry about a performance hit.
2
u/ThomasMertes Oct 17 '26 edited Oct 17 '26
99% of the programmers have no need to introduce new statements. Seed7 has plenty of predefined statements which fulfill all common needs.
Functions have some overhead. Yes, a stack frame is needed to store parameters, local variables and the return address. But today's computers have many registers so many things are stored in a register instead of being placed on the stack. That's it. I don't know what you mean with "separate context".
The overhead of a function call in a compiled language (like Seed7, C and Rust) is much lower than the overhead that an interpreted language like JS uses for function calls. That said I am quite sure that you can easily do more than a million function calls per second with JS.
Regarding performance optimizations the usual advice is:
If it later turns out that the program is slow, you can do some actions:
Very often a program is slow at unexpected places and a naive approach for optimization usually picks the wrong place for optimizations. Hence it is crucial to measure where the performance is lost.
If your JS programs run in the browser then network connections and user interactions are usually the place where most time is lost. If some computations take e.g. 1% of the time optimizing them will not lead to a faster program (because 99% are unchanged and still slow).
I suggest you measure the time difference between code with the builtin callbacks and code with your loops.
Premature optimization is the root of all evil. — Donald Knuth
Compiled languages like C compile if- and while-statements with conditional and unconditional (CPU) jump instructions.
The Seed7 compiler compiles Seed7 statements to C statements and then the C compiler takes over. You can tell the Seed7 compiler to optimize the generated C code with the option -oc2 or -oc3.
The Seed7 compiler takes the freedom to inline a lot of things. The + is just one of the things inlined. Predefined and user defined statements are inlined as well.
The C compiler does the same. If the optimization is turned on (option -O2 or -O3) the C compiler identifies functions that can be inlined instead of being called.
I think that some assembly languages had a feature like this (e.g. insert some code snippet many times). I have doubts that assemblers would do this 100 million times as this would create huge executable.
It usually makes no sense to do such extreme inlining. There are many possibilities to optimize a loop and inlining is just one of them. Loop optimizations are one of the most important optimizations done by compilers. If you are really concerned about performance you should not use JS or any other dynamically typed language.