I am looking for advice/best practice on caching painless scripts. For my case, the script itself is lightweight an simple, no more than one simple statement, and no script parameters. An example is "return doc.getter()" . The scripts are, however, executed very frequently, say thousands of times per second.
As per Painless doc , Painless has built-in caching. So my first vanilla version looks like this.
scriptService.compile("return doc.getter()", CONTEXT).newInstance().execute(doc); // run thousands of times a second.
Since the impacts on performance are significant, I changed it a bit to cache the compiled script on the caller side. It looks like this.
Script cachedScript = scriptService.compile("return doc.getter()", CONTEXT).newInstance(); // only once
cachedScript.execute(doc); // still thousands of times a second
The performance is significantly better than that of the vanilla version. So I am wondering if I am using Painless correctly.