From 9e44fa469cf888995fa4e563ea82b609141babf2 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 3 Feb 2021 13:08:23 -0500 Subject: [PATCH] Slightly speed up scripts This saves a couple of nanoseconds on ever invocation of a painless script that uses `SearchLookup`. Which is most of the scripts in the hot path. This amounts to a 3.5% performance savings for things like script scoring and runtime fields. We got this by speeding up a method that the async profiler reported as taking 20% of the time running a script score with painless. This drops it to 1%. You'd think that'd result in a 19% speed up. But no such luck. --- .../search/lookup/LeafStoredFieldsLookup.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/search/lookup/LeafStoredFieldsLookup.java b/server/src/main/java/org/elasticsearch/search/lookup/LeafStoredFieldsLookup.java index f01f4e8933891..f4ffacf1a027b 100644 --- a/server/src/main/java/org/elasticsearch/search/lookup/LeafStoredFieldsLookup.java +++ b/server/src/main/java/org/elasticsearch/search/lookup/LeafStoredFieldsLookup.java @@ -137,6 +137,14 @@ private FieldLookup loadFieldData(String name) { } private void clearCache() { + if (cachedFieldData.isEmpty()) { + /* + * This code is in the hot path for things like ScoreScript and + * runtime fields but the map is almost always empty. So we + * bail early then instead of building the entrySet. + */ + return; + } for (Entry entry : cachedFieldData.entrySet()) { entry.getValue().clear(); }