man apl/outer-product

The outer product: APL's most elegant way to waste memory

lib / APL 9 mindraft

checked against: GNU APL 2.0

How much memory does an outer product take? In APL, ∘.= compares every item of one list with every item of another and returns the whole table of answers. APL code often uses it to count: +/(∪X)∘.=X gives how often each distinct value of X occurs, in one line and without a loop. I wanted to know what that line costs in GNU APL 2.0, “an interpreter for APL as described in ISO standard 13751” in its manual’s words, so I measured it and read the interpreter’s source.

All experiments ran on an Apple M2 Pro with 16 GB of memory, macOS 27.2.1, GNU APL 2.0 from Homebrew. Sources are from the apl-2.0 tag of the GNU APL Subversion repository; the 16 files I compared are identical to the ones in the release tarball.

The short version

  • (∪X)∘.=X builds a k × n table, k distinct values by n items. GNU APL stores every element of every array in a 24-byte cell, 0s and 1s included, so the table costs 24 · k · n bytes.
  • For 100,000 values with 43,282 distinct ones that is 104 GB. GNU APL checks the size before it allocates anything and stops with WS FULL.
  • On macOS GNU APL can’t read /proc/meminfo, so it assumes 3 GB of memory and lets values use 90% of that. Any outer product above about 120 million elements fails, whatever the machine has.
  • Comparing one distinct value at a time with {+/⍵=X}¨∪X needs one row of memory and the same k · n comparisons. Sorting first and measuring runs of equal values needs neither.

What the outer product computes

A∘.f B applies f to every pair of an item of A and an item of B. With = and the distinct values on the left, each row of the table marks where one value occurs:

      X←3 1 3 0 3 1
      ∪X
3 1 0
      (∪X)∘.=X
1 0 1 0 1 0
0 1 0 0 0 1
0 0 0 1 0 0
      +/(∪X)∘.=X
3 2 1

+/ adds along the last axis, so each row sum is a count. The order of the operands matters. +/X∘.=∪X builds the transposed table, 6 × 3, and its row sums are all 1: each item of X equals exactly one distinct value.

      +/X∘.=∪X
1 1 1 1 1 1

Every element is 24 bytes

GNU APL keeps an array as a Value: a header with the shape and bookkeeping, followed by the elements, which it calls the ravel. Each element is a Cell, a small C++ object. Cell.hh gives it one data member, a union of everything a cell can hold: a 64-bit integer, a double, two doubles for a complex number, a character, or a pointer to a nested value. The class has virtual functions, so every cell also carries a vtable pointer. That makes 8 bytes of pointer and 16 of union. apl --cfg prints the sizes for the build:

$ apl --cfg 2>&1 | grep sizeof
        sizeof(Value)       : 464 bytes
        sizeof(Cell)        :  24 bytes
        sizeof(Value header): 176 bytes

The 464 bytes of a Value are the 176-byte header plus room for 12 cells inside the object, so a list of up to 12 elements needs no second allocation. Anything longer gets its ravel from new char[length * sizeof(Cell)] (Value.cc, line 142).

The interpreter keeps a running total of ravel bytes, and ⎕SYL (system limits) shows it, so the 24 bytes can be checked from APL:

      ⎕SYL[5 6;]
 current number of values    152
 current total ravel bytes 14352
      Q←1000000⍴1
      ⎕SYL[5 6;]
 current number of values       153
 current total ravel bytes 24014352

A million 1s cost exactly 24,000,000 bytes. Comparisons don’t produce anything smaller. Cell::bif_equal writes its result with IntCell::zI, an integer cell (Cell.cc, line 264). A table of 0s and 1s takes as much room as a table of 64-bit integers.

So the counting table costs 24 · k · n bytes, and the same check works on a real table:

      ⍴∪X
4318
      ⎕SYL[6;]
 current total ravel bytes 254352
      M←(∪X)∘.=X
      ⍴M
4318 10000
      ⎕SYL[6;]
 current total ravel bytes 1036574352

4,318 × 10,000 × 24 = 1,036,320,000, the difference to the byte.

How the table is filled

Bif_OPER2_OUTER::eval_ALRB allocates the whole result first (line 152). If the function is a scalar function like = and both arguments are flat, it then runs PF_scalar_outer_product. For each result position z it divides z by the length of the right argument to find the row, takes the remainder as the column, and calls the cell’s comparison through a member-function pointer (lines 295 to 301). There is a parallel branch that splits the table between cores, but the Homebrew build is configured with CORE_COUNT_WANTED=0, sequential, according to apl --cfg.

+/ then reads the finished table and writes a list of k sums. The table is never needed again, but it exists in full until the reduction is done.

Measured

I ran +/U∘.=X (with U←∪X, then grading the counts) on lists of random integers, n values drawn from n/2 distinct ones, and read the peak memory footprint from /usr/bin/time -l:

nk distinct24 · k · npeak footprinttime (best of 3)
5,0002,185262 MB268 MB0.18 s
10,0004,3181,036 MB1,045 MB0.69 s
15,0006,4882,336 MB2,349 MB1.58 s
20,0008,5934,124 MB14 MBWS FULL, 0.02 s

