
(4. Context-Oriented Optimization) ;; Project Description and Current Results
The use of context orientation for optimization purposes constitutes one exploratory
path in this more general agenda.
In order to ground the research into a realistic application domain, we chose the
field of image processing, as it is already an area of expertise in our lab (Levillain
et al., 2010). To that aim, we started by developing a first prototype, called Climb,
the Common Lisp Image Manipulation Bundle (Senta et al., 2012)
1
. Although still
1
Senta, L., Chedeau, C.,
and Verna, D. (2012).
Generic image processing
with Climb. In ELS’12,
5th European Lisp
Symposium, Zadar,
Croatia.
experimental, the library not only provides an extensible core of data structures and
algorithms, but also a DSL and a graphical modeling language, in order to ease the
writing of complex processing chains.
The idea behind genericity is to be able to write algorithms only once, indepen-
dently of the data types to which they may be applied. In the context of image
processing, being fully generic means being independent from the image formats,
pixel types, storage schemes etc. To this aim, Climb provides different abstractions,
among which are the following. A site is an abstract view of a pixel location. Image
sites may be 2, 3, or nD coordinates, and the grids need not be rectangular (for ex-
ample, some images types have hexagonal grids). A site set represents a collection of
sites (a whole image, a neighborhood, etc.) over which it is possible to iterate. Climb
also has abstract pixel values such as Boolean (for black and white images or masks),
RGB, RGBA, etc. Values can in turn be encoded in different forms (integers, floats,
etc.). Finally, image processing algorithms can be written on top of a core of abstract
primitives such as site iterators, generic value accessors etc.
This very high level of abstraction makes it trivial to work on peculiar kinds of
images such as graph-based ones, that is, images where the pixel adjacency relation
is represented by a graph instead of a grid. Graph nodes are just a special kind of
site, site iterators and value accessors work on them out of the box, so that any image
processing algorithm written in Climb will also work on graph-based images with no
modification to the code.
In Verna and Ripault (2015)
2
, we added support for context-oriented programming
2
Verna, D. and Ripault, F.
(2015). Context-oriented
image processing. In
COP’15,
Context-Oriented
Programming Workshop.
in Climb, and we demonstrated how this paradigm could be used, in a somewhat
counter-intuitive manner, to optimize the algorithms otherwise written in a highly
generic fashion. The general idea is that instead of cluttering the design with a
proliferation of classes and hierarchies for every single type of grid, site, or value,
contextual layers will automatically switch the object-oriented model according the
characteristics of the image currently being processed. Two examples of contextual
optimization are given below, one behavioral, one structural.
One example of behavioral optimization lies in the handling of static type anno-
tations in Clos. Instead of providing a whole hierarchy of say, RGB values, one for
each kind of value type (e.g. fixnum, float, etc.), it becomes possible to provide a
single, layered, RGB value class, each layer adding the appropriate type annotation to
the slots. In a similar fashion, all methods performing arithmetic operations on these
slots can in turn be equally layered and annotated, so that with full optimization, we
end up with fully dedicated, static code instead of generic one.
One example of structural optimization lies in the storage scheme for pixel values.
In the fully generic version, nD images are stored in n-dimensional arrays of values,
the values being in turn instances of different classes, such as the aforementioned RGB
one. This abstract representation is not efficient, as a lot of indirections and accessor
calls are involved for pixel access. Suppose, however, that we know in advance that
the image is a 3D rectangular grid of RGB fixnum values. A much more compact and
iteration-friendly storage scheme is to use a single 1D vector of inlined fixnum triplets
16 (dynamic (programming paradigms)) ;; performance and expressivity