From ad9eff3c2759be04132deec6237188c65f816e83 Mon Sep 17 00:00:00 2001 From: Stefan Vodita <41467371+stefanvodita@users.noreply.github.com> Date: Fri, 8 Dec 2023 00:27:49 +0000 Subject: [PATCH] Shorten getOrdinal synchronized loop (#12870) --- lucene/CHANGES.txt | 3 +- .../directory/DirectoryTaxonomyReader.java | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 2aa9c1eca1f..a48eea3871b 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -171,7 +171,8 @@ New Features Improvements --------------------- -(No changes) + +* GITHUB#12870: Tighten synchronized loop in DirectoryTaxonomyReader#getOrdinal. (Stefan Vodita) Optimizations --------------------- diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java index d8101ffe8e8..bf4867e0dda 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java @@ -279,21 +279,22 @@ public class DirectoryTaxonomyReader extends TaxonomyReader implements Accountab } // First try to find the answer in the LRU cache: + Integer res; synchronized (ordinalCache) { - Integer res = ordinalCache.get(cp); - if (res != null) { - if (res < indexReader.maxDoc()) { - // Since the cache is shared with DTR instances allocated from - // doOpenIfChanged, we need to ensure that the ordinal is one that - // this DTR instance recognizes. - return res; - } else { - // if we get here, it means that the category was found in the cache, - // but is not recognized by this TR instance. Therefore, there's no - // need to continue search for the path on disk, because we won't find - // it there too. - return TaxonomyReader.INVALID_ORDINAL; - } + res = ordinalCache.get(cp); + } + if (res != null) { + if (res < indexReader.maxDoc()) { + // Since the cache is shared with DTR instances allocated from + // doOpenIfChanged, we need to ensure that the ordinal is one that + // this DTR instance recognizes. + return res; + } else { + // if we get here, it means that the category was found in the cache, + // but is not recognized by this TR instance. Therefore, there's no + // need to continue search for the path on disk, because we won't find + // it there too. + return TaxonomyReader.INVALID_ORDINAL; } }