harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Dmitriev (JIRA)" <j...@apache.org>
Subject [jira] Created: (HARMONY-5025) [classlib][nio] make AbstractSelectableChannel.containsValidKeys() a little bit faster
Date Thu, 25 Oct 2007 18:26:51 GMT
[classlib][nio] make AbstractSelectableChannel.containsValidKeys() a little bit faster
--------------------------------------------------------------------------------------

                 Key: HARMONY-5025
                 URL: https://issues.apache.org/jira/browse/HARMONY-5025
             Project: Harmony
          Issue Type: Improvement
          Components: Classlib
            Reporter: Sergey Dmitriev


Some time ago I've found a piece of code in NIO which can be simply improved.

AbstractSelectableChannel.java:
    /**
     * Returns true if the keyList contains at least 1 valid key and false otherwise.
     */
    private synchronized boolean containsValidKeys() {
-         for (Iterator<SelectionKey> iter = keyList.iterator(); iter.hasNext();) {
-             SelectionKey key = iter.next();
+        for (int i=0; i<keyList.size(); i++) {
+            SelectionKey key = keyList.get(i);
            if (key != null && key.isValid()) {
                return true;
            }
        }
    }

Just don't create an Iterator here. Indeed no big deal, but it would be just pity to loose
this discovery. :)
I even created the mini test to demonstrate the benefit of my patch (approx 10%).

{guy@nowhere:~/tmp} cat cvk.java
import java.io.*;
import java.nio.channels.*;

// cvk: containsValidKeys()
public class cvk {

    static int numOfIters = 10;
    static int numOfConfigures = 10*1000*1000;

    public static void main(String[] args) throws Exception {
        ServerSocketChannel ssc = ServerSocketChannel.open();

        for (int i=0; i<numOfIters; i++) {
            long millis = System.currentTimeMillis();
            for (int j=0; j<numOfConfigures; j++) {
                ssc.configureBlocking(false);
                ssc.configureBlocking(true);
            }
            System.out.println(System.currentTimeMillis() - millis);
        }
    }
}

{guy@nowhere:~/tmp} ~/harmony.29sep.my.nocvk/bin/java -Xem:server cvk
7049
6149
6134
6125
6140
6139
6131
6142
6148
6200

{guy@nowhere:~/tmp} ~/harmony.29sep.my.cvk/bin/java -Xem:server cvk
5769
5499
5491
5497
5483
5474
5466
5470
5453
5478


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message