Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeMap from, to and range methods are O(n) instead O(log n) #4026

Closed
scabug opened this issue Nov 23, 2010 · 5 comments
Closed

TreeMap from, to and range methods are O(n) instead O(log n) #4026

scabug opened this issue Nov 23, 2010 · 5 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Nov 23, 2010

Calls to TreeMap.from, TreeMap.to, TreeMap.range run in time O(n) with respect of number of the items in the returned subtree.

Valuable comment from Nathan Bronson, I got on the scala-user discussion list:

The underlying RedBlack's range implementation is O(log n), but
TreeMap.rangeImpl then immediately calls RedBlack.count on the subrange, which is O(n).
Perhaps RedBlack.NonEmpty.count should be a val instead of a def?"

Expected behaviour: the time should be O(log n) with respect to the number of all items in the tree.

Reproducibility: always.

How to reproduce: Create a large TreeMap and call one of the mentioned methods, to return a large portion of the tree.

What versions of the following are you using?
Scala: 2.8.1.final
Java: 1.6.0 update 22 (both server and client)
Operating system: Windows Vista Home Premium

@scabug
Copy link
Author

scabug commented Nov 23, 2010

Imported From: https://issues.scala-lang.org/browse/SI-4026?orig=1
Reporter: Piotr Koaczkowski (pkolaczk)
See #5331

@scabug
Copy link
Author

scabug commented Nov 23, 2010

@axel22 said:
Hello, thanks for reporting this!

Have you tried this with the trunk, after a recent commit:

https://lampsvn.epfl.ch/trac/scala/ticket/3796#comment:22

@scabug
Copy link
Author

scabug commented Nov 23, 2010

@dcsobral said:
Replying to [comment:1 prokopec]:

Hello, thanks for reporting this!

Have you tried this with the trunk, after a recent commit:

https://lampsvn.epfl.ch/trac/scala/ticket/3796#comment:22

Actually, that was a different issue. The original RedBlack code, the one that went into 2.8.0 and 2.8.1, was broken. The temporary fix was O(n), but never got released (as far as I know -- unless it went into 2.8.1 after all).

The problem referred to is here (scala.collection.immutable.TreeMap):

    val ntree = tree.range(from,until)
    new TreeMap[A,B](ntree.count, ntree)

Specifically, that count on ntree. On the other hand, turning NonEmpty's count into a {{val}} sounds reasonable. It can always be counted in constant time, because it only needs to add the counts of its (at most) two subtrees. And if those subtree's count have already been computed... And, of course, since the collection is immutable, there's no need to recompute.

It would make the memory footprint of RedBlack heavier. And if we went this way, I'd be much in favor of putting in a depth val too -- I missed it on range, and it usually helps with red&black algorithms.

@scabug
Copy link
Author

scabug commented Nov 23, 2010

@axel22 said:
@dcsobral: I've mistaken it for this commit - https://lampsvn.epfl.ch/trac/scala/changeset/23202/scala/trunk/src/library/scala/collection/immutable/RedBlack.scala, which didn't go to 2.8.1. as far as I know.

But, as far as the memory footprint goes, currently NonEmpty has 5 fields - $$outer reference and 4 other references. If "Rule 1: every object is aligned to an 8 bytes granularity." in Sun's JVM and this post are to be trusted ("http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html"), then 4 bytes are lost anyway.

I wouldn't say that would hurt the memory footprint considerably, since clients of red black trees are already aware of them. So, I'd vote for adding the count field.

@scabug
Copy link
Author

scabug commented May 19, 2012

@retronym said:
TreeMap was reimplemented for Scala 2.10, these methods are efficient now.

https://github.com/scala/scala/commits?author=erikrozendaal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants