NIO excessive Integer allocation

Jon V. sybersnake at gmail.com
Mon Oct 31 20:20:00 UTC 2016


Hi everyone,

EpollSelectorImpl.java #updateSelectedKeys() has a major object creation
problem.  It creates Integers in order to get the key from the map.

Changing Integer.valueOf() to new Integer() might allow escape-analysis to
remove the object.

Otherwise, fdToKey could be change to a Map<Number, SelectionKeyImpl> and a
new mutable Integer class could be created so only a single object would
need to be created per select() instead of every Key.

/**
     * Update the keys whose fd's have been selected by the epoll.
     * Add the ready keys to the ready queue.
     */
    private int updateSelectedKeys() {
        int entries = pollWrapper.updated;
        int numKeysUpdated = 0;
        for (int i=0; i<entries; i++) {
            int nextFD = pollWrapper.getDescriptor(i);
            SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
            // ski is null in the case of an interrupt
            if (ski != null) {
                int rOps = pollWrapper.getEventOps(i);
                if (selectedKeys.contains(ski)) {
                    if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
                        numKeysUpdated++;
                    }
                } else {
                    ski.channel.translateAndSetReadyOps(rOps, ski);
                    if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
                        selectedKeys.add(ski);
                        numKeysUpdated++;
                    }
                }
            }
        }
        return numKeysUpdated;
    }


More information about the jdk8u-dev mailing list