• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about method call stack  RSS feed

 
John Stuart
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one topic in the book I read today. According to the book and Wikipedia, call stack operates on a LIFO (Last In First Out) basis and is used to store information about active methods so that it knows where to return value or control back to the caller or to be more precise, call stack stores the return address. (correct me if I'm wrong).

With that said, if I have 3 methods and each method calls another method, the first call would be placed at the bottom of the stack and the subsequent call on top of it.



//Imaginary stack
- Method call 3
- Method call 2
- Method call 1

If I'm correct up until this point, then I'm assuming that Method call 1 would get the slowest execution time even thought it is placed at earlier order in the source code?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a lot more complicated than that. You can have a guess by reading the bytecode. Compile a tiny class and view its bytecode with
javap -c MyClass
Yes, method1 is put onto the stack then method2 higher up the stack, etc etc. Method 1 is not executed more slowly; the execution time for method 1 includes the time for method 2 so method 1 is almost by definition longer.

But even that is a horrible oversimplification; some methods may be removed from the stack and inlined as an optimisation. That optimisation may be different on different platforms and different runs, because it depends on how frequently the method it called.
 
John Stuart
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you mention it, Wikipedia did mention something about it.

The actual details of the stack in a programming language depend upon the compiler, operating system, and the available instruction set.


Thanks for the clarification. It confuses me at the first time since I thought that a source code is read from up to bottom and left to right (most of the time) and it suddenly mention about LIFO approach.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The direction of reading source code varies from language to language. In C you must declare everything before you use it. In Java® however you can declare fields afterwards.Now, the usual convention is to have fields first, but that class will compile nicely. The code is parsed top line to bottom line and left to right.

If you don't think only about calling methods, you can get LIFO easily. You need to think about finishing methods, too:-
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if method 1 would have executed earlier than method 2
Nothing would have been working in java
As main method is always at the bottom of stack frame so main method method would have been executed earlier than any other method and execution of java program would stop without executing any other method except main
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By executed you mean completed, I presume.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ,it means completed
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C compilers - like many compilers - historically compiled code linearly from top to bottom. Requiring a method or object to be declared before using it made it easier for the compiler to properly configure and check the object and the code. Actually in the oldest compilers, RAM was so tight that 6 or 7 passes might be made over the code and its product before the final binary was created. Even today, well-known compilers like gcc run through multiple stages, but that's more aligned with orderly processing and the ability to swap out front ends (language syntax) and back ends (target CPU/OS).

Java's compiler was designed to encourage recursive constructs, so it's possible to declare stuff after it's referenced. as the compiler will sort everything out and thus everything will be in its proper place in the class file.

Regardless, the order that things were declared does not affect performance. And actually, neither does the order in which they are called, although performance-monitoring tools do tend to report the total execution time for a method to be the actual total of both the method and and methods/services that are in turn called by the method and thus the higher methods on the stack will have spent more time living on the stack.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!