Compile-time information across macros
Posted: Sat Aug 15, 2009 5:00 pm
Hi, I am trying to code a set of algebraic algorithms parameterised on structures such as rings and fields. My primary concern is efficiency.
Since algorithms that take higher-order functions representing the operations on the structure would incur a function call overhead, I have been trying to use macros to achieve a similar effect.
Having some experience with C++, I decided to perform something similar to templates in Lisp, but I have been having a problem. In C++, the code generated by templates goes to some sort of "cache" so that duplication in minimized. But I have not found a way to implement this "cache", and that is exactly because I do not know of any mechanism to keep a "compile-time data structure" to be shared among macros.
Put another way, in Paul Graham's book "On Lisp", there is a chapter about shifting computation to compile-time. In that chapter, he teaches how to implement a macro "nth". That macro receives an integer n and a list and returns the n-th smallest element in the list. If the datum passed to the macro as the integer n is in fact an integer and is less than 20, the macro expands into a (huge) specialized code suitable only to the integer n. The problem that is not addressed in the book is that sucessive invocations of the macro with the same integer argument will produce different copies of the same (huge) code. That makes the program larger and can make the instruction cache miss a lot.
If possible, I would like the code to be shared even among different compilation units, just like the C++ compiler does.
What do you recommend that I do?
Since algorithms that take higher-order functions representing the operations on the structure would incur a function call overhead, I have been trying to use macros to achieve a similar effect.
Having some experience with C++, I decided to perform something similar to templates in Lisp, but I have been having a problem. In C++, the code generated by templates goes to some sort of "cache" so that duplication in minimized. But I have not found a way to implement this "cache", and that is exactly because I do not know of any mechanism to keep a "compile-time data structure" to be shared among macros.
Put another way, in Paul Graham's book "On Lisp", there is a chapter about shifting computation to compile-time. In that chapter, he teaches how to implement a macro "nth". That macro receives an integer n and a list and returns the n-th smallest element in the list. If the datum passed to the macro as the integer n is in fact an integer and is less than 20, the macro expands into a (huge) specialized code suitable only to the integer n. The problem that is not addressed in the book is that sucessive invocations of the macro with the same integer argument will produce different copies of the same (huge) code. That makes the program larger and can make the instruction cache miss a lot.
If possible, I would like the code to be shared even among different compilation units, just like the C++ compiler does.
What do you recommend that I do?