[NTG-context] Filling forms

Taco Hoekwater taco at elvenkind.com
Tue Sep 6 16:47:20 CEST 2005

Hi Luigi,

It is hard to benchmark stuff without the actual dataset, but I
have some hints that may help you. (I hope my prose is not too

luigi.scarso wrote:
> Hi, I'm working on a project that require filling forms.
> Every field has  (x,y,width,height) dimension and
> ui=(barcode,texEdit) type (textEdit has some attributes too)

I would have used two separate macros then, one for barcodes
and one for textEdit. More macros that each do less is generally
faster than fewer macros that do more.

If you have 200 fields, don't be scared to define 200 named
macros for them

> \def\Field[#1]{%
> \bgroup
> \getparameters[!!][#1]%% collect key/val

You could use \rawgetparameters in this kind of input. It is a
lot faster and you have complete control over spaces in the
input, so there is no need to use the slower \getparameters.

> %
> %
> %%%% textEdit
> \doifsamestring{\!!ui}{textEdit}{% text field

\doifsamestring is quite slow. A faster solution: most arguments
can only be one or two different things, right? Then you can
predefine a macro for the possible cases, say \dotextEdit and
\dobarcode, and replace most of the body of the \Field command


I would suggest you try to do this pre-defining trick for all things
you can possibly get away with.  String comparisons in macro code
are very expensive compared to the C lookup times for macro names.
Another example:

  \doifsamestring{\!!para at hAlign}{left}{%
    \hbox to \!!w {\getvalue\s!dummy \vphantom{K} \getvalue\!!name \hss}%

is a lot slower than:

  \def\parahAlignleft{% in the preamble
    \hbox to \!!w {\getvalue\s!dummy \vphantom{K} \getvalue\!!name \hss}%

followed by

   \getvalue{parahAlign\!!para at hAlign} % in actual code

What is that \getvalue\s!dummy doing there, btw? I don't see what
purpose it serves.

You can also gain a considerable bit of speed by using an explicit
\hrule with the correct size, instead of \vphantom{K}. Or just put
the dimensions in a global \hbox beforehand:

   % in the preamble
    \hbox to \!!w {\getvalue\s!dummy \copy\Kbox \getvalue\!!name \hss}%

Oh, and grouping is also slow. Why would you bother restoring stuff
if you will overwrite the values before the next use ? There is no
need for \bgroup ...\egroup around the pages if every page defines
every field anyway.

I'm quite confident that a near-optimal solution will look like this:

	... lots and lots of macro defs,
          %% Some 200 \getvalue  calls

Final trick: when in doubt, run TeX with

   \tracingall \tracingonline=0

in the preable, on an input file of just a few pages. The general rule
of thumb is: the smaller log file, the faster the 'real' run will be.

I hope this helps,


More information about the ntg-context mailing list