LuciferYang opened a new pull request, #50346:
URL: https://github.com/apache/spark/pull/50346

   ### What changes were proposed in this pull request?
   This pr changes the explicit `Iterable` types in Spark code to use 
`nonEmpty`/`isEmpty` for empty checks, replacing `Iterable.size > 
0`/`Iterable.size == 0`.
   
   ### Why are the changes needed?
   For data structures that can only be confirmed as `Iterable`, since their 
specific data structure cannot be determined, using `Iterable.size > 
0`/`Iterable.size == 0` for empty checks may trigger an iteration over the 
collection:
   
   
https://github.com/scala/scala/blob/3f6bdaeafde17d790023cc3f299b81eaaf876ca3/src/library/scala/collection/IterableOnce.scala#L969-L982
   
   ```scala
     /** The size of this $coll.
      *
      *  $willNotTerminateInf
      *
      *  @return    the number of elements in this $coll.
      */
     def size: Int =
       if (knownSize >= 0) knownSize
       else {
         val it = iterator
         var len = 0
         while (it.hasNext) { len += 1; it.next() }
         len
       }
   ```
   
   Therefore, it is best to use `nonEmpty`/`isEmpty` for empty checks to avoid 
potential performance issues:
   
   
https://github.com/scala/scala/blob/3f6bdaeafde17d790023cc3f299b81eaaf876ca3/src/library/scala/collection/IterableOnce.scala#L946-L960
   
   ```scala
     /** Tests whether the $coll is empty.
      *
      *  Note: The default implementation creates and discards an iterator.
      *
      *  Note: Implementations in subclasses that are not repeatedly iterable 
must take
      *  care not to consume any elements when `isEmpty` is called.
      *
      *  @return    `true` if the $coll contains no elements, `false` otherwise.
      */
     def isEmpty: Boolean =
       knownSize match {
         case -1 => !iterator.hasNext
         case  0 => true
         case  _ => false
       }
   ```
   
   
   ### Does this PR introduce _any_ user-facing change?
   No
   
   
   ### How was this patch tested?
   Pass GitHub Actions
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to