The footprint is 24 · k · n plus 6 to 13 MB for the interpreter, the input and the other lists. An empty script peaks at 4.4 MB. The 20,000-value run fails in 0.02 s with a footprint of 14 MB, because the table was never allocated.

Why WS FULL comes so early

A new value is created through the Value_P constructor, which calls Value::check_WS_FULL before allocating (Value_P.icc, line 244). That function adds up the cells already in use, the requested cells and the value headers, and compares the total with total_memory times a scale of 90% (Value.cc, lines 196 to 205).

total_memory comes from Quad_WA::get_free_memory. On Linux it reads MemAvailable from /proc/meminfo. If that fails, it returns 0xC0000000, with the comment “assume ~3 GB on error” (Quad_WA.cc, line 174). macOS has no /proc, so every GNU APL session on a Mac believes it has 3 GB, whatever the hardware. ⎕WA, the free workspace, says so:

      ⎕WA
3221149672

90% of 3,221,225,472 bytes is 2,899,102,924 bytes, room for 120,795,955 cells if nothing else were in use. With the interpreter’s own values counted, the line falls between 120,770,000 and 120,780,000 cells:

      M←(⍳10000)∘.=⍳12077
      ⍴M
10000 12077
      M←(⍳10000)∘.=⍳12078
WS FULL+
      M←(⍳10000)∘.=⍳12078
        ^          ^

The GNU APL option for setting memory explicitly, --mem, reads /proc/meminfo too, and exits when it can’t:

$ apl --mem 8G --script --noSV -f wa.apl
problem with the --mem option: no readable /proc/meminfo: No such file or directory
$ echo $?
3

The ceiling has one upside. The 104 GB table on 100,000 values doesn’t drive the Mac into swap. It fails at once:

$ cat timing.txt | /usr/bin/time -l apl --script --noSV -f outer.apl 2>&1 \
    | grep -E 'WS FULL|real|footprint'
WS FULL+
        0.07 real         0.06 user         0.00 sys
            52953592  peak memory footprint

Two ways around the table

The table holds k · n answers, but the counts are only k numbers. The first fix keeps the comparisons and drops the table. It compares one distinct value at a time and sums right away.

C←{+/⍵=X}¨U

{...} is a lambda, ⍵ its argument, and ¨ (Each) applies it to each distinct value. Each call builds one row of n cells, sums it and frees it. On the 100,000-value list it ran to the end with a peak footprint of 55 MB and took 79.7 s for all 4,328,200,000 comparisons, one cell object at a time.

The second fix changes the algorithm. Sorted, equal values sit next to each other, and each count is the length of a run:

S←X[⍋X]
P←⍸1,(1↓S)≠¯1↓S
C←(1↓P,1+⍴S)-P

⍋X (Grade Up) gives the positions that sort X, (1↓S)≠¯1↓S compares each element with the one before it, ⍸ lists the positions where a run starts, and subtracting neighbouring start positions gives the run lengths. That is a sort plus a few passes, O(n log n). Same list: 0.10 s and 56 MB for the whole script.

methodmemory beyond the inputcomparisons100,000 values
+/U∘.=X24 · k · n bytesk · nWS FULL
{+/⍵=X}¨U24 · n bytes at a timek · n79.7 s
sort, run lengthsa few lists of nn log n0.10 s

Curious facts

  • In GNU APL, A∘B, a jot with values on both sides, is the matrix product. Bif_JOT::eval_AB computes A+.×B (Bif_OPER2_OUTER.cc, lines 36 to 140). 1 2∘3 4 is 11.
  • The outer product checks for WS FULL before it computes a single comparison, so the failing case is the fastest one: 0.07 s on 100,000 values, against 0.10 s for the version that works.
  • The allocation code in Value.cc still carries a commented-out block that checked whether new returned 0 or threw. The live code only calls new; the check in Value_P runs before it.
  • On a build with 32-bit pointers, Value.cc refuses any value longer than 2,000,000,000 / 24 cells, about 83 million, because new could return a pointer to memory that later faults (line 115).

History

  • 2000: the gzip header of the ISO 13751 draft that the GNU APL manual links to, is13751.pdf, is dated 9 June 2000.
  • October 2013: the newest files in the apl-1.0 tarball are dated 13 October 2013, the same date as its gzip header. Its manual already describes GNU APL as an implementation of ISO 13751.
  • March 2026: the newest files in the apl-2.0 tarball are dated 15 March 2026.

Sources

GNU APL 2.0, Subversion tag apl-2.0 (identical to apl-2.0.tar.gz, sha256 24bbb744fce47e62837234a053bdeecee51b9ea61c82c79f7cc191bc6a54c0a1). Links go to raw files; line numbers are given in the text.

ls apl/

  • draft How GNU APL sorts and stores arraysInside GNU APL 2.0: 24-byte cells, a value header with room for 12 of them, integers that turn into floats at 9.2 × 10¹⁸, and a heapsort behind ⍋, ⍒, ∪ and ⍳.