Skip to content

A fix for the bug #1528074 [warning: quite slow] #44697

Closed

Description

@rtvd
mannequin
BPO 1678345
Nosy @birkenfeld
Files
  • python-bug-1528074-fix.diff: A fix for the bug difflib.SequenceMatcher.find_longest_match() wrong result #43714 (covered by the testcase python/issues-test-cpython#1678339)
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2008-01-21.17:50:48.288>
    created_at = <Date 2007-03-11.15:28:30.000>
    labels = ['library']
    title = 'A fix for the bug python/cpython#43714 [warning: quite slow]'
    updated_at = <Date 2008-01-21.17:50:48.286>
    user = 'https://bugs.python.org/rtvd'

    bugs.python.org fields:

    activity = <Date 2008-01-21.17:50:48.286>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-01-21.17:50:48.288>
    closer = 'georg.brandl'
    components = ['Library (Lib)']
    creation = <Date 2007-03-11.15:28:30.000>
    creator = 'rtvd'
    dependencies = []
    files = ['7861']
    hgrepos = []
    issue_num = 1678345
    keywords = ['patch']
    message_count = 6.0
    messages = ['52175', '52176', '52177', '52178', '52179', '61429']
    nosy_count = 4.0
    nosy_names = ['georg.brandl', 'jimjjewett', 'jorend', 'rtvd']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1678345'
    versions = ['Python 2.6']

    Activity

    1. rtvd commented on Mar 11, 2007

      rtvdmannequin
      MannequinAuthor

      This is a fix for the bug bpo-1528074 in the difflib's SequenceMatcher which makes (among other possible things) find_longest_match return invalid results sometimes.

      The previously submitted test case for this bug has the bpo-1678339 ID.

      The find_longest_match and __chain_b methods in the SequenceMatcher are perfectly optimized. The find_longest_match would work both fast and correctly if only the __chain_b did not break it's assumptions.

      The find_longest_match assumes that the b2j mapping has a mapping of all non-junk elements in b to lists of their indices in the "b" list. However, when __chain_b creates the b2j mapping, it removes popular elements from the list and marking the elements as popular in the "populardict". As a result, the find_longest_match method can't find the indices for the popular elements and they become automatically considered as something like a junk.

      I tried to fix the bug by both changing the find_longest_match and __chain_b methods. No matter how hard I tried, the change dropped the performance and slowed down the matching by 5-10 times. The impact of find_longest_match method was larger, so I decided to send a patch for the __chain_b.

      Please, note, that even though the method starts to work properly and the test cases pass on my computer just fine, the ingenious optimizations performed before become broken, so it would be great if a guru in Python code optimization tries to improve the things a bit.

      One more point: if the indices are not removed, the memory consumption on the large strings can become quite great. If this is a serious concern, a fix in the find_longest_match will need to be done instead. However that fix would probably be far less efficient that this one.

    2. added
      stdlib
      on Mar 11, 2007
    3. jimjjewett commented on Mar 19, 2007

      jimjjewettmannequin
      Mannequin

      I think the bug is documentation only.

      According to the comments (but not the docstring) find_longest_match returns the longest _interesting_ match. A single element appearing too often is likely to cause spurious matches, and is therefore not interesting.

      I do agree that this should be noted more prominently, so people don't try things like comparing text strings letter-by-letter (where 1% is far too low a threshhold for a 26-character alphabet).

      And yes, the comments on popular are correct -- it ignores elements which constitute *more* than 1%.

      I recommend closing this set of tracker items. If you could submit changes to the documentation (docstrings and/or help files; maybe even the comments), I would recommend applying them.

    4. jorend commented on Apr 20, 2007

      jorendmannequin
      Mannequin

      Recommend closing this.

    5. rtvd commented on Apr 21, 2007

      rtvdmannequin
      MannequinAuthor

      I am sure this is NOT a bug in the documentation. There is no such thing as "interesting" match unless it can be described mathematically. Instead, the documentation
      specifies _precisely_ what exactly the function must do in a very precise manner.

      The problem is that it does a different thing.

      Another issue of this algorithm is that it uses a quite slow algorithm. It's computational complexity is O(n*m). Another one based on generalized suffix trees has a much more modest complexity O(n+m). The details are here:
      http://en.wikipedia.org/wiki/Longest-common_substring_problem

    6. jorend commented on Apr 23, 2007

      jorendmannequin
      Mannequin

      rtvd, Thanks for taking the time to post this patch and participate in the discussion.

      This patch changes intentional, by-design behavior. It would hurt difflib's performance and reduce its usability, by making the matches worse. Subjectively worse, admittedly, but with many programmers sharing similar subjective views. Read the comments in the source code and you'll get the gist.

      And even a mathematician would dispute your assertion that there's no such thing as "interesting". That's a pretty extreme view.

    7. birkenfeld commented on Jan 21, 2008

      @birkenfeld
      Member

      Rejecting as per discussion.

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlib

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions