| | @@ -82,6 +82,19 @@ |
| 82 | 82 | the budget; and, across modules, the imported module's |
| 83 | 83 | text kept to be read again, which is to be checked. |
| 84 | 84 | |
| 85 | +More cases, from sauce (`~/r/sauce`, RAZ-NOTES.md). Its SHA-256, with |
| 86 | +the rotation a procedure, `return (x shr n) | (x shl (32 - n))`, and |
| 87 | +each word read by `Str.be`, ran at 52 MB/s; with both written out in |
| 88 | +the loop, 115 MB/s, where FPC `-O2` does about 200 and `check off` |
| 89 | +adds only 9%. Most of the gap is calls. `ror` is the narrow case |
| 90 | +above; `Str.be` is not, a loop over its bytes, and would want a |
| 91 | +`Str.be32` of four bytes that only returns, or the loop unrolled where |
| 92 | +it is. A rotate of the language's own, an operator or a built-in |
| 93 | +procedure, would serve SHA, the other hashes and checksums without |
| 94 | +any inlining, but it is a language change, for a thaw. On 100 MB of |
| 95 | +new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7, |
| 96 | +and `verify` 0.95 s against 0.48: the hashing. |
| 97 | + |
| 85 | 98 | ## A slice that outlives what it points at |
| 86 | 99 | |
| 87 | 100 | ``` |
| | @@ -114,3 +127,38 @@ |
| 114 | 127 | checks the type of `return`'s expression. |
| 115 | 128 | |
| 116 | 129 | Met in sauce's `Ssha.short`, which takes `var h: Hex` now. |
| 130 | + |
| 131 | +## `Vec(T)`, a growable array |
| 132 | + |
| 133 | +The library has `List`, linked, and `Map`, a hash; nothing that grows |
| 134 | +in place and can be indexed. A program that keeps what it learns the |
| 135 | +length of as it reads (lines, entries, changes) writes one: sauce's |
| 136 | +`Svec`, 90 lines, imported four times. Every program of size will. |
| 137 | + |
| 138 | +A generic module of the standard library, not the core: like `List` |
| 139 | +and `Map` it takes its memory from the allocator `init` gives it, and |
| 140 | +a target with no heap to speak of may leave it out. It is `Buf` for |
| 141 | +any `T`, and works as `Buf` does: |
| 142 | + |
| 143 | +``` |
| 144 | +module Vec(T); |
| 145 | +type Vec* = record len*: INT; ... end; |
| 146 | +proc init*(v: ^Vec; a: ^Mem.Allocator); |
| 147 | +proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) |
| 148 | +proc push*(v: ^Vec; x: T); (* x added at the end *) |
| 149 | +proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) |
| 150 | +proc resize*(v: ^Vec; n: INT); (* n elements, the new ones zero *) |
| 151 | +proc all*(v: ^Vec): []T; (* the elements, until it next grows *) |
| 152 | +proc reset*(v: ^Vec); (* emptied, its memory kept *) |
| 153 | +proc done*(v: ^Vec); (* its memory given back *) |
| 154 | +``` |
| 155 | + |
| 156 | +`all` gives a slice, so that indexing is checked; `v.len` is its |
| 157 | +length. `push`, `add` and `resize` stop the program when the |
| 158 | +allocator has no room, as `Buf.add` does, rather than returning a |
| 159 | +`BOOL` as `List.push` does: a `push` whose result is dropped loses an |
| 160 | +element silently, and an array is pushed to far more often than a |
| 161 | +list. `room` asks first. No `pop`, `insert` or `remove` until a |
| 162 | +program needs one: `v.len := v.len - 1` and `Sort` cover most. |
| 163 | + |
| 164 | +A new module breaks no program, so it can come with any release. |