Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Here's an example of some code from LaTeX's \DeclareFontFamily:

\def\reserved@a{#3}
\global
\expandafter\let\csname #1+#2\expandafter\endcsname
       \ifx \reserved@a\@empty
         \@empty
       \else \reserved@a
       \fi

I could be wrong, but to me this looks identical to:

\expandafter\gdef\csname#1+#2\endcsname{#3}

Now, the source for this code comments:

We compare \reserved@a with \@empty If these two are the same we \let the ‘extra’ macro equal to \@empty which is not the same a doing a \let to \reserved@a — the latter would blow one extra memory location rather then reusing the one from \@empty.

But if we're not re-using the contents of \reserved@a after this point (which we're not, as far as I can see), there's no reason to go this roundabout route, right?

share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

For a full answer you'd need to ask one of the people who might have written this (possible Frank Mittelbach or even Leslie Lamport - I wonder if this has come all the way from 2.09). However, what I think is happening is that when \reserved@a is not equal to \@empty then there is no saving, as at some later stage \reserved@a will be needed again. On the other hand, when \reserved@a is equal to \@empty then a memory position is saved, as \@empty never changes and so the two macros always have the same definition as each other. When this code was written, such small savings were probably really important (they all add up).

share|improve this answer
1  
Do I read that correctly that TeX uses some form of copy-on-write with macros defined using \let? This is very interesting. –  Martin Scharrer Feb 23 '11 at 9:37
5  
@Martin: Macros \let to each other have only one definition in memory (the hash table entries point to the same definition), whereas \def creates independent definitions even if the two are identical. –  Joseph Wright Feb 23 '11 at 10:06
    
I think I now have an alternative explanation: \def\a{}\def\b{} takes more (or twice as much) memory as \def\a{}\let\b\a. (I still think \reserved@a isn't reused.) Hence my first suggestion wouldn't be a good idea (back then when memory was scarce). WDYT? More importantly, does it matter these days? –  Will Robertson Feb 23 '11 at 13:33
    
@Will: That's what I've tried to say - \reserved@a isn't the key here, it's the \@empty branch that does the saving. I'd say today it doesn't matter at all: one of the problems with the current kernel is that these tricks make maintenance much harder. –  Joseph Wright Feb 23 '11 at 13:37
    
@Will. Isn't \reserved@a used in e.g. environments, and everywhere in LaTeX? –  Bruno Le Floch Feb 23 '11 at 13:41
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.