| | @@ -1949,42 +1949,101 @@ |
| 1949 | 1949 | The bytes are in the target's order (appendix A), as a view's are. |
| 1950 | 1950 | |
| 1951 | 1951 | It is `PTR` and `SIZE` made one checked expression, about 30 lines in |
| 1952 | | -the compiler. It breaks nothing. It also gives "Filling bytes: |
| 1953 | | -`Bytes.fill`", below, every array of words: `Bytes.fill(BYTES(a), 0)`. |
| 1952 | +the compiler. It breaks nothing. An array of words is zeroed by |
| 1953 | +"Filling: `FILL(d, x)`", below, as `FILL(a, 0)`; `BYTES` is for its |
| 1954 | +bytes as such. |
| 1954 | 1955 | |
| 1955 | 1956 | Met in raudio, surco and rcrypto. |
| 1956 | 1957 | |
| 1957 | 1958 | **Recommendation:** take; it replaces an unchecked pointer and a |
| 1958 | 1959 | hand-written length with one checked expression. |
| 1960 | + |
| 1961 | +## Filling: `FILL(d, x)` |
| 1959 | 1962 | |
| 1960 | | -## Filling bytes: `Bytes.fill` |
| 1963 | +**Status.** Proposed 2026-10-22 as `Bytes.fill`, collated from |
| 1964 | +RAZ-GAPS.md; amended 2026-10-23 to a built-in, from rat. |
| 1965 | + |
| 1966 | +`COPY` copies and `NEW` zeroes, but nothing sets the elements of an |
| 1967 | +array or a slice already in hand. rttf clears each glyph's raster a |
| 1968 | +byte at a time (`clear`), its `Pgm` the same for an image, a |
| 1969 | +`Buf.spare` slice is cleared by a loop, and raudio zeroes a `[576]F32` |
| 1970 | +with a loop or a helper. Pascal has `FillChar`, C `memset`. |
| 1971 | + |
| 1972 | +rat fills with a value that isn't zero, on its hottest path: each |
| 1973 | +pixel row of a cell's background is `U32` pixels of the cell's colour, |
| 1974 | +some 4 million pixels for a full redraw of a 3440 by 1440 window. The |
| 1975 | +loop for it was most of `Draw.paint`'s time, itself 81 per cent of |
| 1976 | +rat's on full redraws, as its counter had lost its register to |
| 1977 | +`paint`'s busier variables; moved into a procedure of its own it has |
| 1978 | +one, and rat's time on full redraws fell by 30 per cent. Even then a fill does better: |
| 1979 | +libc's `wmemset`, against the loop with its counter in a register, |
| 1980 | +fills runs of 10 `U32`s in 0.6 of the time, and of 4 million in a |
| 1981 | +third. A `Bytes.fill(s: []U8; b: U8)`, as this proposal first was, |
| 1982 | +can't serve it: a colour's four bytes differ. |
| 1983 | + |
| 1984 | +The proposal: one built-in procedure, a statement, as `COPY` is. |
| 1985 | + |
| 1986 | +| Call | Result | |
| 1987 | +|---|---| |
| 1988 | +| `FILL(d, x)` | sets every element of `d`, an array or a slice of elements of type `T`, to `x`, a value of `T` | |
| 1989 | + |
| 1990 | +``` |
| 1991 | +FILL(w^.band[row..row + w^.cellW], U32(l.bg)); (* a row of pixels *) |
| 1992 | +FILL(window, 0.0); (* window: [576]F32 *) |
| 1993 | +FILL(raster, 0); (* raster: []U8 *) |
| 1994 | +``` |
| 1995 | + |
| 1996 | +`d` is written as `COPY`'s destination is, and `x` is assigned to each |
| 1997 | +element as by `:=`. An element of one, two, four or eight bytes is |
| 1998 | +stored with `rep stos` of that width, or with moves when there are |
| 1999 | +few, as `zeroBytes` does; any other is stored once and copied forward |
| 2000 | +from there. A zero of any type is `rep stosb`, as `NEW`'s zeroing is. |
| 2001 | + |
| 2002 | +It breaks nothing, and leaves `Bytes.fill` out: a slice of bytes is |
| 2003 | +filled as any other. It is not a wipe: an optimizer may leave out a |
| 2004 | +fill that nothing reads after, as it may any write (7.6). Its size in |
| 2005 | +the compiler is not yet measured. |
| 2006 | + |
| 2007 | +Met in rttf, raudio and rat. |
| 2008 | + |
| 2009 | +**Recommendation:** take; one statement for a loop every buffer-handling |
| 2010 | +program writes, as fast as the machine fills, for any element type. |
| 1961 | 2011 | |
| 1962 | | -**Status.** Proposed 2026-10-22, collated from RAZ-GAPS.md. |
| 2012 | +## `COPY` forwards unless it must go back |
| 1963 | 2013 | |
| 1964 | | -`COPY` copies and `NEW` zeroes, but nothing sets the bytes of a slice |
| 1965 | | -already in hand. rttf clears each glyph's raster a byte at a time |
| 1966 | | -(`clear`), its `Pgm` the same for an image, a `Buf.spare` slice is |
| 1967 | | -cleared by a loop, and raudio zeroes a `[576]F32` with a loop or a |
| 1968 | | -helper. Pascal has `FillChar`, C `memset`. |
| 2014 | +**Status.** Proposed 2026-10-23, from rat. |
| 1969 | 2015 | |
| 1970 | | -The proposal: library growth in `Bytes`, beside `index` and `count`. |
| 2016 | +`COPY` is right when its two sides overlap (6.8), and the compiler |
| 2017 | +makes it so by copying backwards whenever the destination is above |
| 2018 | +the source (`Gen.copySlice`): `std`, `rep movsb` down from the last |
| 2019 | +byte, `cld`. That is right, but it goes backwards for every copy to a |
| 2020 | +higher address, overlapping or not, and a backward `rep movsb` gets |
| 2021 | +none of the processor's fast copying: it moves a byte at a time. Ten |
| 2022 | +million copies between the two halves of one `[4096]U32`, never |
| 2023 | +overlapping: |
| 2024 | + |
| 2025 | +| Elements | Down | Up | |
| 2026 | +|---|---|---| |
| 2027 | +| 10 `U32`s | 66 ms | 510 ms | |
| 2028 | +| 1000 `U32`s | 390 ms | 9,800 ms | |
| 1971 | 2029 | |
| 1972 | | - (* Sets every byte of s to b. *) |
| 1973 | | - proc fill*(s: []U8; b: U8); |
| 2030 | +rat meets it drawing a scaled font: each pixel row of a cell is copied |
| 2031 | +to the rows under it, a higher address in the same image, for every |
| 2032 | +cell drawn. |
| 1974 | 2033 | |
| 1975 | | -It writes eight bytes at a time as `Bytes` reads them, and |
| 1976 | | -`Bytes.x86_64.raz` may use `rep stosb`. With `BYTES`, above, an array |
| 1977 | | -of words is zeroed in one statement: `Bytes.fill(BYTES(a), 0)`. A |
| 1978 | | -built-in `FILL(d, x)` for any element type was weighed and left: a |
| 1979 | | -value other than zero for words is rare, and a loop says it. |
| 2034 | +The proposal, the compiler's alone, as the report already says what |
| 2035 | +`COPY` does: copy backwards only when the destination starts inside |
| 2036 | +the source (`rsi < rdi < rsi + rcx`, one more comparison), and |
| 2037 | +forwards otherwise. A backward copy that must be made could go by |
| 2038 | +words, eight bytes at a time down to the last few, rather than by |
| 2039 | +`rep movsb`. |
| 1980 | 2040 | |
| 1981 | | -It breaks nothing. It is not a wipe: an optimizer may leave out a fill |
| 1982 | | -that nothing reads after, as it may any write (7.6). |
| 2041 | +It breaks nothing: no program can tell, but by its time. |
| 1983 | 2042 | |
| 1984 | | -Met in rttf and raudio. |
| 2043 | +Met in rat. |
| 1985 | 2044 | |
| 1986 | | -**Recommendation:** take; one procedure for a loop every buffer-handling |
| 1987 | | -program writes. |
| 2045 | +**Recommendation:** take; one comparison in `copySlice` for a copy up |
| 2046 | +to eight times faster at ten elements and twenty-five at a thousand. |
| 1988 | 2047 | |
| 1989 | 2048 | ## A slice of a `const` parameter, returned |
| 1990 | 2049 | |