why a typeguard single implementation interface field

Peter Veentjer alarmnummer at gmail.com
Mon Jan 30 09:50:30 UTC 2017


Hi,

I'm studying the Assembly the JIT generates and one of the examples I'm
looking at is the overhead of a field using some interface and having a
single implementation of that interface. This is very common in a lot of
applications; you program to some interface e.g. EmployeeManager,
ContractDao etc etc and there is only a single implementation of that
interface.

public class SingleImplementation {

    private final Service service;

    public SingleImplementation(Service service) {
        this.service = service;
    }

    public int sizePlusOne() {
        return service.size() + 1;
    }

    public static void main(String[] args) {
        SingleImplementation f = new SingleImplementation(new
ServiceImpl());

        int result = 0;

        for (int k = 0; k < 100_000; k++) {
            result += f.sizePlusOne();
        }

        System.out.println("result:" + result);
    }
}

interface Service {
    int size();
}

class ServiceImpl implements Service {
    private int size = 1;

    @Override
    public int size() {
        return size;
    }
}

When I use the following command to list the Assembly:

-XX:+UnlockDiagnosticVMOptions -XX:PrintAssemblyOptions=intel
-XX:-TieredCompilation -XX:-Inline -XX:-BackgroundCompilation
-XX:CompileCommand=print,*SingleImplementation.sizePlusOne

Using Java:

java version "1.8.0_91"

Java(TM) SE Runtime Environment (build 1.8.0_91-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)


If I copy the relevant part I get the following:

  0x0000000110aa500c: mov    r11d,DWORD PTR [rsi+0xc]  ;*getfield service
                                                ; -
com.liskov.SingleImplementation::sizePlusOne at 1 (line 12)

  0x0000000110aa5010: mov    r10d,DWORD PTR [r12+r11*8+0x8]
                                                ; implicit exception:
dispatches to 0x0000000110aa5049
  0x0000000110aa5015: cmp    r10d,0x33187b42    ;
{metadata('com/liskov/ServiceImpl')}
  0x0000000110aa501c: jne    0x0000000110aa5034
  0x0000000110aa501e: lea    r10,[r12+r11*8]    ;*invokeinterface size
                                                ; -
com.liskov.SingleImplementation::sizePlusOne at 4 (line 12)

  0x0000000110aa5022: mov    eax,DWORD PTR [r10+0xc]
  0x0000000110aa5026: inc    eax                ;*iadd
                                                ; -
com.liskov.SingleImplementation::sizePlusOne at 10 (line 12)

This code shows there is a typeguard; even though there is only a single
implementation of the 'Service' interface. Why is this typeguard required?
If a second conflicting implementation of Service would be loaded, why not
rely on CHA to take care of deoptimizing the code?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20170130/5924653c/attachment.html>


More information about the hotspot-compiler-dev mailing list