@Override
    public void apply(final Traversal.Admin<?, ?> traversal) {
        final Step step = traversal.getParent().asStep();
        if (step instanceof ByModulating && step instanceof TraversalParent) {
            final TraversalParent byModulatingStep = (TraversalParent) step;
            if (step instanceof Grouping) {
                final Grouping grouping = (Grouping) step;
                optimizeByModulatingTraversal(byModulatingStep, grouping.getKeyTraversal());

                // the value by() needs different handling because by(Traversal) only equals by(String) or by(T)
                // if the traversal does a fold().
                final Traversal.Admin<?, ?> currentValueTraversal = grouping.getValueTraversal();
                final List<Step> stepsInCurrentValueTraversal = currentValueTraversal.getSteps();
                if (stepsInCurrentValueTraversal.size() == 1 && stepsInCurrentValueTraversal.get(0) instanceof IdentityStep)
                    optimizeForStep(byModulatingStep, currentValueTraversal, stepsInCurrentValueTraversal.get(0));
                else if (stepsInCurrentValueTraversal.size() == 2 && stepsInCurrentValueTraversal.get(1) instanceof FoldStep)
                    optimizeForStep(byModulatingStep, currentValueTraversal, stepsInCurrentValueTraversal.get(0));

            } else {
                for (final Traversal.Admin<?, ?> byModulatingTraversal : byModulatingStep.getLocalChildren()) {
                    optimizeByModulatingTraversal(byModulatingStep, byModulatingTraversal);
                }
            }
        }
    }
This strategy looks for standard traversals in by-modulators and replaces them with more optimized traversals
(e.g. {@code TokenTraversal}) if possible.
<p/>

@author Daniel Kuppitz (http://gremlin.guru)
@author Stephen Mallette (http://stephen.genoprime.com)
@example <pre>
__.path().by(id())                        // is replaced by __.path().by(id)
__.dedup().by(label())                    // is replaced by __.dedup().by(label)
__.order().by(values("name"))             // is replaced by __.order().by("name")
__.group().by().by(values("name").fold()) // is replaced by __.group().by("name")
</pre>
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/ByModulatorOptimizationStrategy.java