I've just been profiling some code where I increment some frequency counters with the following code:
Datastore ds = ...
final Query<Unit> query = ds.createQuery(Unit.class);
query.field("text").equal(text);
query.field("langCode").equal(lang.getCode());
UpdateOperations ops = ds.createUpdateOperations(Unit.class);
ops.inc("frequency", value);
ds.update(query, ops, false);
The creation of the query takes almost 50% of the execution time, and I'd like to reuse the work somehow. Is it safe to save the query
and ops
objects in a ThreadLocal and just call query.field("text").equal(text)
again to replace the "text" field? It also looks like validation is taking up some 30% of the total time.
Yes, you can do that. Each field is stored in a map so it will replace it when called multiple times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With