Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D8FC8DDDC for ; Tue, 24 Jul 2012 22:05:35 +0000 (UTC) Received: (qmail 51046 invoked by uid 500); 24 Jul 2012 22:05:35 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 50976 invoked by uid 500); 24 Jul 2012 22:05:35 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 50961 invoked by uid 99); 24 Jul 2012 22:05:35 -0000 Received: from issues-vm.apache.org (HELO issues-vm) (140.211.11.160) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Jul 2012 22:05:35 +0000 Received: from isssues-vm.apache.org (localhost [127.0.0.1]) by issues-vm (Postfix) with ESMTP id D3C2C1402B8 for ; Tue, 24 Jul 2012 22:05:34 +0000 (UTC) Date: Tue, 24 Jul 2012 22:05:33 +0000 (UTC) From: "Adrian Nistor (JIRA)" To: issues@commons.apache.org Message-ID: <1288841382.98618.1343167534869.JavaMail.jiratomcat@issues-vm> Subject: [jira] [Created] (COLLECTIONS-426) performance problem in ListOrderedSet.retainAll() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Adrian Nistor created COLLECTIONS-426: ----------------------------------------- Summary: performance problem in ListOrderedSet.retainAll() Key: COLLECTIONS-426 URL: https://issues.apache.org/jira/browse/COLLECTIONS-426 Project: Commons Collections Issue Type: Bug Affects Versions: 3.2.1 Environment: java 1.6.0_24 Ubuntu 11.10 Reporter: Adrian Nistor Attachments: Test.java, patch.diff Hi, I am encountering a performance problem in ListOrderedSet.retainAll(). It appears in version 3.2.1 and also in revision 1365132. I attached a test that exposes this problem and a patch that fixes it. On my machine, for this test, the patch provides a 263X speedup. To run the test, just do: $ java Test The output for the un-patched version is: Time is 3682 The output for the patched version is: Time is 14 The problem is that the code for method "ListOrderedSet.retainAll(Collection coll)" is: {code:java|borderStyle=solid} public boolean retainAll(Collection coll) { boolean result = collection.retainAll(coll); .... } {code} because "collection.retainAll(coll)" has quadratic complexity if parameter "coll" is a List. Conceptually, the solution is to call "coll.retainAll(collection)" which has linear complexity (not quadratic), because "collection" is always a HashSet (we know this, because "collection" is an inherited field of ListOrderedSet, and thus cannot have another type). See the "java.util.AbstractCollection.retainAll()" code inlined below for why one call has quadratic complexity, and the other has linear complexity. Directly calling "coll.retainAll(collection)" would change the behavior of ListOrderedSet.retainAll(), which is why the patch seems a bit involved. In reality, the patch simulates the behavior of "coll.retainAll(collection)" (which is just the intersection of two collections). You can easily see this by looking at the patch and at the "java.util.AbstractCollection.retainAll()" code inlined below. "collection.retainAll(coll)" is "java.util.HashSet.retainAll()", which is "java.util.AbstractCollection.retainAll()", which has the code: {code:java|borderStyle=solid} public boolean retainAll(Collection c) { boolean modified = false; Iterator e = iterator(); while (e.hasNext()) { if (!c.contains(e.next())) { e.remove(); modified = true; } } return modified; } {code} Is this a bug, or am I misunderstanding the intended behavior? If so, can you please confirm if the patch is correct? Thanks, Adrian -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira