如何减少在Scala中创建的对象的数量? [英] How to reduce the number of objects created in Scala?

查看:107
本文介绍了如何减少在Scala中创建的对象的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编程Scala中的计算机图形应用程序,它使用RGB类来返回图像中某点的颜色。正如你可以想象的那样,返回RGB颜色对象的函数被多次调用。

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

函数getPixelRGB经常被使用如下所示:

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

问题是,我可能会调用这个函数一百万次,然后我相信会生成一百万个独特的RGB对象实例,这是非常不吸引人的情况。我对此有一些想法:


  1. 如果被称为无限数字,getPixelRGB可能会创建无限数量的对象的时间,但不一定是无限数量的对象,因为最多可以为RGB生成255 * 255 * 255种可能的组合。所以创建的对象的数量应该是有限的。此函数可以调整为使用对象池,如果它返回相同的颜色,则可以返回该颜色的相同池对象实例。


  2. 我可以将这个RGB编码为一个Int。 Int会比普通的Scala / Java对象拥有更少的内存开销,Java对象会有额外的内存开销。由于Scala Int类型是4个字节宽,所以前3个字节可以存储RGB值。只从getPixelRGB方法返回一个Int而不是RGB将会减少我假设的内存开销。然而,如何做到这一点,同时仍然有RGB类的说服力?

  3. 据说,它们是短命的对象,我读过垃圾收集器应该尽快重新提出索赔。不过我仍然很担心。 GC如何知道我很快把它扔掉?所以一般来说,我的问题是如何使getPixelRGB更友善?也应该我甚至担心它? 您可以用单一长度编码RGB或 int 。而且,在scala 2.10中,你可以为原语定义值类值,例如

      class RGB private(val underlying:Long)extends AnyVal {
    def toTriple = / * decode to (红色,绿色,蓝色)* /
    }
    对象RGB {
    def apply(红色:Int,绿色:Int,蓝色:Int)= / *编码并创建具有新RGB (longvalue)* /
    }

    使用值类,您仍然可以获得类型信息并享受JVM中无类内存布局。


    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?

    解决方案

    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.

    这篇关于如何减少在Scala中创建的对象的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