Since C# strings are immutable (right?), doesn't using the string.Copy(string) method just use up memory for nothing?<BR><BR>What's a practical example of where it might be useful?
Metasyntactic":1utbw7hf said:Some in research try to argue exactly that. For example, it would be interesting to have a system that, because of program analysis, could determine that the following is waste and just elide it:ShuggyCoUk":1utbw7hf said:By taking your logic to the extreme any data which is technically reachable but the code guarantees it won't touch is garbage. Should the runtime try to deal with that too?
static List<int> values = new List<int>()
...
while (true) {
values.Add(1);
}
Nothing reads from 'Values', so it actually be removed, and all references to it eliminated. Your code would behave the same, just that it would use far less memory.
while(true) {
// Do nothing
}
Funny thing about the analyses that go into this. For a long time, people assumed that very coarse analysis was not very much work, while better ones would be more, so they accepted pretty poor results. It turns out that more precise analysis leads to having less data to work on, so even if you've increased your exponents in a Big-O sense, you may be operating on only 1 element at each step, rather than a large set that leads to useless output. Once you do precise points-to and call-graph analysis (which actually go together in JVM/CLR sorts of environments, because of virtual method invocation), you can substitute implementations for interfaces, and get really useful results.Metasyntactic":1utbw7hf said:Unfortunately, in a complicated system like .Net, waste like this is exceedingly difficult to practively remove. The very nature of the reflective system means that most types of analysis simply cannot know with any reasonable degree of certainty what may or may not be used. Similarly, the use of interfaces almost always rules out these sorts of optimizations as you really don't know what sorts of things may be happening under the covers when an interface call it made (for example .Add on an impl of IList could go mutate somethign else in the system). And, even if you know all the implementations of the interface at compile time, that doesn't help with when instances are dynamically constructed or loaded from some stream of bytes somewhere.
In very constrained languages, or if you constrain .Net enough, you might be able to do some of this. But, in practice, it's exceedingly useless. Far better to just provide diagnostics to the user to guide them toward making smart decisions about what can/can't be optimized away.
static IList<int> values = new List<int>();
static {
values.Add(1);
}
so the values variable is safe to remove.
Sure. But you could also have a system that intelligently handles an infinite loop. For example, the runtime could realize that it would not make any further progress and stop scheduling it for the CPU. A very smart (non-.net) runtime could also just get rid of the thread entirely. It can't happen in .Net since threads are externally observable. i.e. it would change the semantics of the system since the thread wouldn't show up for another thread that expected it to be there.MilleniX":ztks1mmr said:Metasyntactic":ztks1mmr said:Some in research try to argue exactly that. For example, it would be interesting to have a system that, because of program analysis, could determine that the following is waste and just elide it:ShuggyCoUk":ztks1mmr said:By taking your logic to the extreme any data which is technically reachable but the code guarantees it won't touch is garbage. Should the runtime try to deal with that too?
static List<int> values = new List<int>()
...
while (true) {
values.Add(1);
}
Nothing reads from 'Values', so it actually be removed, and all references to it eliminated. Your code would behave the same, just that it would use far less memory.
So this just becomes
right? We can't change that it loops infinitely, just whether it makes the useless infinitely large list in the process?Code:while(true) { // Do nothing }
Sure. MSR is full of papers and implementations where this sort of thing happens. However, in practice you find that it's not actually that helpful. i.e. you get a few percent improvement in runtime at a huge cost in complexity as well as overhead on things like the JIT.Funny thing about the analyses that go into this. For a long time, people assumed that very coarse analysis was not very much work, while better ones would be more, so they accepted pretty poor results. It turns out that more precise analysis leads to having less data to work on, so even if you've increased your exponents in a Big-O sense, you may be operating on only 1 element at each step, rather than a large set that leads to useless output. Once you do precise points-to and call-graph analysis (which actually go together in JVM/CLR sorts of environments, because of virtual method invocation), you can substitute implementations for interfaces, and get really useful results.Metasyntactic":ztks1mmr said:Unfortunately, in a complicated system like .Net, waste like this is exceedingly difficult to practively remove. The very nature of the reflective system means that most types of analysis simply cannot know with any reasonable degree of certainty what may or may not be used. Similarly, the use of interfaces almost always rules out these sorts of optimizations as you really don't know what sorts of things may be happening under the covers when an interface call it made (for example .Add on an impl of IList could go mutate somethign else in the system). And, even if you know all the implementations of the interface at compile time, that doesn't help with when instances are dynamically constructed or loaded from some stream of bytes somewhere.
In very constrained languages, or if you constrain .Net enough, you might be able to do some of this. But, in practice, it's exceedingly useless. Far better to just provide diagnostics to the user to guide them toward making smart decisions about what can/can't be optimized away.
Phil
That example is trivial because it's an example. The general technique of finding isolated portions of a program to which data is written but from which data is never read, if applied to a large program, might find places where data could be optimized away by looking at complex usage patterns across many objects. Furthermore, it may be able to optimize in ways humans simply couldn't. For example, suppose a hash table maintains a length field returned by the size method but size() is never called on a particular hash table. The runtime could remove the length field and any updates to it for that particular instance but not for other hash tables.ShuggyCoUk":3sj18snk said:Sure but in isolation that's an entirely pointless optimization since the code removed was entirely pointless so trivial for a human to do too.
It's optimizations that are tricky for humans to do (or that would reduce in much less readable/maintainable code) that are where you want to spend the effort.
I'm prepared to believe such cases exist but no amount of trivial examples like that are going to convince people it's worthwhile.
Furthermore, it may be able to optimize in ways humans simply couldn't. For example, suppose a hash table maintains a length field returned by the size method but size() is never called on a particular hash table. The runtime could remove the length field and any updates to it for that particular instance but not for other hash tables..
The human might not even know that the hash table maintains a size variable; the only noticeable effect would be performance. Furthermore, I'd rather not have to create a new class for every usage pattern. If the new hash table class were API-incompatible with the old, you could no longer pass that hash table to a method accepting another, or if the new hash table implements an OShuggyCoUk":36d6fzzt said:But this is where a human *could* detect this and use an alternate interface implementation that didn't bother to maintain the size.
The optimizations could be done at compile time.The overhead to the runtime of trying to spot this sort of thing is immense, the bug surface is enormous and if it ever really maters the human can do it.