Static and Dynamic Map Instances
This is an interesting piece of Gremlin:
It creates a Map
from an injected value by passing it to project()
with a single key. It’s interesting because it provides a way to dynamically construct a Map
using a specified value and key within Gremlin. While it is equally possible to do the following:
to achieve the same end, I think that the project()
case demonstrates a difference worth examining a bit using a more advanced example with constant()
. First, note that we can see the same sort of Map
creation using that step:
and the same result can be achieved with a static Map
supplied as a constant()
:
There is a subtle difference however:
The use of constant([x:0])
supplies the same Map
instance for each Vertex
traverser that passes to it, where as the use of constant(0).project('x')
creates a new Map
for each traverser. We might wonder the effect such equality would have on a traveral. Interestingly, profile()
should similar results for bulking operations:
Note that the barrier()
reduces the traverser count to one in both cases. It appears that Map
equality is based on the contents for bulking purposes. Using the same object (i.e. constant()
) rather than allocating new ones (i.e. constant(0).project('x')
) seems to have no direct difference in traversal operation apart from adding more items to the heap for garbage collection in the latter case. Since there are no Gremlin steps that can directly modify the Map
instance, there don’t appear to even be situations where modifications of the same Map
might cause problems. All anyone could do is unfold()
the Map
to entries, merge in new ones, and then create a new Map
object from that as shown here:
The constant().project()
approach seems to only have some usage where the value is dynamic, but it seems to point out a missing aspect of Gremlin. Specifically, Gremlin lacks the means to dynamically generate values within the traversal. Imagine doing a remote traversal where it was desired to do a server side timestamp. There really is no option in that case. The constant()
-step does not help for this purpose as whatever value is provided to it will be resolved on the client prior to Gremlin bytecode generation and then sent as a static value to the server. A missing component here is the ability to do:
in which case, the use of project()
might have some desireable usage:
Perhaps a feature like that will be available in future versions of Gremlin.