Selector cleanup

Rémi Forax forax at
Thu Jan 24 12:05:44 UTC 2008

Hi all, i currently develop a small web server and  I think codes related
to selectors can be improved just by changing some small pieces of code.
To be crystal clear, i don't want to re-implement all selector related 
stuffs but
just patch some parts of the actual code.

There are some allocations in JDK API  that can be removed,
the code was badly retrofited to 1.5 and lot of field can be declared final.
Some methods/fields still 'use' raw types and doesn't take
advantage of autoboxing.

Futhermore, there is some divergence between Windows and *nix
code i don't understand.
By example, WindowsSelectorImpl and PollSelectorImpl uses a pipe to
implements wakeup but WindowsSelectorImpl  relies on Pipe
and PollSelectorImpl on IOUtil.initPipe().
I think this code should be the same.

in WindowsSelectorImpl:
  - updateSelectedKeys() use an iterator to traverse the array
    (an ArrayList). It should use an indexed loop instead
    to avoid Iterator allocation.
  - field threads should be declared as an ArrayList
    because adjustThreadsCount() supose that i can be iterate
    using an indexed loop.
    Furthermore, it can be generified like this:
    private final ArrayList<Thread> threads = new ArrayList<Thread>();
 - class FDMap,
   I don't see why FdMap need to be a class, all methods can be moved
   as member methods of WindowsSelectorImpl without problems.
   Futhermore, the constructor of FdMap is private (get/put/remove too)
   so the compiler stupidly inserts accessor methods (access$000 etc.).
   Ok, the main point, here when the code was retrofited to 1.5,
   The new Integer() was not transformed to use Integer.valueOf()
   to share small integers and avoid allocation if file descriptor value 
are small.
- In class MapEntry, ski should be declared final.
- close(), set selectedKeys() to null doesn't allow the Set to be collected
    because publicSelectedKeys contains() a reference to it.

in PollSelectorImpl:
  - interruptLock should be final.
  - close(), see WindowsSelectorImpl

in EpollSelectorImpl:
  - like in poll, interruptLock should be final.
  - hashMap fdTokey should be generified and final.
  - close(), see WindowsSelectorImpl
  - implRegister/implDereg
    - They should use Integer.valueOf() instead of new Integer().
    - IOUtil.fdVal() is used spuriously, in implRegister but not
      in implDereg.

  - EPollArrayWrapper
    - updateList is a LinkedList, a double linked list that stores 
Updator object,
      I think it's more efficient to add a field next in the Updator 
object and
      link updator by hand in order to avoid to create LinkedList$Entry .
   - Updataor.opcode and Updator.fd should be declared final.
   - SelectorImpl:
     key and selectedKeys should be LinkedHashSet instead of Set
     because they are frequently iterated.
let discuss about that before I submit patchs.

More information about the core-libs-dev mailing list