Hi,
As you may know from Gremlin2, g.V.out.out.out is cheaper than g.V.out.loop(1){it.loops < 3} in terms of both time and space. There is nearly 5x time difference as you can see in our recommended optimizations section of the Gremlin2 documentation:
One way to avoid this cost is if the "ifPredicate" is of the form "it.loops < x," then unroll your loop. This technique is used extensively in modern language compilers --
http://en.wikipedia.org/wiki/Loop_unwinding. With Gremlin3 and TraversalStrategies, we can now unroll loops by analyzing the Traversal accordingly within the new UnrollJumpStrategy.
The strategy finds all JumpSteps in the traversal. If the JumpStep is "unrollable" then the … section of as('x')…jump('x') is isolated and cloned and inserted accordingly. Here are some runtimes with the strategy enabled and disabled.
MANUAL UNROLLING
gremlin> t = System.currentTimeMillis(); g.V().out().out().out().iterate(); System.currentTimeMillis() - t
==>841
UNROLL JUMP STRATEGY DISABLED
gremlin> traversal = g.V().as('x').out().jump('x',3); traversal.strategies().unregister(com.tinkerpop.gremlin.process.graph.strategy.UnrollJumpStrategy.class)
gremlin> t = System.currentTimeMillis(); traversal.iterate(); System.currentTimeMillis() - t
UNROLL JUMP STRATEGY ENABLED
gremlin> t = System.currentTimeMillis(); g.V().as('x').out().jump('x',3).iterate(); System.currentTimeMillis() - t
Pretty nifty eh?
Enjoy!,
Marko.