2

What is the best way to set up multiple static scopes within the same application? I have structs that serve as wrappers over accessing an array.

Here's an example:

class FooClass{
   static int[] BarArray;
}

struct FooStruct{
    public int BarArrayIndex;

    public int BarArrayValue { 
      get { return FooClass.BarArray[BarArrayIndex]; } 
      set { FooClass.BarArray[BarArrayIndex] = value; }
    }
}

For performance reasons, I don't want to store a reference to BarArray in every instance of FooStruct, hence I declared the array static. However, it's possible that in the future I'll have to work with multiple different BarArrays at the same time (where different instances of the struct should point into different arrays). Is there a way to achieve that without having to store an additional reference in every instance of the structs and not using a static variable neither? If not, what's the best way to use multiple static instances while making the whole application feel as "one application" for the end-user?

user974608
  • 242
  • 1
  • 8
  • 2
    "For performance reasons, I don't want to store a reference to BarArray in every instance of FooStruct" - premature micro optimization. – H H Oct 01 '11 at 16:04
  • I have serious doubts that the extra references will have absolutely any impact on performance. – Yuriy Faktorovich Oct 01 '11 at 16:05

6 Answers6

5

You seem to think that holding a reference to an array means copying the array .. ie that each instance of your struct would contain a copy of the array? This is not the case. All the struct would contain is a reference to the array ... a pointer. There would only exist one instance of the array in memory. I'm not sure this is gaining you any performance points.

iDevForFun
  • 978
  • 6
  • 10
  • Just out of curiosity ... am I right in thinking that on a 64 bit machine a reference to an object (the pointer not the instance) would take up 64 bits of memory? So this is all to save 8 bytes ... in a machine that has memory measured in Gb. I *really* wouldn't bother with this. – iDevForFun Oct 01 '11 at 16:34
  • No, I don't think it will hold a copy of the array, but it'll hold at least an additional 4 bytes of data, which I would like to avoid. There can be several millions of these structures being created and copied concurrently. – user974608 Oct 01 '11 at 16:51
  • At 8 bytes per reference for 1m objects we're talking 8mb of RAM ... tops. I doubt even microsoft are this efficient with memory usage in building the framework? Can I ask what sort of machine this software is going to run on? Any modern pc I can think of would not require this sort of memory efficiency. You could try a quick test with/without the static objects and measure memory footprint. Have you actually measured a performance issue that you're trying to optimise? – iDevForFun Oct 01 '11 at 16:57
  • It's not only memory. Every time I create a new instance or copy it (pass it to a method etc.) it'll add some cpu time overhead as well. That's the main thing I'd like to minimize. I am actually rewriting an application I've written previously because it runs too slow and now I'd like to optimize certain things from the basis as long as they don't cause serious design problems. Trying to see if eliminating the additional array reference would be possible without sacrificing extensiblity. Don't forget, the additional reference can double or triple the size of my structs. – user974608 Oct 01 '11 at 17:02
  • In answer to your original question ... you can't. Static implies one instance in memory and one global pointer to it. Its not possible to substitute instances. Feraref is on the right track .. the only way I can see is implement it as a singleton instead of a static and use an IOC container to inject the instances. The use of an IOC container would require more memory than you are attempting to save tho so I think the best case you have is just to take the memory hit for the references. – iDevForFun Oct 01 '11 at 17:05
  • More info in your last comment ... if its running slow and you're using lots of arrays ... you might find more of a perf benefit in ditching the arrays and using a hash table or dictionary. Arrays aren't the most efficient thing to search. I don't know what it is you're doing with the arrays ... just a suggestion. – iDevForFun Oct 01 '11 at 17:08
  • I don't search the array, it's being sent to the GPU. – user974608 Oct 01 '11 at 17:12
2

You can't. The point of static is to have one instance over the whole application.

Have a look at Dependency Injection instead. It should fulfill your usecase perfectly fine and is the prefered way of handling such a problem.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 1
    I cannot see how dependency injection could fulfill this usecase perfectly (regarding performance). Could you please show it to me? – user974608 Oct 01 '11 at 17:36
2

It's not only memory. Every time I create a new instance or copy it (pass it to a method etc.) it'll add some cpu time overhead as well. That's the main thing I'd like to minimize

Then make them class objects. Then you only have to pass a reference around and you can add a ref to the array without penalty. (And No, using 1M small objects on the heap is not a perfromance issue).

But I seriously doubt that copying small structs was singled out by a profiler. It sounds like you're guessing where the bottleneck is.

H H
  • 263,252
  • 30
  • 330
  • 514
  • If I make them class objects that'll make the performance way worse. If I can get away with the structure only holding an integer (its index in the array), it's all just 4 bytes. Could you people please refer to my original question? I didn't feel like sharing the whole problem, because it would have required a LOT bigger description, and the question I actually asked represents the essence of it. I have my reasons why I need a solution to this exact problem. If I wanted to know how to solve it without using structs, I'd have asked that. – user974608 Oct 01 '11 at 17:21
  • "Us people" get to see a lot of premature and foolish optimization questions here. Just asking to find out if yours has any substance. So far none was detected. But your base question was answered many times, the answer is "No". – H H Oct 01 '11 at 17:29
  • Fine, I'm sorry for getting a bit angry seeing all these answers that weren't relevant to my problem. Next time I'll start with a better specified one. Thank you for answering anyways. I just somehow thought there's some common way to eliminate references like this. However I did receive one answer that might lead to a solution. – user974608 Oct 01 '11 at 17:40
1
static class FooClass{
    private static int[][] barArrays;

    public static int[] getBarArray(int instanceIndex)
    {
        return barArrays[instanceIndex];
    }
}

struct FooStruct{
    private int instanceIndex;
    public int BarArrayIndex;

    public int BarArrayValue { 
        get { return FooClass.getBarArray[instanceIndex][BarArrayIndex]; } 
        set { FooClass.getBarArray[instanceIndex][BarArrayIndex] = value; }
    }
}

This is a generalization of the Singleton pattern.

By the way, the performance penalty for each instance of FooStruct to hold on to a common instance of FooClass is absolutely trivial.

dgvid
  • 26,293
  • 5
  • 40
  • 57
  • Even tho we all have doubt about the likely performance gain vs code complexity and readability ... this is a really neat solution to the original problem of static arrays that can be swapped around. Kudos for cool answer :) – iDevForFun Oct 01 '11 at 16:27
  • What is this good for? Holding a new integer value, instanceIndex, has the same performance penalty as holding a reference to barArray, it just makes the code less useable. – user974608 Oct 01 '11 at 16:53
0

The best you could do at this moment is to add a factory method to the FooClass, responsible for returning an instance of the BarArray.

class FooClass {
   int[] GetBarArray() {
   }
}

For now, implement this method to return a static object. If somewhere in future you decide to change the policy of creating the array, you just reimplement the factory method.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

If you just want to have multiple static Variables use a new AppDomain for every Context.
But i'm not sure if this is the right soultion for your Problem (See other Answers).

EDIT: Tutorials and Helpful
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx
http://www.beansoftware.com/NET-Tutorials/Application-Domain.aspx
I don't understand Application Domains

Community
  • 1
  • 1
oberfreak
  • 1,799
  • 13
  • 20
  • Could you please point me to somewhere where I could see how to create a new AppDomain within an application? – user974608 Oct 01 '11 at 17:17