samedi 11 juillet 2015

Caching Dependencies

I have a small MVC framework of my own with an IoC container that is capable of automatically resolving class dependencies (Auto Resolution).

To do that, it creates a reflection object of the subject class and than a reflection method of the constructor and loops through the parameters of the constructor.

The problem is that i have been told by many that using reflection every time the application runs, can slow it down significantly, and i have also been told by some that it is a bad practice, but that is the only way i can implement the auto resolution feature.

I have been thinking about how to optimize my framework so it won't use reflection every run, and i think that i found a good approach.

Every time a object is requested, i will check if its recent dependencies are stored in the cache, if so, i will fetch them to the object without using reflection, is not, i will use reflection to evaluate the necessary dependencies and then i will cache them.

To check if the dependencies that are in the cache are the recent dependencies of the requested class, i will keep an hash of the class file contents in the cache and compare it to the most recent hash of the class file every run.

That way i can avoid using reflection every run, only in case of modifications, which are seldom in production.

Is it a good approach or should i stay with the reflection every run approach? My main concerns are of whether this approach will help to optimize the app performance or will it just slow it down?

Thanks, Arik





Aucun commentaire:

Enregistrer un commentaire