Opened 16 years ago

Closed 16 years ago

Last modified 15 years ago

#272 closed defect (fixed)

QueryParser scales as O(N*N) with AUTO_MULTIWORD_SYNONYM

Reported by: Richard Boulton Owned by: Olly Betts
Priority: low Milestone: 1.0.7
Component: Other Version: SVN trunk
Severity: normal Keywords:
Cc: Blocked By:
Blocking: Operating System: All

Description (last modified by Richard Boulton)

I've just fixed various cases where the QueryParser scaled as O(N*N) where N is the number of terms in the query. However, there is one such case left, which involves parsing queries with the multiword synonym option.

Unlike the cases I've just fixed (which involved pair-wise construction of Query objects), the O(N*N) behaviour here appears to stem from searching for any of the synonyms in all the possible multiword substrings of the query. A callgrind run showed 500501 invocations of string::resize() from the query parser for a 1000 word query.

The current algorithm works by, for each starting point:

  1. build a string containing all the subsequent words in the query
  2. test for a synonym matching this string
  3. (if no synonym found) cut off the last word in the string and go back to 2.

I'm not sure that the feature can be reduced to linear, but some possible optimisations might be:

  • Don't bother looking for synonyms of more than 250 characters (or thereabouts), since there is a limit on the length of synonym keys of about this amount.
  • keep track of the number of words in the longest multiword synonym key stored, and only bother testing for substrings of the query of at most this length. This alone should change the scaling to O(N*M), where M is the number of words in the longest synonym.
  • Synonyms are stored in the btree in sorted order, so we can use synonym_keys_begin(prefix) to check if any synonyms exist with a given prefix. Then, we could check for a synonym based on the first word, and only progress to searching for one based on the first and second word if a possible candidate was found.

Change History (3)

comment:1 by Richard Boulton, 16 years ago

Description: modified (diff)

comment:2 by Richard Boulton, 16 years ago

Resolution: fixed
Status: newclosed

Fixed in trunk SVN HEAD (r10648), using the synonym_keys method.

comment:3 by Olly Betts, 15 years ago

Milestone: 1.0.7

This was actually fixed in trunk r10684 (note: 84 not 48!)

Backported for 1.0.7 in r10746.

Note: See TracTickets for help on using tickets.