Skip to main content
27 events
when toggle format what by license comment
Nov 28, 2018 at 15:15 vote accept Milack27
Nov 30, 2017 at 0:34 answer added user204677 timeline score: 2
Nov 6, 2017 at 15:36 comment added Doc Brown @Milack27: yes, "flyweight" means what amon suggested, and yes, I missed that part of the comment initially. FWIW, maybe it is useful for you to know that approach has a broadly accepted name.
Nov 1, 2017 at 12:54 history tweeted twitter.com/StackSoftEng/status/925707566939811840
Oct 28, 2017 at 1:44 comment added Milack27 @rwong Yes, that's an interesting approach. I think it is somewhat similar to passing the master as parameter to the methods in the slave. The class that invokes the slaves is the same that knows where the master is.
Oct 28, 2017 at 1:10 comment added rwong Consider an array of slaves! (or vector of slaves.) No pun intended, or maybe. Extract slave methods into a new class, one which processes an entire array of slaves at a time. Then this method will be told (injected) where to find the master.
Oct 27, 2017 at 15:57 comment added Berin Loritsch That's less than the 64-bit pointer I was thinking :) The only way you could optimize it (i.e. use less than 32 bits for the reference) would be to customize how references work and memory is used in your application. The error-prone hacks would probably introduce more instability than it is worth.
Oct 27, 2017 at 15:50 answer added Berin Loritsch timeline score: 4
Oct 27, 2017 at 15:43 comment added Milack27 @BerinLoritsch Actually, I was thinking in a scenario where the slave is really small, say 20 bytes, and there are lots of them. In this case, 32-bit pointers would take 20% of the total memory used by the slaves. I understand that it takes millions of slaves to make a considerable difference, but still, it would be nice to optimize it.
Oct 27, 2017 at 15:29 comment added Berin Loritsch Working with C# and Java applications, this style of development is used very commonly. Objects in those languages are passed by reference, and the only time we run into memory problems is when we forget to stop referencing an object. I agree with @CandiedOrange about premature optimization. If you have memory problems it will be more likely that your slaves consume too much memory rather than the 8 bytes it takes to reference the singleton.
Oct 27, 2017 at 14:07 comment added Milack27 @BerinLoritsch No, all of this is just hypothetical. I don't have an actual problem with memory constraints, I just would like to know if there's a way of optimizing memory usage without losing code maintainability.
Oct 27, 2017 at 13:36 comment added Berin Loritsch Is this embedded design where memory is really constrained? Are the pointers really going to cause that much of a memory problem for your application?
Oct 27, 2017 at 13:03 comment added Milack27 I may have misunderstood it, but applying the flyweight pattern in this case would be equivalent to amon's suggestion: pass a reference to the master (or a part of it) as a parameter for each relevant method of the slave.
Oct 27, 2017 at 12:46 comment added Paul Kertscher @DocBrown Okay, after re-reading the question I think I've got it.
Oct 27, 2017 at 12:43 comment added Paul Kertscher @DocBrown I'm not sure if the fylweight pattern would do here, since it does not solves the problem of the "duplicated" references. Still each slave will have to hold a reference, either to the shared data (which is the pointer to the master here) or to the master itself.
Oct 27, 2017 at 2:31 comment added Milack27 @DocBrown Don't worry, this is not bothering me about a specific application. I'm just curious. :)
Oct 27, 2017 at 2:28 comment added Doc Brown Honestly, IMHO you are overthinking this. But if this ever gets a real problem over the next years, try the flyweight pattern from the GOF book.
Oct 27, 2017 at 2:22 comment added Milack27 @DocBrown I think you understood me right, the memory waste that I'm talking about is beacause of the pointer in each slave. I'm aware that it is not a problem for most applications, it's a premature optimization, as CandiedOrange said. But still, it seems suboptimal to me.
Oct 27, 2017 at 2:11 comment added Doc Brown "a lot of memory is wasted in pointers that point to the same thing"- for me it looks as if there is almost no memory is "wasted", since every slave holds only a pointer, which has only a low memory profile. Unless you have millions of slave objects, it is unlikely to be a problem. Please clarify.
Oct 27, 2017 at 2:10 history edited Milack27 CC BY-SA 3.0
added 3 characters in body
Oct 27, 2017 at 2:05 comment added Milack27 @SebastianRedl Actually, this code is just an example. I usually implement DI in my C++ applications, and I'm kind of happy with it. But this memory thing makes me think if there is a better way.
Oct 26, 2017 at 23:20 answer added candied_orange timeline score: 12
Oct 26, 2017 at 22:15 comment added Sebastian Redl Have you actually determined that the memory usage of those references is an issue in your application?
Oct 26, 2017 at 22:09 comment added amon The static Master* master effectively is a singleton: it's a global variable. If you don't want to store the master reference in each instance, you will have to pass it to all relevant methods as a parameter.
Oct 26, 2017 at 21:02 review Close votes
Nov 1, 2017 at 3:01
Oct 26, 2017 at 20:47 review First posts
Oct 27, 2017 at 12:02
Oct 26, 2017 at 20:45 history asked Milack27 CC BY-SA 3.0