5 ms·
You can use a NextStep style object pool. try (ObjectPool pool = new ObjectPool()) { ... Foo foo = pool.register(new Foo()); ... Bar bar = po
by sievebrain 11y ago
You can use a NextStep style object pool.
try (ObjectPool pool = new ObjectPool()) {
...
Foo foo = pool.register(new Foo());
...
Bar bar = pool.register(new Bar());
}
When ObjectPool is closed, it closes all its registered objects too.
Note that this would be a lot easier with a language like Scala or Kotlin.
- pavanky 11y agoWe are implementing a numerical library. The problem is with temporary buffers when you are chaining functions. Array C = Array.Add(A, Array.sin(B)) The output C and the output of Array.sin(B) have not been allocated from the object pool.
- sievebrain 10y agoWell for constructs like that, it's what escape analysis is meant to be good at, as those functions would be small and inlineable.
- elcritch 10y agoThis is quite difficult and puts you more into the realm of DSL's. Have you tried adding a fluent Builder interface? It's one of the better ways to handle these types of problems in Java. For example: ``` ArrBuilder = arrs; arrs.add(A, arrs.sin(B)); Array C = arrs.get(); ``` Otherwise if you're willing to do Lombok style AST manipulation you could of course do most anything you wanted, perhaps even proper operator overloading: ``` @Arrays public void test() { Array C = A + sin(B); } ``` (Just be ready for the Java programmers to rebel on you and your "dark arts" ;) )