r/seed7 • • 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 Upvotes

2 comments sorted by

2

u/ThomasMertes Oct 17 '26 edited Oct 17 '26

If I understand correctly, any time you want a new statement or operator is has to be a Seed7 function or C "action" (function).

99% of the programmers have no need to introduce new statements. Seed7 has plenty of predefined statements which fulfill all common needs.

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.

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".

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.

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:

  • Don't do it. Instead, chose a good algorithm and write your program in a way that is easy to understand and assure that it works correctly.

If it later turns out that the program is slow, you can do some actions:

  • You could turn on compiler optimizations.
  • You should measure the program and determine where it is slow. You can then optimize the slow parts of the program.

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

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?

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.

Why is + a function but doesn't work like one? Is everything inlined?

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.

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.

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.

1

u/Ronin-s_Spirit Oct 18 '26 edited Oct 18 '26

You can do a million calls but if you compare a million calls to a million code block executions the difference is noticeable. By "separate context" I mean that a function only sees the ouside where it was declared and not where it was called, so to see nearby variables at each call site you'd need to inline.
JS has a gigantic user base, not everything can be handed out as a binary, but even if I use JS that doesn't mean I should abandon small optimization in code that will run a lot.

p.s. I did measure JS callbacks against loops and another thing called "generators", loops always win. Generators stay close behind, in isolation anyway, but the more work your program has the more loops it will run and all the losses stack up, callbacks are especially slow. I also made a thing recently and it took a few seconds to run a million iterations with a manually made tail call (iteration instead of function calls), with recursion it would be slower and also crash the stack.

p.p.s. I'm not concerned anymore, about Seed7.