Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3941516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:33:09+00:00 2026-05-20T00:33:09+00:00

An excerpt from the stairway book: If efficiency is very important, lean towards using

  • 0

An excerpt from the stairway book:

If efficiency is very important, lean towards using a class. Most Java
runtimes make a virtual method invocation of a class member a faster operation
than an interface method invocation. Traits get compiled to interfaces
and therefore may pay a slight performance overhead. However, you should
make this choice only if you know that the trait in question constitutes a performance
bottleneck and have evidence that using a class instead actually
solves the problem.

I wrote some simple code to see what really happens behind the scenes. And I did notice invokevirtual being used in case of an abstract class and invokeinterface in case of an interface.
But no matter what kind of code I wrote they always roughly performed the same. I use HotSpot 1.6.0_18 in server mode.

Is it JIT doing such a great job optimizing?
Does anybody have a sample code which proves the claim from the book about invokevirutal being the faster operation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:33:09+00:00Added an answer on May 20, 2026 at 12:33 am

    If HotSpot notices that all instances at the call site are of the same type, it is able to use a monomorphic method call and both virtual and interface methods are optimized the same way. The documents PerformanceTechniques and VirtualCalls make no distinction between virtual and interface methods.

    But in the general non-monomorphic case there might be some difference. The InterfaceCalls document says:

    There is no simple prefixing scheme in which an interface’s methods are displayed at fixed offsets within every class that implements that interface. Instead, in the general (non-monomorphic) case, an assembly-coded stub routine must fetch a list of implemented interfaces from the receiver’s klassOop, and walk that list seeking the current target interface.

    It also confirms that the monomorphic case is the same for both:

    Nearly the same optimizations apply to interface calls as to virtual calls. As with virtual calls, most interface calls are monomorphic, and can therefore be rendered as direct calls with a cheap check.

    Other JVMs might have different optimizations.

    You could try a micro benchmark (if you know how) which calls methods on multiple classes which implement the same interface, and on multiple classes which extend the same abstract class. That way it should be possible to force the JVM to use non-monomorphic method calls. (Though in the real life any difference there might not matter, since most calls sites are anyways monomorphic.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an excerpt of code from a class I am working with in
Excerpt from a book: A float value consists of a 24-bit signed mantissa and
Excerpt from http://php.about.com/od/advancedphp/ss/php_sessions.htm : So how will it know it's me? Most sessions set
What did I do wrong? Here is an excerpt from my code: public void
Excerpt from: http://www.sqlservercentral.com/articles/Indexing/68563/ The width of the clustering key does not, however, only affect
Excerpt from my JavaScript console: > 0 in [1, 2] true Why?
An excerpt from Programming Perls : A Simple Design : Antonie de Saint-Exupery, the
An excerpt from Exceptional C++: In the old days, you could just replace #include
Here's an excerpt from a fairly standard Rails form: <p> <%= f.label :from_station %>
I've posted an excerpt from my spring.xml file and the stack trace I'm getting.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.