packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | // Decoding a PostgreSQL DataRow stream (the codec's hot path): ROWS rows |
| 2 | // of COLS text columns, decoded ROUNDS times. Not a test: run it with |
| 3 | // `louise run --native` in this directory. The size knob is ROWS × COLS × |
| 4 | // ROUNDS; the checksum adds up what decoding gave back. |
| 5 | |
| 6 | import postgres/protocol |
| 7 | import std/bytes |
| 8 | import std/int |
| 9 | import std/io |
| 10 | import std/list |
| 11 | |
| 12 | // The stream size: rows and text columns per row. |
| 13 | fn rows :: -> Int |
| 14 | { |
| 15 | 1000 |
| 16 | } |
| 17 | fn cols :: -> Int |
| 18 | { |
| 19 | 10 |
| 20 | } |
| 21 | |
| 22 | // The decode count: passes over the stream. |
| 23 | fn rounds :: -> Int |
| 24 | { |
| 25 | 100 |
| 26 | } |
| 27 | |
| 28 | /// `n` as 2 big-endian bytes. |
| 29 | fn be16 :: b: bytes.Builder n: Int -> bytes.Builder |
| 30 | { |
| 31 | let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) |
| 32 | bytes.append_byte b (int.and n 255) |
| 33 | } |
| 34 | |
| 35 | /// `n` as 4 big-endian bytes. |
| 36 | fn be32 :: b: bytes.Builder n: Int -> bytes.Builder |
| 37 | { |
| 38 | let b = bytes.append_byte b (int.and (int.shift_right n 24) 255) |
| 39 | let b = bytes.append_byte b (int.and (int.shift_right n 16) 255) |
| 40 | let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) |
| 41 | bytes.append_byte b (int.and n 255) |
| 42 | } |
| 43 | |
| 44 | /// A column's text: deterministic, a few bytes long. |
| 45 | fn column :: row: Int col: Int -> Bytes |
| 46 | { |
| 47 | bytes.from_string "value {row},{col}" |
| 48 | } |
| 49 | |
| 50 | /// One DataRow frame: 'D', the length, the column count, and a length and |
| 51 | /// the bytes per column. |
| 52 | fn data_row :: b: bytes.Builder row: Int -> bytes.Builder |
| 53 | { |
| 54 | let payload = |
| 55 | list.fold |
| 56 | (list.range 1 (cols ())) |
| 57 | (be16 (bytes.builder ()) (cols ())) |
| 58 | fn p col |
| 59 | { |
| 60 | let data = column row col |
| 61 | bytes.append (be32 p (bytes.length data)) data |
| 62 | } |
| 63 | let payload = bytes.build payload |
| 64 | let b = bytes.append_byte b 68 |
| 65 | let b = be32 b (bytes.length payload + 4) |
| 66 | bytes.append b payload |
| 67 | } |
| 68 | |
| 69 | /// The stream: ROWS DataRow frames, as one query's answer would arrive. |
| 70 | fn stream :: -> Bytes |
| 71 | { |
| 72 | bytes.build (list.fold (list.range 1 (rows ())) (bytes.builder ()) data_row) |
| 73 | } |
| 74 | |
| 75 | /// A checksum of a decoded row: its column count and column bytes. |
| 76 | fn row_checksum :: cols: List<Option<Bytes>> -> Int |
| 77 | { |
| 78 | list.fold |
| 79 | cols |
| 80 | 0 |
| 81 | fn acc col |
| 82 | { |
| 83 | match col { Some(data) -> acc + bytes.length data, None -> acc + 1 } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Decode the stream to its end, checksumming every row. |
| 88 | fn decode_stream :: buffer: Bytes at: Int acc: Int -> Int |
| 89 | { |
| 90 | match protocol.next buffer at |
| 91 | { protocol.Incomplete -> acc |
| 92 | , protocol.Malformed(e) -> |
| 93 | panic "postgres bench: the stream is malformed: {e}" |
| 94 | , protocol.Message(msg, next) -> match msg |
| 95 | { protocol.DataRow(cols) -> |
| 96 | decode_stream buffer next (acc + row_checksum cols) |
| 97 | , _ -> panic "postgres bench: a message that is not a DataRow" |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | fn round :: data: Bytes left: Int acc: Int -> Int |
| 103 | { |
| 104 | if left == 0 |
| 105 | { |
| 106 | acc |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | round data (left - 1) (acc + decode_stream data 0 0) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | pub fn main :: -> () / <io> |
| 115 | { |
| 116 | io.println (show (round (stream ()) (rounds ()) 0)) |
| 117 | } |