perf(smith): derive message timestamps from UUIDv7
cc9ce07845afc73fa8f0491fc5f1e762885c4dcb
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (5)
+174 / −0 text lines
17+source = "registry+https://github.com/rust-lang/crates.io-index" 18+checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" 1420[[package]][[package]] 1521name = "anstream"name = "anstream" 1622version = "1.0.0"version = "1.0.0" 6773source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 6874checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" 79+source = "registry+https://github.com/rust-lang/crates.io-index" 80+checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" 7082[[package]][[package]] 7183name = "base64"name = "base64" 7284version = "0.22.1"version = "0.22.1" 101113source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 102114checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" 119+source = "registry+https://github.com/rust-lang/crates.io-index" 120+checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" 104122[[package]][[package]] 105123name = "cc"name = "cc" 106124version = "1.4.5"version = "1.4.5" 228246source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 229247checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48"checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" 252+source = "registry+https://github.com/rust-lang/crates.io-index" 253+checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" 278+name = "criterion-plot" 280+source = "registry+https://github.com/rust-lang/crates.io-index" 281+checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" 231287[[package]][[package]] 232288name = "crossbeam-deque"name = "crossbeam-deque" 233289version = "0.8.8"version = "0.8.8" 291347 "syn 3.0.5", "syn 3.0.5", 353+source = "registry+https://github.com/rust-lang/crates.io-index" 354+checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" 294356[[package]][[package]] 295357name = "encoding_rs"name = "encoding_rs" 296358version = "0.8.41"version = "0.8.41" 539601source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 540602checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 607+source = "registry+https://github.com/rust-lang/crates.io-index" 608+checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" 542610[[package]][[package]] 543611name = "http"name = "http" 544612version = "1.5.0"version = "1.5.0" 791859source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 792860checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0"checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" 865+source = "registry+https://github.com/rust-lang/crates.io-index" 866+checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" 870+ "windows-sys 0.61.2", 794873[[package]][[package]] 795874name = "is_terminal_polyfill"name = "is_terminal_polyfill" 796875version = "1.70.2"version = "1.70.2" 797876source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 798877checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" 882+source = "registry+https://github.com/rust-lang/crates.io-index" 883+checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" 800888[[package]][[package]] 801889name = "itoa"name = "itoa" 802890version = "1.0.18"version = "1.0.18" 1020+source = "registry+https://github.com/rust-lang/crates.io-index" 1021+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 9291026[[package]][[package]] 9301027name = "once_cell"name = "once_cell" 9311028version = "1.21.4"version = "1.21.4" 9381035source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 9391036checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" 1041+source = "registry+https://github.com/rust-lang/crates.io-index" 1042+checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" 9411044[[package]][[package]] 9421045name = "openssl"name = "openssl" 9431046version = "0.10.81"version = "0.10.81" 10051108source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 10061109checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" 1114+source = "registry+https://github.com/rust-lang/crates.io-index" 1115+checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" 1118+ "plotters-backend", 1125+name = "plotters-backend" 1127+source = "registry+https://github.com/rust-lang/crates.io-index" 1128+checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" 1131+name = "plotters-svg" 1133+source = "registry+https://github.com/rust-lang/crates.io-index" 1134+checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" 1136+ "plotters-backend", 10081139[[package]][[package]] 10091140name = "potential_utf"name = "potential_utf" 10101141version = "0.1.6"version = "0.1.6" 10381169source = "registry+https://github.com/rust-lang/crates.io-index"source = "registry+https://github.com/rust-lang/crates.io-index" 10391170checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" 1175+source = "registry+https://github.com/rust-lang/crates.io-index" 1176+checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" 1185+source = "registry+https://github.com/rust-lang/crates.io-index" 1186+checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" 10411192[[package]][[package]] 10421193name = "redox_users"name = "redox_users" 10431194version = "0.4.6"version = "0.4.6" 10491200 "thiserror", "thiserror", 1206+source = "registry+https://github.com/rust-lang/crates.io-index" 1207+checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" 10521215[[package]][[package]] 10531216name = "regex-automata"name = "regex-automata" 10541217version = "0.4.18"version = "0.4.18" 13141477version = "0.1.0"version = "0.1.0" 13151478dependencies = [dependencies = [ 13161479 "ciborium", "ciborium", 13181482 "futures", "futures", 13191483 "serde", "serde", 15111675 "zerovec", "zerovec", 1679+name = "tinytemplate" 1681+source = "registry+https://github.com/rust-lang/crates.io-index" 1682+checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" 15141688[[package]][[package]] 15151689name = "tokio"name = "tokio" 15161690version = "1.53.1"version = "1.53.1" +7 / −0 text lines
1515dirs = { workspace = true }dirs = { workspace = true } 1616futures = { workspace = true }futures = { workspace = true } 19+criterion = { workspace = true } 22+name = "message_creation" 1926default = []No final newlinedefault = []No final newline smith/benches/message_creation.rs
Addednew file mode 100644+25 / −0 text lines
3+use criterion::{Criterion, black_box, criterion_main}; 4+use smith::{Message, MessageId, Role}; 6+fn message_creation(criterion: &mut Criterion) { 7+ let mut group = criterion.benchmark_group("message_creation"); 8+ group.bench_function("message_id", |bencher| { 9+ bencher.iter(|| black_box(MessageId::new())); 11+ group.bench_function("empty_message", |bencher| { 12+ bencher.iter(|| black_box(Message::new(Role::Assistant))); 14+ group.bench_function("text_message", |bencher| { 15+ bencher.iter(|| black_box(Message::with_text(Role::User, "benchmark text"))); 21+ let mut criterion = Criterion::default().configure_from_args(); 22+ message_creation(&mut criterion); 25+criterion_main!(benches); +20 / −0 text lines
6161 assert_eq!(msg.role, Role::User); assert_eq!(msg.role, Role::User); 65+ fn message_timestamps_are_current_unix_milliseconds() { 66+ let before = std::time::SystemTime::now() 67+ .duration_since(std::time::UNIX_EPOCH) 70+ let empty = Message::new(Role::Assistant); 71+ let text = Message::with_text(Role::User, "hello"); 72+ let after = std::time::SystemTime::now() 73+ .duration_since(std::time::UNIX_EPOCH) 77+ for timestamp in [empty.timestamp_ms, text.timestamp_ms] { 78+ let timestamp = u128::try_from(timestamp).unwrap(); 79+ assert!(timestamp >= before); 80+ assert!(timestamp <= after); 6585 fn stream_event_serializes_deterministically() { fn stream_event_serializes_deterministically() { 6686 let msg = MessageId::new(); let msg = MessageId::new(); smith/src/message.rs
Modified+16 / −10 text lines
114114 #[must_use] #[must_use] 115115 pub fn new(role: Role) -> Self { pub fn new(role: Role) -> Self { 116+ let id = MessageId::new(); 117- id: MessageId::new(), 119120 blocks: Vec::new(), blocks: Vec::new(), 120- timestamp_ms: now_ms(), 121+ timestamp_ms: id_timestamp_ms(id), 125126 #[must_use] #[must_use] 126127 pub fn with_text<S: Into<String>>(role: Role, text: S) -> Self { pub fn with_text<S: Into<String>>(role: Role, text: S) -> Self { 128+ let id = MessageId::new(); 128- id: MessageId::new(), 130132 blocks: vec![ContentBlock::Text(text.into())], blocks: vec![ContentBlock::Text(text.into())], 131- timestamp_ms: now_ms(), 133+ timestamp_ms: id_timestamp_ms(id), 168- use std::time::{SystemTime, UNIX_EPOCH}; 170- .duration_since(UNIX_EPOCH) 171- .map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)) 171+fn id_timestamp_ms(id: MessageId) -> i64 { 172+ let Some(timestamp) = id.as_uuid().get_timestamp() else { 175+ let (seconds, nanos) = timestamp.to_unix(); 176+ let millis = u128::from(seconds) * 1_000 + u128::from(nanos / 1_000_000); 177+ i64::try_from(millis).unwrap_or(i64::MAX)