scala-way for managing object pools
Asked Answered
E

1

8

What is the preferred way in scala for managing object pools?

I need to create and delete large scale of objects single-threaded (no need for synchronization) . In c++ I've used array of static objects.

What is idiomatic and effective way to cope with it in scala?

Earthquake answered 25/9, 2012 at 2:27 Comment(3)
the idiomatic way is to let the gc do its work and not worry about it. If that isn't fast enough, the idiomatic thing to do is to tune the GC.Nunci
Why do you think you need an object pool ? Have you tried relying on GC as a first attempt ? Is the cost to initialized/reset pooled object high ?Cumbrance
I'm writing a particle system. A particle lives fast and dies young. In fact there are plenty of particles doing so. The most prominent way for coping with it seems to create an object pool, since all particles are the same in terms of properties and fields.Earthquake
T
5

I would wrap it in an Actor. If you are not familiar, check out Akka: http://doc.akka.io/docs/akka/2.0.3/scala/actors.html

This is a pretty good example: https://github.com/derekjw/fyrie-redis/blob/master/src/main/scala/net/fyrie/redis/ConnectionPool.scala

The actor model allows you to guarantee single-threaded access because actors process incoming messages one at a time. This leads to very simple code inside the actor and a very simple API.

Tisbee answered 25/9, 2012 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.