Github user srowen commented on a diff in the pull request:
https://github.com/apache/spark/pull/5608#discussion_r29046278
--- Diff: core/src/main/scala/org/apache/spark/util/SizeEstimator.scala ---
@@ -204,25 +204,36 @@ private[spark] object SizeEstimator extends Logging {
}
} else {
// Estimate the size of a large array by sampling elements without
replacement.
- var size = 0.0
+ // To exclude the shared objects that the array elements may link,
sample twice
+ // and use the min one to caculate array size.
val rand = new Random(42)
- val drawn = new OpenHashSet[Int](ARRAY_SAMPLE_SIZE)
- var numElementsDrawn = 0
- while (numElementsDrawn < ARRAY_SAMPLE_SIZE) {
- var index = 0
- do {
- index = rand.nextInt(length)
- } while (drawn.contains(index))
- drawn.add(index)
- val elem = ScalaRunTime.array_apply(array,
index).asInstanceOf[AnyRef]
- size += SizeEstimator.estimate(elem, state.visited)
- numElementsDrawn += 1
- }
- state.size += ((length / (ARRAY_SAMPLE_SIZE * 1.0)) * size).toLong
+ val drawn = new OpenHashSet[Int](2 * ARRAY_SAMPLE_SIZE)
--- End diff --
Yes that looks better. We could even generalize to sampling n times easily
but that could be overkill. I think we have a potential problem here, that we
sample if the array size is >= 400, but then want at least 400 distinct
elements from the array, twice. This will enter an infinite loop if the array
has between 400 and 800 elements, and will be very slow if it's just a bit
larger than 800.
You could sample with replacement, or, only draw `ARRAY_SAMPLE_SIZE/2`
elements ( `ARRAY_SAMPLE_SIZE/n` in general. For simplicity, and to avoid
slow-downs, I'd say sample with replacement.
You can put the sample threshold back to 200 then, too. I don't know if
that needs to change.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]