views:

116

answers:

3

I'm reading about program specialization - specifically java and I don't think I quite understand it to be honest. So far what I understand is that it is a method for optimizing efficiency of programs by constraining parameters or inputs? How is that actually done? Can someone maybe explain to me how it helps, and maybe an example of what it actually does and how its done?

Thanks

I have been reading:

Program Specialization - java

A: 

Program specialization is the process of specializing a program when you know in advance what are the arguments you're going to have.

One example is if you have a test and you know that with your arguments, you're never going to enter the block, you can eliminate the test.

You create a specialized version of the program for a certain kind of input.

Basically, it helps to get rid off useless with your input. However, with the modern architectures and compilers (at least in C), you're not going to win a lot in terms of performance.

From the same authors, i would recommend the Tempo work.

EDIT

From the Toplas paper:

Program specialization is a program transformation technique that optimizes a pro- gram fragment with respect to information about a context in which it is used, by generating an implementation dedicated to this usage context. One approach to automatic program specialization is partial evaluation, which performs aggressive inter-procedural constant propagation of values of all data types, and performs constant folding and control-flow simplifications based on this information [Jones et al. 1993]. Partial evaluation thus adapts a program to known (static) informa- tion about its execution context, as supplied by the user (the programmer). Only the program parts controlled by unknown (dynamic) data are reconstructed. Par- tial evaluation has been extensively investigated for functional languages [Bondorf 1990; Consel 1993], logic languages [Lloyd and Shepherdson 1991], and imperative languages [Andersen 1994; Baier et al. 1994; Consel et al. 1996].

LB
I don't think you're quite right - the example I read didn't remove "useless" code, it just provided a faster path for a given input.
Adrian Mouat
control flow specialization is that, or when you have a loop that is going to iterate only once, you remove the loop and execute the block only once. the other typical example is specializing the pow(n), functions into square() or cube().
LB
A: 

Interesting.

It's not a very common term, at least I haven't come across it before.

I don't have time to read the whole paper, but it seems to refer to the potential to optimise a program depending on the context in which it will be run. An example in the paper shows an abstract "power" operation being optimised through adding a hard-coded "cube" operation. These optimisations can be done automatically, or may require programmer "hints".

It's probably worth pointing out that specialization isn't specific to Java, although the paper you link to describes "JSpec", a Java code specializer.

Adrian Mouat
A: 

It looks like Partial Evaluation applied to Java.

That idea is if you have a general function F(A,B) having two parameters A and B, and (just suppose) every time it is called, A is always the same. Then you could transform F(A,B) into a new function FA(B) that only takes one parameter, B. This function should be faster because it is not having to process the information in A - it already "knows" it. It can also be smaller, for the same reason.

This is closely related to code generation.

In code generation, you write a code generator G to take input A and write the small, fast specialized function FA. G(A) -> FA. In specialization, you need three things, the general program F, the specializer S, and the input A: S(F,A) -> FA.

I think it's a case of divide-and-conquer. In code generation, you only have to write G(A), which is simple because it only has to consider all As, while the generated program considers all the Bs. In Partial Evaluation, you have to get an S somewhere, and you have to write F(A,B) which is more difficult because it has to consider the cross product of all possible As and Bs.

In personal experience, a program F(A,B) had to be written to bridge real-time changes from an older hierarchical database to a newer relational one. A was the meta-description of how to map the old database to the new, in the form of another database. B was the changes being made to the original database, and F(A,B) computed the corresponding changes to the newer database. Since A changed at low frequency (weekly), F(A,B) did not have to be written. Instead a generator G(A) was written (in C) to generate FA(B) (in C). Time saved was roughly an order of magnitude of development time, and two orders of magnitude of run time.

Mike Dunlavey