[PATCH -perfbook 2/2] Redefine line count format at the beginning of VerbatimT env
Akira Yokosawa <[email protected]> Fri, 26 Jun 2026 20:41:56 +0900
| Newsgroups | org.kernel.vger.perfbook |
|---|---|
| Message-ID | <[email protected]> |
Line count format in multi-part listings is customized by
redefining fancyvrb's \theFancyVerbLine macro.
However, its definition is common to Verbatim and its derivative
envs such as VerbatimL, VerbatimN, VerbatimU, etc.
Currently, to make a definition local to a specific snippet,
it needs to be enclosed by "{ ... }" with its counterpart
VerbatimT env, which may be error prone.
Instead, add the definition to the VerbatimT env:
\AtBeginEnvironment{VerbatimT}{%
\renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
, where \myfvline can be defined in front of \begin{VerbatimT}:
\renewcommand{\myfvline}{A\arabic{FancyVerbLine}} % A1, A2, ...
\begin{VerbatimT}
...
\end{VerbatimT}
Signed-off-by: Akira Yokosawa <[email protected]>
---
defer/rcuintro.tex | 24 ++++++------------------
perfbook-lt.tex | 4 ++++
2 files changed, 10 insertions(+), 18 deletions(-)
diff --git a/defer/rcuintro.tex b/defer/rcuintro.tex
index 898cdf5f..46625c67 100644
--- a/defer/rcuintro.tex
+++ b/defer/rcuintro.tex
@@ -71,38 +71,32 @@ that would normally be used in single-threaded code.
\begin{listing}[tb]
\begin{adjustwidth}{10pt}{5pt}
\footnotesize
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{A\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = gp;
do_something_with(p->a);
do_something_with(p->b);
\end{VerbatimT}
-}
Might be transformed to:
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{B\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = gp;
do_something_with(p->a);
p = gp;
do_something_with(p->b);
\end{VerbatimT}
-}
The compiler assumes normal variables do not spontaneously change,
\co{do_something_with()} might use many machine registers, and this
transformation reduces register pressure.
But if some other thread changes \co{gp} between lines~B1 and~B3 of the
transformed code, the values of \co{p->a} and \co{p->b} will be mismatched.
Prevent this by using \co{rcu_dereference()} as follows:
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{C\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = rcu_dereference(gp);
do_something_with(p->a);
do_something_with(p->b);
\end{VerbatimT}
-}
\end{adjustwidth}
\caption{Compilers Can Reload Values}
\label{lst:defer:Compilers Can Reload Values}
@@ -111,25 +105,21 @@ Prevent this by using \co{rcu_dereference()} as follows:
\begin{listing}[tb]
\begin{adjustwidth}{10pt}{5pt}
\footnotesize
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{A\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = malloc(sizeof(*p));
p->a = compute_value();
p->b = 42;
gp = p;
\end{VerbatimT}
-}
Might be transformed to:
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{B\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = malloc(sizeof(*p));
gp = p;
p->a = compute_value();
p->b = 42;
\end{VerbatimT}
-}
The compiler assumes normal variables are not concurrently accessed,
and thus that the order of stores does not matter.
If \co{compute_value()} was inlined, this transformation might produce
@@ -138,15 +128,13 @@ In this example, if some other thread loads \co{gp} immediately after
line~B2 of the transformed code, that thread might see pre-initialization
garbage in \co{p->a} and \co{p->b}.
Prevent this by using \co{rcu_assign_pointer()} as follows:
-{\renewcommand{\theFancyVerbLine}{%
- {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\renewcommand{\myfvline}{C\arabic{FancyVerbLine}}
\begin{VerbatimT}
p = malloc(sizeof(*p));
p->a = compute_value();
p->b = 42;
rcu_assign_pointer(gp, p);
\end{VerbatimT}
-}
\end{adjustwidth}
\caption{Compilers Can Reorder Accesses}
\label{lst:defer:Compilers Can Reorder Accesses}
diff --git a/perfbook-lt.tex b/perfbook-lt.tex
index cb8299a5..f6d6efc2 100644
--- a/perfbook-lt.tex
+++ b/perfbook-lt.tex
@@ -403,6 +403,10 @@
\BeforeBeginEnvironment{VerbatimT}{\vspace*{2pt}}
\AfterEndEnvironment{VerbatimT}{\vspace*{-1pt}}
\AfterEndEnvironment{adjustwidth}{\vspace*{-2pt}}
+% for custom line count format such as A1, B2, etc.
+\newcommand{\myfvline}{\arabic{FancyVerbLine}} % redefine on-the-fly
+\AtBeginEnvironment{VerbatimT}{%
+ \renewcommand{\theFancyVerbLine}{\rmfamily\tiny \myfvline}}
\IfLmttForCode{
\AtBeginEnvironment{verbatim}{\renewcommand{\ttdefault}{lmtt}}
--
2.43.0