tinkerpop/pipes — pull request #70 by BinaryRage (2012-07-31 10:06:17+00:00)
Title: Use fast exceptions for NoSuchElementException on next()

This adds a NoSuchElementException subclass, FastNoSuchElementException, that provides a static instance. This requires no change to method signatures, only the throw needs to be changed:

throw new FastNoSuchElementException.instance()

Background is discussed here:

https://groups.google.com/forum/?fromgroups#!topic/gremlin-users/t7T0A-QRJX8

There's no particular problem with exceptions or try/catch as a pattern for flow control, the overhead of exception _handling_ is largely overblown, but the CPU overhead of fillInStackTrace and object allocation of the stack frames and exception object itself is not.

Comparisons between three approaches using 100000 iterators constructed in a loop and next() called twice to cause NoSuchElementException:

throw new NoSuchElementException() (current): 295.4235050678253ms
throw new FastNoSuchElementException(): 26.63270902633667ms
throw new FastNoSuchElementException.instance(): 13.198302030563354ms