Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Where do all the empty methods go?  (Read 4654 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Posted 2016-02-28 15:38:40 »

In my game all AI behaviors implement an interface that defines two methods: plan() and act(). Plan is for expensive calculations and is only executed infrequently, while act is for fine-grained updates like incrementing position. A lot of my behaviors don't need to plan, or don't need to act, so many of the methods have empty bodies.

What does Java do to empty methods? I'm guessing there could be some clever JIT optimizations for this.

Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2016-02-28 18:46:59 »

When it comes to inlining, the result should be obvious. Interface methods are however a tad harder to inline, as in your case, the callsite will probably call multiple interface implementations. This typically leads to a switch-table, jumping to the implementation (empty or not).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline princec

« JGO Spiffy Duke »


Medals: 1053
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2016-02-28 19:49:55 »

In the cases where the implementation is provably monomorphic - that is, there is only ever one implementation that can be called - then it'll get inlined. Empty methods calls are completely removed.

Apparently Hotspot can also inline "probably monomorphic" code, when nearly all calls to a particular implementation are to the same one out of a choice of many, which inserts a tiny fast check before the inline call to make sure it's actually doing the right thing. Someone who enjoys looking at machine code could show us this in action (I don't Cheesy)

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #3 - Posted 2016-02-28 20:27:25 »

No, this code isn't monomorphic so it won't get optimized.

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1232 views)
2018-06-10 19:43:48

EgonOlsen (1101 views)
2018-06-10 19:43:44

EgonOlsen (842 views)
2018-06-10 19:43:20

DesertCoockie (1251 views)
2018-05-13 18:23:11

nelsongames (1100 views)
2018-04-24 18:15:36

nelsongames (1329 views)
2018-04-24 18:14:32

ivj94 (2068 views)
2018-03-24 14:47:39

ivj94 (1222 views)
2018-03-24 14:46:31

ivj94 (2147 views)
2018-03-24 14:43:53

Solater (787 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!