Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the number of objects created in Scala?

I'm programming a computer graphics application in Scala which uses a RGB class to return the color at a point in the image. As you can imagine, the function which return the color RGB object is called many times.

class RGB(val red: Int, val green: Int, val blue: Int) { }

There is a function getPixelRGB which is often used as follows

val color:RGB = getPixelRGB(image, x, y)

The problem is that I may call this function a million times which will then, I believe, generate a million unique RGB object instances, thats a very unattractive situation. There are some thoughts I have about this:

  1. getPixelRGB could potentially create an infinite number of objects if it was called an infinite number of times, but it need not be an infinite number of objects as there are only a maximum of 255 * 255 * 255 possible combinations which can be produced for RGB. So the number of objects created "should" be finite. This function could be adjusted to use a object pool where if it is to return the same color as some time before it could return the same pooled object instance for that color.

  2. I could encode this RGB as a Int. An Int would have less memory overhead than a normal Scala/Java object, Java objects have extra memory overhead. Since a Scala Int type is 4 bytes wide, the first 3 bytes could store the RGB value. Only returning an Int rather than a RGB from the getPixelRGB method would be less memory overhead I assume. However how to do this while still having the convince of the RGB class?

  3. Supposedly, and they are, short lived objects and I have read that the garbage collector should re-claim them quickly. However I'm still worried about it. How does the GC know that I'm throwing it away quickly? So confusing.

So in general, my question is how to make this getPixelRGB more memory friendly? also should I even be worried about it?

like image 683
Phil Avatar asked Nov 30 '12 13:11

Phil


People also ask

What is Scala reduce function?

The reduce function is applicable to both Scala's Mutable and Immutable collection data structures. The reduce method takes an associative binary operator function as parameter and will use it to collapse elements from the collection. Unlike the fold method, reduce does not allow you to also specify an initial value.

Does Scala use garbage collection?

Garbage collection is the responsibility of the JVM, not Scala. So the precise details depend on which JVM you're running. There is no defined time at which garbage collection is triggered; the JVM tries to do it when it is opportune or necessary.

What is difference between class and object in Scala?

Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.

How do you create an object in Scala?

The syntax of creating object in Scala is: Syntax: var obj = new Dog(); Scala also provides a feature named as companion objects in which you are allowed to create an object without using the new keyword.


1 Answers

You can encode RGB with single long or int. Moreover, in scala 2.10 you can define value class for primitive values, say

class RGB private(val underlying: Long) extends AnyVal {
  def toTriple = /*decoding to (red, green, blue)*/
} 
object RGB {
  def apply(red: Int, green: Int, blue: Int) = /* encode and create class with new RGB(longvalue)*/
}

With value class you can still have type information and enjoy class-less memory layout in JVM.

like image 193
om-nom-nom Avatar answered Nov 10 '22 02:11

om-nom-nom