I understand the reason for using a parameter object when it comes to handling methods with long parameter lists. But I have a situation where I would like to reduce the memory footprint of my code.
The question I have is from a performance perspective. How much benefit would I get by going to a long parameter list?
The use case is that these methods will end up being called thousands of times and every time a call is made, a new parameter object is created. I want to really avoid creating all those parameter objects.
Is there a "I can have my cake and eat it too" solution of encapsulating the parameters and only using stack storage?
Unfortunately there are multiple methods in the call chain needing all those parameters, and I don't mind making this change since these methods are not public. Just wanted to know of a better performing solution.
Making this change will impact a large portion of the code and will take a lot of my time. Just want to check if this is a worthwhile change to make before I take this route.
Data structure
The data structure is an object graph. The objects are the business data and the edges connecting them are the calls. These edges are associated with the parameter data. Using parameter objects is beautiful and my code works very well. But I am not satisfied with the performance and want to improve it.
I have profiled my code and these objects are one significant part of the memory overhead. I doubt if they contribute much to the CPU overhead, but am definitely concerned about memory.
This data structure will be part of a framework that will be used in a highly concurrent environment. So reducing memory footprint is of utmost concern to me.
The parameter object has about 10 fields and some behavior associated with it. I can always encapsulate the behavior alternatively.
Depends on your situation. The chief issues with object creation (in resource-lacking environments) are memory consumption, and object creation time.
In most cases, object creation time is a snap if you don't have numerous nested objects. For memory cost, count up the number of primitives and multiply them by their respective memory cost, then add eight bytes for the object itself. (And of course do this recursively for any contained objects.)
For the case of Strings, it's roughly (40 + 2*character count) bytes. This is obviously subject to change, but not dramatically. For basic arrays, multiply the primitive cost by the capacity, then add sixteen.
O-notation of creation is basically the O-notation of your constructor. I'm sure there's a little overhead, but it's nothing significant. For a simple parameter object, it's probably inconsequential.
Not to crowd your schedule, but the best possible approach, if efficiency is that important and the answer isn't obvious, is to run benchmarks on both approaches.
As I said, I don't know your exact situation. However, I've solved similar problems in the past with intelligent deviations on the Factory pattern. However, that would require yet another class, and it sounds like that is an issue for you.