mercoledì 20 settembre 2023

[English] DMVCFramework Gotcha #1: Object Pool explained

 One aspect related to the performance of an application that is often overlooked is the overhead in the execution of specific tasks. Overhead is influenced by various factors. One of these factors, in the context of OOP (Object-Oriented Programming), is the time it takes to create an object: when we create an object, the process of creation and initialization can be more or less time-consuming depending on the complexity of the object and the initialization process of its attributes. The repeated creation and destruction of this object negatively impact the runtime of both the individual process and the entire application as a whole.

One possible solution to this problem would be to have pre-created objects available, for which we do not have to worry about their lifecycle.

One of the Design Patterns belonging to the category of Creational Design Patterns is the Object Pool. According to its definition:

The Object Pool is a creational design pattern, which is one of those patterns applicable to resolving performance issues related to the initialization process of the components (classes) that make up our system.

The underlying concept of the Object Pool can be summarized by describing a process in which a client, in need of an instance of a class, requests it from the Pool. The Pool, upon the request, 'checks' if it has an instance of that class available to fulfill the request. If the instance is available in the pool, it is returned; if the instance is not available, the Object Pool, which knows the method for creating the instance, creates a new one and returns it.

When the client finishes using the obtained instance, it returns that instance to the Pool, which does not destroy it but makes it available for future requests.

This process is cyclical and, in the best case scenario, helps reduce the overhead derived from creating an instance when it is already present in the pool.

DMVCFramework, the RESTful Framework, JSON-RPC, and ActiveRecord for Delphi maintained by Daniele Teti and the DMVCFramework team, has many small but important features that may be lesser-known. One of these is the implementation of the ObjectPool.

The unit MVCFramework.ObjectPool.pas, present in DMVCFramework, defines a class that implements the Object Pool as the pattern defines it:

```delphi

TObjectPool<T: class, constructor> = class(TInterfacedObject, IObjectPool<T>)

```

The (generic) class TObjectPool implements the IObjectPool<T: class, constructor> interface. IObjectPool requires the implementation of two specific methods for pooling:

```delphi

function GetFromPool(const RaiseExceptionIfNotAvailable: Boolean = False): T;

procedure ReleaseToPool(const Obj: T);

```

`GetFromPool()` is called to obtain an object from the Pool, and `ReleaseToPool()` is called to release and return the object instance to the Pool.

An instance of the TObjectPool class is obtained by invoking the class function `CreatePool()` of the TPoolFactory class, defined as follows:

Note the `Factory: TFunc<T>` parameter of `CreatePool()`: it represents the constructor of the class that specializes the pool. It will be used by the pool when it receives a request and no instances are available.

At this point, I will show in the following code an example of how this is applied:

1) Define a pool for the TClientDataset class:

           2) Utilizziamo l'istanza:

With these simple instructions, we have delegated the creation and management of the lifecycle of our objects to the Pool.

To complete this introduction, I would like to draw attention to two features of the Object Pool present in DMVCFramework:

- The Pool has a size: POOL_SIZE indicates the maximum number of instances maintained in the pool (however, instances consume memory!)

- A thread is implemented; it periodically cleans the pool of instances based on the configured Shrink values.

To conclude, it's worth noting that there is also the unit MVCFramework.IntfObjectPool, which similarly implements an ObjectPool for interfaces. 


#codinglikeacoder

sabato 16 settembre 2023

Lezione della settimana #1

 In questo sabato mattino, ho ancora in testa il progetto al quale sto lavorando. L'averlo ancora in testa è per me sintomo del fatto che non è ancora terminato e che quel che c'è da fare non è completamente definito nel mio cervellino.

Una riflessione l'ho fatta anche su ciò che questa settimana mi ha 'lasciato'.

Questa settimana ho imparato una (nuova, forse saputa e risaputa ma spesso dimenticata) lezioncina:

"basa quel che fai su ciò che è e non su ciò che potrebbe essere"

Sipario.

#codinglikeacoder

 

[EN] ITDevCon bites 2024

 ITDevCon Bites! 📌 Tuesday 8 October, 2.00 pm - Milan c/o Estensya, Via San Gregorio 55 💥 FREE ITDevCon Bites is a short, half-day edition...