@Override
    public void apply(final Traversal.Admin<?, ?> traversal) {
        if (!(traversal.isRoot()) || TraversalHelper.onGraphComputer(traversal))
            return;
        final List<Step> steps = traversal.getSteps();
        if (steps.size() < 2 ||
                !(steps.get(0) instanceof GraphStep) ||
                0 != ((GraphStep) steps.get(0)).getIds().length ||
                !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
            return;

        for (int i = 1; i < steps.size() - 1; i++) {
            final Step current = steps.get(i);
            // used to include "current instanceof MapStep" but they will not necessarily emit an element as
            // demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
            //
            // used to include "current instanceof CollectingBarrierStep" but those steps like sample() can filter
            // demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
            if (!(current instanceof IdentityStep ||
                    current instanceof NoOpBarrierStep) ||
                    (current instanceof TraversalParent &&
                            TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateGlobalStep), (TraversalParent) current)))
                return;
        }
        final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
        TraversalHelper.removeAllSteps(traversal);
        traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
    }
This strategy will do a graph size call if the traversal is a count of the vertices and edges of the graph
or a one-to-one map chain thereof.

@author Marko A. Rodriguez (http://markorodriguez.com)
@example <pre>
g.V().count()               // is replaced by TinkerCountGlobalStep
g.V().map(out()).count()    // is replaced by TinkerCountGlobalStep
g.E().label().count()       // is replaced by TinkerCountGlobalStep
</pre>
tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/strategy/optimization/TinkerGraphCountStrategy.java