Uploaded image for project: 'Lucene - Core'
  1. Lucene - Core
  2. LUCENE-9395

ConstantValuesSource creates more than one DoubleValues unnecessarily

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 8.5.2
    • 9.0, 8.7
    • core/search
    • None
    • New

    Description

      At my day job, we use ConstantValuesSource to represent default values or a constant query-level feature by calling DoubleValuesSource.constant. I realized under the hood the ConstantValuesSource.getDoubleValues creates a new DoubleValues which simply return the specified value each time it is called. 

      Unless I missed something, I don't see a risk of creating one DoubleValues as use it as the return value of all getDoubleValues() calls given that the constant DoubleValues doesn't maintain any state.

      We can also offer the user flexibilities of how to initialize it. 

      1) DoubleValuesSource.constant(double constant) – we can eagerly initialize an `DoubleValues` that returns the constant and make it the return value of all getDoubleValues() calls.

      2) DoubleValuesSource.constant(DoubleSupplier doubleSupplier)  – For lazy evaluation if the constant takes some time to compute and user expects the returned DVS will not be used in all code path.

      Attachments

        1. LUCENE-9395.patch
          2 kB
          Tony Xu

        Activity

          People

            Unassigned Unassigned
            hypothesisx86 Tony Xu
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: