├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── paracord-benches ├── Cargo.toml ├── README.md └── benches │ ├── global.rs │ └── scoped.rs └── paracord ├── Cargo.toml └── src ├── lib.rs ├── macros.rs ├── serde.rs └── slice ├── alloc.rs └── mod.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "ahash" 7 | version = "0.8.11" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" 10 | dependencies = [ 11 | "cfg-if", 12 | "once_cell", 13 | "version_check", 14 | "zerocopy 0.7.35", 15 | ] 16 | 17 | [[package]] 18 | name = "aho-corasick" 19 | version = "1.1.3" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 22 | dependencies = [ 23 | "memchr", 24 | ] 25 | 26 | [[package]] 27 | name = "allocator-api2" 28 | version = "0.2.21" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" 31 | 32 | [[package]] 33 | name = "anstyle" 34 | version = "1.0.10" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" 37 | 38 | [[package]] 39 | name = "autocfg" 40 | version = "1.4.0" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 43 | 44 | [[package]] 45 | name = "bitflags" 46 | version = "2.9.0" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" 49 | 50 | [[package]] 51 | name = "boxcar" 52 | version = "0.2.10" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "225450ee9328e1e828319b48a89726cffc1b0ad26fd9211ad435de9fa376acae" 55 | dependencies = [ 56 | "loom", 57 | ] 58 | 59 | [[package]] 60 | name = "byteorder" 61 | version = "1.5.0" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 64 | 65 | [[package]] 66 | name = "cfg-if" 67 | version = "1.0.0" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 70 | 71 | [[package]] 72 | name = "clap" 73 | version = "4.5.32" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83" 76 | dependencies = [ 77 | "clap_builder", 78 | ] 79 | 80 | [[package]] 81 | name = "clap_builder" 82 | version = "4.5.32" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8" 85 | dependencies = [ 86 | "anstyle", 87 | "clap_lex", 88 | "terminal_size", 89 | ] 90 | 91 | [[package]] 92 | name = "clap_lex" 93 | version = "0.7.4" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" 96 | 97 | [[package]] 98 | name = "clashmap" 99 | version = "1.2.0" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | checksum = "e8a055b1f1bf558eae4959f6dd77cf2d7d50ae1483928e60ef21ca5a24fd4321" 102 | dependencies = [ 103 | "crossbeam-utils", 104 | "hashbrown 0.15.2", 105 | "lock_api", 106 | "parking_lot_core", 107 | "polonius-the-crab", 108 | "replace_with", 109 | ] 110 | 111 | [[package]] 112 | name = "condtype" 113 | version = "1.3.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "baf0a07a401f374238ab8e2f11a104d2851bf9ce711ec69804834de8af45c7af" 116 | 117 | [[package]] 118 | name = "crossbeam-utils" 119 | version = "0.8.21" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" 122 | 123 | [[package]] 124 | name = "dashmap" 125 | version = "6.1.0" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" 128 | dependencies = [ 129 | "cfg-if", 130 | "crossbeam-utils", 131 | "hashbrown 0.14.5", 132 | "lock_api", 133 | "once_cell", 134 | "parking_lot_core", 135 | ] 136 | 137 | [[package]] 138 | name = "divan" 139 | version = "0.1.17" 140 | source = "git+https://github.com/conradludgate/divan?branch=singletons#fe0096bcfbde65c8531fb879f58e3476a91a0ccd" 141 | dependencies = [ 142 | "cfg-if", 143 | "clap", 144 | "condtype", 145 | "divan-macros", 146 | "libc", 147 | "regex-lite", 148 | ] 149 | 150 | [[package]] 151 | name = "divan-macros" 152 | version = "0.1.17" 153 | source = "git+https://github.com/conradludgate/divan?branch=singletons#fe0096bcfbde65c8531fb879f58e3476a91a0ccd" 154 | dependencies = [ 155 | "proc-macro2", 156 | "quote", 157 | "syn", 158 | ] 159 | 160 | [[package]] 161 | name = "errno" 162 | version = "0.3.10" 163 | source = "registry+https://github.com/rust-lang/crates.io-index" 164 | checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" 165 | dependencies = [ 166 | "libc", 167 | "windows-sys", 168 | ] 169 | 170 | [[package]] 171 | name = "fastrand" 172 | version = "2.3.0" 173 | source = "registry+https://github.com/rust-lang/crates.io-index" 174 | checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" 175 | 176 | [[package]] 177 | name = "foldhash" 178 | version = "0.1.4" 179 | source = "registry+https://github.com/rust-lang/crates.io-index" 180 | checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" 181 | 182 | [[package]] 183 | name = "generator" 184 | version = "0.8.4" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "cc6bd114ceda131d3b1d665eba35788690ad37f5916457286b32ab6fd3c438dd" 187 | dependencies = [ 188 | "cfg-if", 189 | "libc", 190 | "log", 191 | "rustversion", 192 | "windows", 193 | ] 194 | 195 | [[package]] 196 | name = "getrandom" 197 | version = "0.3.2" 198 | source = "registry+https://github.com/rust-lang/crates.io-index" 199 | checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" 200 | dependencies = [ 201 | "cfg-if", 202 | "libc", 203 | "r-efi", 204 | "wasi", 205 | ] 206 | 207 | [[package]] 208 | name = "hashbrown" 209 | version = "0.14.5" 210 | source = "registry+https://github.com/rust-lang/crates.io-index" 211 | checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" 212 | dependencies = [ 213 | "ahash", 214 | "allocator-api2", 215 | ] 216 | 217 | [[package]] 218 | name = "hashbrown" 219 | version = "0.15.2" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" 222 | 223 | [[package]] 224 | name = "higher-kinded-types" 225 | version = "0.1.1" 226 | source = "registry+https://github.com/rust-lang/crates.io-index" 227 | checksum = "561985554c8b8d4808605c90a5f1979cc6c31a5d20b78465cd59501233c6678e" 228 | dependencies = [ 229 | "never-say-never", 230 | ] 231 | 232 | [[package]] 233 | name = "lasso" 234 | version = "0.7.3" 235 | source = "registry+https://github.com/rust-lang/crates.io-index" 236 | checksum = "6e14eda50a3494b3bf7b9ce51c52434a761e383d7238ce1dd5dcec2fbc13e9fb" 237 | dependencies = [ 238 | "dashmap", 239 | "hashbrown 0.14.5", 240 | ] 241 | 242 | [[package]] 243 | name = "lazy_static" 244 | version = "1.5.0" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" 247 | 248 | [[package]] 249 | name = "libc" 250 | version = "0.2.171" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" 253 | 254 | [[package]] 255 | name = "libm" 256 | version = "0.2.11" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" 259 | 260 | [[package]] 261 | name = "linux-raw-sys" 262 | version = "0.9.3" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | checksum = "fe7db12097d22ec582439daf8618b8fdd1a7bef6270e9af3b1ebcd30893cf413" 265 | 266 | [[package]] 267 | name = "lock_api" 268 | version = "0.4.12" 269 | source = "registry+https://github.com/rust-lang/crates.io-index" 270 | checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" 271 | dependencies = [ 272 | "autocfg", 273 | "scopeguard", 274 | ] 275 | 276 | [[package]] 277 | name = "log" 278 | version = "0.4.26" 279 | source = "registry+https://github.com/rust-lang/crates.io-index" 280 | checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" 281 | 282 | [[package]] 283 | name = "loom" 284 | version = "0.7.2" 285 | source = "registry+https://github.com/rust-lang/crates.io-index" 286 | checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" 287 | dependencies = [ 288 | "cfg-if", 289 | "generator", 290 | "scoped-tls", 291 | "tracing", 292 | "tracing-subscriber", 293 | ] 294 | 295 | [[package]] 296 | name = "matchers" 297 | version = "0.1.0" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" 300 | dependencies = [ 301 | "regex-automata 0.1.10", 302 | ] 303 | 304 | [[package]] 305 | name = "memchr" 306 | version = "2.7.4" 307 | source = "registry+https://github.com/rust-lang/crates.io-index" 308 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 309 | 310 | [[package]] 311 | name = "never-say-never" 312 | version = "6.6.666" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | checksum = "cf5a574dadd7941adeaa71823ecba5e28331b8313fb2e1c6a5c7e5981ea53ad6" 315 | 316 | [[package]] 317 | name = "nu-ansi-term" 318 | version = "0.46.0" 319 | source = "registry+https://github.com/rust-lang/crates.io-index" 320 | checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" 321 | dependencies = [ 322 | "overload", 323 | "winapi", 324 | ] 325 | 326 | [[package]] 327 | name = "num-traits" 328 | version = "0.2.19" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 331 | dependencies = [ 332 | "autocfg", 333 | "libm", 334 | ] 335 | 336 | [[package]] 337 | name = "once_cell" 338 | version = "1.21.1" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" 341 | 342 | [[package]] 343 | name = "overload" 344 | version = "0.1.1" 345 | source = "registry+https://github.com/rust-lang/crates.io-index" 346 | checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" 347 | 348 | [[package]] 349 | name = "paracord" 350 | version = "0.1.0-rc.7" 351 | dependencies = [ 352 | "boxcar", 353 | "clashmap", 354 | "foldhash", 355 | "hashbrown 0.15.2", 356 | "rand", 357 | "rand_distr", 358 | "serde", 359 | "serde_test", 360 | "sync_wrapper", 361 | "typed-arena", 362 | ] 363 | 364 | [[package]] 365 | name = "paracord-benches" 366 | version = "0.1.0" 367 | dependencies = [ 368 | "divan", 369 | "fastrand", 370 | "foldhash", 371 | "lasso", 372 | "paracord", 373 | "ustr", 374 | ] 375 | 376 | [[package]] 377 | name = "parking_lot" 378 | version = "0.12.3" 379 | source = "registry+https://github.com/rust-lang/crates.io-index" 380 | checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" 381 | dependencies = [ 382 | "lock_api", 383 | "parking_lot_core", 384 | ] 385 | 386 | [[package]] 387 | name = "parking_lot_core" 388 | version = "0.9.10" 389 | source = "registry+https://github.com/rust-lang/crates.io-index" 390 | checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" 391 | dependencies = [ 392 | "cfg-if", 393 | "libc", 394 | "redox_syscall", 395 | "smallvec", 396 | "windows-targets", 397 | ] 398 | 399 | [[package]] 400 | name = "pin-project-lite" 401 | version = "0.2.16" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" 404 | 405 | [[package]] 406 | name = "polonius-the-crab" 407 | version = "0.4.2" 408 | source = "registry+https://github.com/rust-lang/crates.io-index" 409 | checksum = "e97ca2c89572ae41bbec1c99498251f87dd5a94e500c5ec19c382dd593dd5ce9" 410 | dependencies = [ 411 | "higher-kinded-types", 412 | "never-say-never", 413 | ] 414 | 415 | [[package]] 416 | name = "ppv-lite86" 417 | version = "0.2.21" 418 | source = "registry+https://github.com/rust-lang/crates.io-index" 419 | checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" 420 | dependencies = [ 421 | "zerocopy 0.8.23", 422 | ] 423 | 424 | [[package]] 425 | name = "proc-macro2" 426 | version = "1.0.94" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" 429 | dependencies = [ 430 | "unicode-ident", 431 | ] 432 | 433 | [[package]] 434 | name = "quote" 435 | version = "1.0.40" 436 | source = "registry+https://github.com/rust-lang/crates.io-index" 437 | checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" 438 | dependencies = [ 439 | "proc-macro2", 440 | ] 441 | 442 | [[package]] 443 | name = "r-efi" 444 | version = "5.2.0" 445 | source = "registry+https://github.com/rust-lang/crates.io-index" 446 | checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 447 | 448 | [[package]] 449 | name = "rand" 450 | version = "0.9.0" 451 | source = "registry+https://github.com/rust-lang/crates.io-index" 452 | checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" 453 | dependencies = [ 454 | "rand_chacha", 455 | "rand_core", 456 | "zerocopy 0.8.23", 457 | ] 458 | 459 | [[package]] 460 | name = "rand_chacha" 461 | version = "0.9.0" 462 | source = "registry+https://github.com/rust-lang/crates.io-index" 463 | checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" 464 | dependencies = [ 465 | "ppv-lite86", 466 | "rand_core", 467 | ] 468 | 469 | [[package]] 470 | name = "rand_core" 471 | version = "0.9.3" 472 | source = "registry+https://github.com/rust-lang/crates.io-index" 473 | checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" 474 | dependencies = [ 475 | "getrandom", 476 | ] 477 | 478 | [[package]] 479 | name = "rand_distr" 480 | version = "0.5.1" 481 | source = "registry+https://github.com/rust-lang/crates.io-index" 482 | checksum = "6a8615d50dcf34fa31f7ab52692afec947c4dd0ab803cc87cb3b0b4570ff7463" 483 | dependencies = [ 484 | "num-traits", 485 | "rand", 486 | ] 487 | 488 | [[package]] 489 | name = "redox_syscall" 490 | version = "0.5.10" 491 | source = "registry+https://github.com/rust-lang/crates.io-index" 492 | checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" 493 | dependencies = [ 494 | "bitflags", 495 | ] 496 | 497 | [[package]] 498 | name = "regex" 499 | version = "1.11.1" 500 | source = "registry+https://github.com/rust-lang/crates.io-index" 501 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 502 | dependencies = [ 503 | "aho-corasick", 504 | "memchr", 505 | "regex-automata 0.4.9", 506 | "regex-syntax 0.8.5", 507 | ] 508 | 509 | [[package]] 510 | name = "regex-automata" 511 | version = "0.1.10" 512 | source = "registry+https://github.com/rust-lang/crates.io-index" 513 | checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" 514 | dependencies = [ 515 | "regex-syntax 0.6.29", 516 | ] 517 | 518 | [[package]] 519 | name = "regex-automata" 520 | version = "0.4.9" 521 | source = "registry+https://github.com/rust-lang/crates.io-index" 522 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" 523 | dependencies = [ 524 | "aho-corasick", 525 | "memchr", 526 | "regex-syntax 0.8.5", 527 | ] 528 | 529 | [[package]] 530 | name = "regex-lite" 531 | version = "0.1.6" 532 | source = "registry+https://github.com/rust-lang/crates.io-index" 533 | checksum = "53a49587ad06b26609c52e423de037e7f57f20d53535d66e08c695f347df952a" 534 | 535 | [[package]] 536 | name = "regex-syntax" 537 | version = "0.6.29" 538 | source = "registry+https://github.com/rust-lang/crates.io-index" 539 | checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" 540 | 541 | [[package]] 542 | name = "regex-syntax" 543 | version = "0.8.5" 544 | source = "registry+https://github.com/rust-lang/crates.io-index" 545 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 546 | 547 | [[package]] 548 | name = "replace_with" 549 | version = "0.1.7" 550 | source = "registry+https://github.com/rust-lang/crates.io-index" 551 | checksum = "e3a8614ee435691de62bcffcf4a66d91b3594bf1428a5722e79103249a095690" 552 | 553 | [[package]] 554 | name = "rustix" 555 | version = "1.0.2" 556 | source = "registry+https://github.com/rust-lang/crates.io-index" 557 | checksum = "f7178faa4b75a30e269c71e61c353ce2748cf3d76f0c44c393f4e60abf49b825" 558 | dependencies = [ 559 | "bitflags", 560 | "errno", 561 | "libc", 562 | "linux-raw-sys", 563 | "windows-sys", 564 | ] 565 | 566 | [[package]] 567 | name = "rustversion" 568 | version = "1.0.20" 569 | source = "registry+https://github.com/rust-lang/crates.io-index" 570 | checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" 571 | 572 | [[package]] 573 | name = "scoped-tls" 574 | version = "1.0.1" 575 | source = "registry+https://github.com/rust-lang/crates.io-index" 576 | checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" 577 | 578 | [[package]] 579 | name = "scopeguard" 580 | version = "1.2.0" 581 | source = "registry+https://github.com/rust-lang/crates.io-index" 582 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 583 | 584 | [[package]] 585 | name = "serde" 586 | version = "1.0.219" 587 | source = "registry+https://github.com/rust-lang/crates.io-index" 588 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 589 | dependencies = [ 590 | "serde_derive", 591 | ] 592 | 593 | [[package]] 594 | name = "serde_derive" 595 | version = "1.0.219" 596 | source = "registry+https://github.com/rust-lang/crates.io-index" 597 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 598 | dependencies = [ 599 | "proc-macro2", 600 | "quote", 601 | "syn", 602 | ] 603 | 604 | [[package]] 605 | name = "serde_test" 606 | version = "1.0.177" 607 | source = "registry+https://github.com/rust-lang/crates.io-index" 608 | checksum = "7f901ee573cab6b3060453d2d5f0bae4e6d628c23c0a962ff9b5f1d7c8d4f1ed" 609 | dependencies = [ 610 | "serde", 611 | ] 612 | 613 | [[package]] 614 | name = "sharded-slab" 615 | version = "0.1.7" 616 | source = "registry+https://github.com/rust-lang/crates.io-index" 617 | checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" 618 | dependencies = [ 619 | "lazy_static", 620 | ] 621 | 622 | [[package]] 623 | name = "smallvec" 624 | version = "1.14.0" 625 | source = "registry+https://github.com/rust-lang/crates.io-index" 626 | checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" 627 | 628 | [[package]] 629 | name = "syn" 630 | version = "2.0.100" 631 | source = "registry+https://github.com/rust-lang/crates.io-index" 632 | checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" 633 | dependencies = [ 634 | "proc-macro2", 635 | "quote", 636 | "unicode-ident", 637 | ] 638 | 639 | [[package]] 640 | name = "sync_wrapper" 641 | version = "1.0.2" 642 | source = "registry+https://github.com/rust-lang/crates.io-index" 643 | checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" 644 | 645 | [[package]] 646 | name = "terminal_size" 647 | version = "0.4.2" 648 | source = "registry+https://github.com/rust-lang/crates.io-index" 649 | checksum = "45c6481c4829e4cc63825e62c49186a34538b7b2750b73b266581ffb612fb5ed" 650 | dependencies = [ 651 | "rustix", 652 | "windows-sys", 653 | ] 654 | 655 | [[package]] 656 | name = "thread_local" 657 | version = "1.1.8" 658 | source = "registry+https://github.com/rust-lang/crates.io-index" 659 | checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" 660 | dependencies = [ 661 | "cfg-if", 662 | "once_cell", 663 | ] 664 | 665 | [[package]] 666 | name = "tracing" 667 | version = "0.1.41" 668 | source = "registry+https://github.com/rust-lang/crates.io-index" 669 | checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" 670 | dependencies = [ 671 | "pin-project-lite", 672 | "tracing-core", 673 | ] 674 | 675 | [[package]] 676 | name = "tracing-core" 677 | version = "0.1.33" 678 | source = "registry+https://github.com/rust-lang/crates.io-index" 679 | checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" 680 | dependencies = [ 681 | "once_cell", 682 | "valuable", 683 | ] 684 | 685 | [[package]] 686 | name = "tracing-log" 687 | version = "0.2.0" 688 | source = "registry+https://github.com/rust-lang/crates.io-index" 689 | checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" 690 | dependencies = [ 691 | "log", 692 | "once_cell", 693 | "tracing-core", 694 | ] 695 | 696 | [[package]] 697 | name = "tracing-subscriber" 698 | version = "0.3.19" 699 | source = "registry+https://github.com/rust-lang/crates.io-index" 700 | checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" 701 | dependencies = [ 702 | "matchers", 703 | "nu-ansi-term", 704 | "once_cell", 705 | "regex", 706 | "sharded-slab", 707 | "smallvec", 708 | "thread_local", 709 | "tracing", 710 | "tracing-core", 711 | "tracing-log", 712 | ] 713 | 714 | [[package]] 715 | name = "typed-arena" 716 | version = "2.0.2" 717 | source = "registry+https://github.com/rust-lang/crates.io-index" 718 | checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" 719 | 720 | [[package]] 721 | name = "unicode-ident" 722 | version = "1.0.18" 723 | source = "registry+https://github.com/rust-lang/crates.io-index" 724 | checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 725 | 726 | [[package]] 727 | name = "ustr" 728 | version = "1.1.0" 729 | source = "registry+https://github.com/rust-lang/crates.io-index" 730 | checksum = "18b19e258aa08450f93369cf56dd78063586adf19e92a75b338a800f799a0208" 731 | dependencies = [ 732 | "ahash", 733 | "byteorder", 734 | "lazy_static", 735 | "parking_lot", 736 | ] 737 | 738 | [[package]] 739 | name = "valuable" 740 | version = "0.1.1" 741 | source = "registry+https://github.com/rust-lang/crates.io-index" 742 | checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" 743 | 744 | [[package]] 745 | name = "version_check" 746 | version = "0.9.5" 747 | source = "registry+https://github.com/rust-lang/crates.io-index" 748 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 749 | 750 | [[package]] 751 | name = "wasi" 752 | version = "0.14.2+wasi-0.2.4" 753 | source = "registry+https://github.com/rust-lang/crates.io-index" 754 | checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" 755 | dependencies = [ 756 | "wit-bindgen-rt", 757 | ] 758 | 759 | [[package]] 760 | name = "winapi" 761 | version = "0.3.9" 762 | source = "registry+https://github.com/rust-lang/crates.io-index" 763 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 764 | dependencies = [ 765 | "winapi-i686-pc-windows-gnu", 766 | "winapi-x86_64-pc-windows-gnu", 767 | ] 768 | 769 | [[package]] 770 | name = "winapi-i686-pc-windows-gnu" 771 | version = "0.4.0" 772 | source = "registry+https://github.com/rust-lang/crates.io-index" 773 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 774 | 775 | [[package]] 776 | name = "winapi-x86_64-pc-windows-gnu" 777 | version = "0.4.0" 778 | source = "registry+https://github.com/rust-lang/crates.io-index" 779 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 780 | 781 | [[package]] 782 | name = "windows" 783 | version = "0.58.0" 784 | source = "registry+https://github.com/rust-lang/crates.io-index" 785 | checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" 786 | dependencies = [ 787 | "windows-core", 788 | "windows-targets", 789 | ] 790 | 791 | [[package]] 792 | name = "windows-core" 793 | version = "0.58.0" 794 | source = "registry+https://github.com/rust-lang/crates.io-index" 795 | checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" 796 | dependencies = [ 797 | "windows-implement", 798 | "windows-interface", 799 | "windows-result", 800 | "windows-strings", 801 | "windows-targets", 802 | ] 803 | 804 | [[package]] 805 | name = "windows-implement" 806 | version = "0.58.0" 807 | source = "registry+https://github.com/rust-lang/crates.io-index" 808 | checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" 809 | dependencies = [ 810 | "proc-macro2", 811 | "quote", 812 | "syn", 813 | ] 814 | 815 | [[package]] 816 | name = "windows-interface" 817 | version = "0.58.0" 818 | source = "registry+https://github.com/rust-lang/crates.io-index" 819 | checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" 820 | dependencies = [ 821 | "proc-macro2", 822 | "quote", 823 | "syn", 824 | ] 825 | 826 | [[package]] 827 | name = "windows-result" 828 | version = "0.2.0" 829 | source = "registry+https://github.com/rust-lang/crates.io-index" 830 | checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" 831 | dependencies = [ 832 | "windows-targets", 833 | ] 834 | 835 | [[package]] 836 | name = "windows-strings" 837 | version = "0.1.0" 838 | source = "registry+https://github.com/rust-lang/crates.io-index" 839 | checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" 840 | dependencies = [ 841 | "windows-result", 842 | "windows-targets", 843 | ] 844 | 845 | [[package]] 846 | name = "windows-sys" 847 | version = "0.59.0" 848 | source = "registry+https://github.com/rust-lang/crates.io-index" 849 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 850 | dependencies = [ 851 | "windows-targets", 852 | ] 853 | 854 | [[package]] 855 | name = "windows-targets" 856 | version = "0.52.6" 857 | source = "registry+https://github.com/rust-lang/crates.io-index" 858 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 859 | dependencies = [ 860 | "windows_aarch64_gnullvm", 861 | "windows_aarch64_msvc", 862 | "windows_i686_gnu", 863 | "windows_i686_gnullvm", 864 | "windows_i686_msvc", 865 | "windows_x86_64_gnu", 866 | "windows_x86_64_gnullvm", 867 | "windows_x86_64_msvc", 868 | ] 869 | 870 | [[package]] 871 | name = "windows_aarch64_gnullvm" 872 | version = "0.52.6" 873 | source = "registry+https://github.com/rust-lang/crates.io-index" 874 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 875 | 876 | [[package]] 877 | name = "windows_aarch64_msvc" 878 | version = "0.52.6" 879 | source = "registry+https://github.com/rust-lang/crates.io-index" 880 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 881 | 882 | [[package]] 883 | name = "windows_i686_gnu" 884 | version = "0.52.6" 885 | source = "registry+https://github.com/rust-lang/crates.io-index" 886 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 887 | 888 | [[package]] 889 | name = "windows_i686_gnullvm" 890 | version = "0.52.6" 891 | source = "registry+https://github.com/rust-lang/crates.io-index" 892 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 893 | 894 | [[package]] 895 | name = "windows_i686_msvc" 896 | version = "0.52.6" 897 | source = "registry+https://github.com/rust-lang/crates.io-index" 898 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 899 | 900 | [[package]] 901 | name = "windows_x86_64_gnu" 902 | version = "0.52.6" 903 | source = "registry+https://github.com/rust-lang/crates.io-index" 904 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 905 | 906 | [[package]] 907 | name = "windows_x86_64_gnullvm" 908 | version = "0.52.6" 909 | source = "registry+https://github.com/rust-lang/crates.io-index" 910 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 911 | 912 | [[package]] 913 | name = "windows_x86_64_msvc" 914 | version = "0.52.6" 915 | source = "registry+https://github.com/rust-lang/crates.io-index" 916 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 917 | 918 | [[package]] 919 | name = "wit-bindgen-rt" 920 | version = "0.39.0" 921 | source = "registry+https://github.com/rust-lang/crates.io-index" 922 | checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" 923 | dependencies = [ 924 | "bitflags", 925 | ] 926 | 927 | [[package]] 928 | name = "zerocopy" 929 | version = "0.7.35" 930 | source = "registry+https://github.com/rust-lang/crates.io-index" 931 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 932 | dependencies = [ 933 | "zerocopy-derive 0.7.35", 934 | ] 935 | 936 | [[package]] 937 | name = "zerocopy" 938 | version = "0.8.23" 939 | source = "registry+https://github.com/rust-lang/crates.io-index" 940 | checksum = "fd97444d05a4328b90e75e503a34bad781f14e28a823ad3557f0750df1ebcbc6" 941 | dependencies = [ 942 | "zerocopy-derive 0.8.23", 943 | ] 944 | 945 | [[package]] 946 | name = "zerocopy-derive" 947 | version = "0.7.35" 948 | source = "registry+https://github.com/rust-lang/crates.io-index" 949 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 950 | dependencies = [ 951 | "proc-macro2", 952 | "quote", 953 | "syn", 954 | ] 955 | 956 | [[package]] 957 | name = "zerocopy-derive" 958 | version = "0.8.23" 959 | source = "registry+https://github.com/rust-lang/crates.io-index" 960 | checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154" 961 | dependencies = [ 962 | "proc-macro2", 963 | "quote", 964 | "syn", 965 | ] 966 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "paracord", 4 | "paracord-benches", 5 | ] 6 | resolver = "2" 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # paracord 2 | 3 | A fast, simple, multi-threaded string interning library. 4 | -------------------------------------------------------------------------------- /paracord-benches/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "paracord-benches" 3 | version = "0.1.0" 4 | edition = "2021" 5 | publish = false 6 | rust-version = "1.80.0" 7 | 8 | [dependencies] 9 | 10 | [dev-dependencies] 11 | divan = { git = "https://github.com/conradludgate/divan", branch = "singletons" } 12 | fastrand = "2" 13 | foldhash = "0.1" 14 | 15 | paracord = { path = "../paracord" } 16 | lasso = { version = "0.7", features = ["multi-threaded"] } 17 | ustr = "1.1.0" 18 | 19 | [[bench]] 20 | name = "scoped" 21 | harness = false 22 | 23 | [[bench]] 24 | name = "global" 25 | harness = false 26 | -------------------------------------------------------------------------------- /paracord-benches/README.md: -------------------------------------------------------------------------------- 1 | Results 2 | 3 | ## Scoped 4 | 5 | ### Linux Ryzen 9 7950x 6 | 7 | ``` 8 | Timer precision: 9 ns 9 | paracord fastest │ slowest │ median │ mean 10 | ├─ get │ │ │ 11 | │ ├─ t=1 63.9 ns │ 525.3 ns │ 78.39 ns │ 83.37 ns 12 | │ ├─ t=2 74.86 ns │ 166.6 ns │ 88.51 ns │ 97.17 ns 13 | │ ╰─ t=16 48.43 ns │ 247.4 ns │ 114.4 ns │ 120.5 ns 14 | ├─ get_or_intern │ │ │ 15 | │ ├─ t=1 37.91 ns │ 91.38 ns │ 39.81 ns │ 42.28 ns 16 | │ ├─ t=2 45.21 ns │ 409 ns │ 328.2 ns │ 240.1 ns 17 | │ ╰─ t=16 192.7 ns │ 1.964 µs │ 1.633 µs │ 1.584 µs 18 | ╰─ resolve │ │ │ 19 | ├─ t=1 9.423 ns │ 34.87 ns │ 11.01 ns │ 12.17 ns 20 | ├─ t=2 8.332 ns │ 35.61 ns │ 11.16 ns │ 11.89 ns 21 | ╰─ t=16 9.002 ns │ 38.09 ns │ 13.15 ns │ 15.27 ns 22 | 23 | lasso fastest │ slowest │ median │ mean 24 | ├─ get │ │ │ 25 | │ ├─ t=1 52.08 ns │ 420.6 ns │ 76.05 ns │ 79.82 ns 26 | │ ├─ t=2 57.3 ns │ 190.7 ns │ 85.54 ns │ 90.81 ns 27 | │ ╰─ t=16 63.71 ns │ 217.7 ns │ 109.5 ns │ 117.1 ns 28 | ├─ get_or_intern │ │ │ 29 | │ ├─ t=1 58.1 ns │ 99.16 ns │ 71.15 ns │ 72.42 ns 30 | │ ├─ t=2 342.6 ns │ 606.6 ns │ 515.4 ns │ 514 ns 31 | │ ╰─ t=16 705.1 ns │ 2.51 µs │ 1.422 µs │ 1.431 µs 32 | ╰─ resolve │ │ │ 33 | ├─ t=1 26.1 ns │ 59.19 ns │ 30.28 ns │ 31.01 ns 34 | ├─ t=2 28.71 ns │ 128.7 ns │ 47.67 ns │ 51.45 ns 35 | ╰─ t=16 34.63 ns │ 144.8 ns │ 84.37 ns │ 86.78 ns 36 | ``` 37 | 38 | ### Apple M4 Max 39 | 40 | ``` 41 | Timer precision: 41 ns 42 | paracord fastest │ slowest │ median │ mean 43 | ├─ get │ │ │ 44 | │ ├─ t=1 22.28 ns │ 53.11 ns │ 25.86 ns │ 27.29 ns 45 | │ ├─ t=2 29.2 ns │ 110.6 ns │ 38.2 ns │ 39.01 ns 46 | │ ╰─ t=14 34.07 ns │ 173.5 ns │ 115.5 ns │ 116.8 ns 47 | ├─ get_or_intern │ │ │ 48 | │ ├─ t=1 26.65 ns │ 54.65 ns │ 30.49 ns │ 30.07 ns 49 | │ ├─ t=2 102.5 ns │ 198.5 ns │ 128.4 ns │ 129.7 ns 50 | │ ╰─ t=14 1.039 µs │ 2.268 µs │ 1.465 µs │ 1.508 µs 51 | ╰─ resolve │ │ │ 52 | ├─ t=1 4.076 ns │ 74.24 ns │ 5.242 ns │ 5.701 ns 53 | ├─ t=2 4.95 ns │ 24.53 ns │ 6.368 ns │ 6.822 ns 54 | ╰─ t=14 5.367 ns │ 134.2 ns │ 6.618 ns │ 9.706 ns 55 | 56 | lasso fastest │ slowest │ median │ mean 57 | ├─ get │ │ │ 58 | │ ├─ t=1 19.48 ns │ 57.4 ns │ 23.21 ns │ 24.49 ns 59 | │ ├─ t=2 26.15 ns │ 81.53 ns │ 33.03 ns │ 33.83 ns 60 | │ ╰─ t=14 41.07 ns │ 177.3 ns │ 106.8 ns │ 102.6 ns 61 | ├─ get_or_intern │ │ │ 62 | │ ├─ t=1 41.4 ns │ 105.8 ns │ 46.53 ns │ 47.13 ns 63 | │ ├─ t=2 86.73 ns │ 612.4 ns │ 170 ns │ 176 ns 64 | │ ╰─ t=14 2.46 µs │ 3.32 µs │ 3.063 µs │ 3.047 µs 65 | ╰─ resolve │ │ │ 66 | ├─ t=1 10.19 ns │ 68.19 ns │ 13.07 ns │ 13.36 ns 67 | ├─ t=2 16.32 ns │ 53.86 ns │ 22.32 ns │ 22.54 ns 68 | ╰─ t=14 22.32 ns │ 182.9 ns │ 135.6 ns │ 128.8 ns 69 | ``` 70 | 71 | ## Global 72 | 73 | ### Linux Ryzen 9 7950x 74 | 75 | ``` 76 | Timer precision: 9 ns 77 | ustr_global fastest │ slowest │ median │ mean 78 | ├─ get │ │ │ 79 | │ ├─ t=1 44.54 ns │ 68.21 ns │ 51.89 ns │ 52.69 ns 80 | │ ├─ t=2 47.84 ns │ 97.97 ns │ 56.56 ns │ 57.39 ns 81 | │ ╰─ t=16 83.58 ns │ 392.9 ns │ 152.3 ns │ 160 ns 82 | ├─ get_or_intern │ │ │ 83 | │ ├─ t=1 61.84 ns │ 2.913 µs │ 83.51 ns │ 114.1 ns 84 | │ ├─ t=2 95.82 ns │ 6.171 µs │ 128.4 ns │ 240.3 ns 85 | │ ╰─ t=16 151.2 ns │ 835.6 ns │ 371.5 ns │ 379 ns 86 | ╰─ resolve │ │ │ 87 | ├─ t=1 3.611 ns │ 12.84 ns │ 4.62 ns │ 4.738 ns 88 | ├─ t=2 4.051 ns │ 13.88 ns │ 4.996 ns │ 5.057 ns 89 | ╰─ t=16 4.471 ns │ 31.73 ns │ 7.106 ns │ 8.012 ns 90 | 91 | paracord_global fastest │ slowest │ median │ mean 92 | ├─ get │ │ │ 93 | │ ├─ t=1 39.63 ns │ 83.95 ns │ 45.28 ns │ 47.39 ns 94 | │ ├─ t=2 45.37 ns │ 81.39 ns │ 50.62 ns │ 52.22 ns 95 | │ ╰─ t=16 60.94 ns │ 257.1 ns │ 105.7 ns │ 113.5 ns 96 | ├─ get_or_intern │ │ │ 97 | │ ├─ t=1 101.4 ns │ 3.039 µs │ 117.2 ns │ 190.5 ns 98 | │ ├─ t=2 296 ns │ 4.648 µs │ 347.6 ns │ 359.1 ns 99 | │ ╰─ t=16 199.8 ns │ 16.01 µs │ 1.23 µs │ 1.851 µs 100 | ╰─ resolve │ │ │ 101 | ├─ t=1 4.731 ns │ 20.95 ns │ 6.751 ns │ 6.818 ns 102 | ├─ t=2 7.122 ns │ 27.27 ns │ 8.456 ns │ 9.273 ns 103 | ╰─ t=16 7.031 ns │ 36.54 ns │ 9.936 ns │ 11.3 ns 104 | 105 | lasso_global fastest │ slowest │ median │ mean 106 | ├─ get │ │ │ 107 | │ ├─ t=1 33.25 ns │ 76.23 ns │ 37.74 ns │ 40.36 ns 108 | │ ├─ t=2 35.26 ns │ 74.71 ns │ 39.28 ns │ 40.58 ns 109 | │ ╰─ t=16 52.24 ns │ 209.7 ns │ 104.4 ns │ 110.3 ns 110 | ├─ get_or_intern │ │ │ 111 | │ ├─ t=1 161.2 ns │ 26.96 µs │ 200.6 ns │ 438 ns 112 | │ ├─ t=2 282.8 ns │ 595.7 ns │ 358.7 ns │ 392.2 ns 113 | │ ╰─ t=16 528.9 ns │ 57.91 µs │ 2.01 µs │ 3.63 µs 114 | ╰─ resolve │ │ │ 115 | ├─ t=1 47.31 ns │ 147.4 ns │ 53.88 ns │ 56.33 ns 116 | ├─ t=2 51.26 ns │ 125.6 ns │ 68.98 ns │ 69.9 ns 117 | ╰─ t=16 65.62 ns │ 195.5 ns │ 102.7 ns │ 107.4 ns 118 | ``` 119 | 120 | ### Apple M4 Max 121 | 122 | ``` 123 | Timer precision: 41 ns 124 | ustr_global fastest │ slowest │ median │ mean 125 | ├─ get │ │ │ 126 | │ ├─ t=1 26 ns │ 124.5 ns │ 29.71 ns │ 30.72 ns 127 | │ ├─ t=2 33.25 ns │ 83.96 ns │ 38.28 ns │ 39.14 ns 128 | │ ╰─ t=14 62.75 ns │ 251.5 ns │ 144.2 ns │ 145.1 ns 129 | ├─ get_or_intern │ │ │ 130 | │ ├─ t=1 36.88 ns │ 1.209 µs │ 45.9 ns │ 55.95 ns 131 | │ ├─ t=2 46.46 ns │ 1.815 µs │ 57.17 ns │ 91.18 ns 132 | │ ╰─ t=14 135.8 ns │ 498.6 ns │ 303.7 ns │ 305.9 ns 133 | ╰─ resolve │ │ │ 134 | ├─ t=1 0.884 ns │ 12.09 ns │ 1.259 ns │ 1.671 ns 135 | ├─ t=2 0.967 ns │ 8.967 ns │ 1.676 ns │ 1.727 ns 136 | ╰─ t=14 1.051 ns │ 26.63 ns │ 1.634 ns │ 2.54 ns 137 | 138 | paracord_global fastest │ slowest │ median │ mean 139 | ├─ get │ │ │ 140 | │ ├─ t=1 21.15 ns │ 88.36 ns │ 25.11 ns │ 26.17 ns 141 | │ ├─ t=2 29.23 ns │ 101.4 ns │ 34.03 ns │ 35.28 ns 142 | │ ╰─ t=14 39.11 ns │ 178.2 ns │ 119.8 ns │ 122 ns 143 | ├─ get_or_intern │ │ │ 144 | │ ├─ t=1 57.69 ns │ 1.612 µs │ 67.19 ns │ 93.38 ns 145 | │ ├─ t=2 116.2 ns │ 4.003 µs │ 125.6 ns │ 137.1 ns 146 | │ ╰─ t=14 1.149 µs │ 8.198 µs │ 1.402 µs │ 1.617 µs 147 | ╰─ resolve │ │ │ 148 | ├─ t=1 3.78 ns │ 173.4 ns │ 5.614 ns │ 5.868 ns 149 | ├─ t=2 4.988 ns │ 20.98 ns │ 6.447 ns │ 6.613 ns 150 | ╰─ t=14 5.28 ns │ 122.4 ns │ 6.572 ns │ 9.12 ns 151 | 152 | lasso_global fastest │ slowest │ median │ mean 153 | ├─ get │ │ │ 154 | │ ├─ t=1 19.45 ns │ 46.9 ns │ 23.03 ns │ 24.03 ns 155 | │ ├─ t=2 25.15 ns │ 84.15 ns │ 30.82 ns │ 31.4 ns 156 | │ ╰─ t=14 43.28 ns │ 170.9 ns │ 119.5 ns │ 123.6 ns 157 | ├─ get_or_intern │ │ │ 158 | │ ├─ t=1 67.57 ns │ 10.73 µs │ 82.93 ns │ 155.3 ns 159 | │ ├─ t=2 161 ns │ 514.6 ns │ 178 ns │ 181.4 ns 160 | │ ╰─ t=14 2.03 µs │ 25.2 µs │ 2.994 µs │ 3.561 µs 161 | ╰─ resolve │ │ │ 162 | ├─ t=1 21.49 ns │ 73.07 ns │ 23.74 ns │ 24.54 ns 163 | ├─ t=2 27.28 ns │ 56.7 ns │ 31.32 ns │ 31.8 ns 164 | ╰─ t=14 31.99 ns │ 189.1 ns │ 140.4 ns │ 137.7 ns 165 | ``` 166 | -------------------------------------------------------------------------------- /paracord-benches/benches/global.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | divan::Divan::from_args() 3 | .sample_count(1000) 4 | .sample_size(1000) 5 | .threads([1, 2, 0]) 6 | .main(); 7 | } 8 | 9 | mod ustr { 10 | use divan::{black_box_drop, Bencher}; 11 | use ustr::Ustr; 12 | 13 | #[divan::bench] 14 | fn get_or_intern(b: Bencher) { 15 | b.with_inputs(|| fastrand::u32(10000000..=99999999).to_string()) 16 | .bench_refs(|s| black_box_drop(Ustr::from(s))); 17 | } 18 | 19 | #[divan::bench] 20 | fn get(b: Bencher) { 21 | for x in 100000..=999999 { 22 | Ustr::from(&x.to_string()); 23 | } 24 | 25 | b.with_inputs(|| fastrand::u32(100000..=999999).to_string()) 26 | .bench_refs(|s| black_box_drop(Ustr::from_existing(s))); 27 | } 28 | 29 | #[divan::bench] 30 | fn resolve(b: Bencher) { 31 | let mut keys = vec![]; 32 | for x in 100000..=999999 { 33 | keys.push(Ustr::from(&x.to_string())); 34 | } 35 | 36 | b.with_inputs(|| *fastrand::choice(&keys).unwrap()) 37 | .bench_values(|key| black_box_drop(&*key)); 38 | } 39 | } 40 | 41 | mod paracord { 42 | use divan::{black_box_drop, Bencher}; 43 | use paracord::custom_key; 44 | 45 | custom_key!(struct Global); 46 | 47 | #[divan::bench] 48 | fn get_or_intern(b: Bencher) { 49 | b.with_inputs(|| fastrand::u32(10000000..=99999999).to_string()) 50 | .bench_refs(|s| black_box_drop(Global::new(s))); 51 | } 52 | 53 | #[divan::bench] 54 | fn get(b: Bencher) { 55 | for x in 100000..=999999 { 56 | Global::new(&x.to_string()); 57 | } 58 | 59 | b.with_inputs(|| fastrand::u32(100000..=999999).to_string()) 60 | .bench_refs(|s| black_box_drop(Global::try_new_existing(s).unwrap())); 61 | } 62 | 63 | #[divan::bench] 64 | fn resolve(b: Bencher) { 65 | let mut keys = vec![]; 66 | for x in 100000..=999999 { 67 | keys.push(Global::new(&x.to_string())); 68 | } 69 | 70 | b.with_inputs(|| *fastrand::choice(&keys).unwrap()) 71 | .bench_values(|key| black_box_drop(key.as_str())); 72 | } 73 | } 74 | 75 | mod lasso { 76 | use divan::{black_box_drop, Bencher}; 77 | use foldhash::fast::RandomState; 78 | use lasso::{Spur, ThreadedRodeo}; 79 | 80 | #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Clone, Copy)] 81 | #[repr(transparent)] 82 | struct Global(lasso::Spur); 83 | 84 | impl Global { 85 | fn rodeo() -> &'static ThreadedRodeo { 86 | static S: std::sync::OnceLock> = 87 | std::sync::OnceLock::new(); 88 | S.get_or_init( 89 | || ThreadedRodeo::::with_hasher(RandomState::default()), 90 | ) 91 | } 92 | 93 | pub fn get(s: &str) -> Option { 94 | Self::rodeo().get(s).map(Self) 95 | } 96 | 97 | pub fn get_or_intern(s: &str) -> Self { 98 | Self(Self::rodeo().get_or_intern(s)) 99 | } 100 | 101 | pub fn resolve(self) -> &'static str { 102 | Self::rodeo().resolve(&self.0) 103 | } 104 | } 105 | 106 | #[divan::bench] 107 | fn get_or_intern(b: Bencher) { 108 | b.with_inputs(|| fastrand::u32(10000000..=99999999).to_string()) 109 | .bench_refs(|s| black_box_drop(Global::get_or_intern(s))); 110 | } 111 | 112 | #[divan::bench] 113 | fn get(b: Bencher) { 114 | for x in 100000..=999999 { 115 | Global::get_or_intern(&x.to_string()); 116 | } 117 | 118 | b.with_inputs(|| fastrand::u32(100000..=999999).to_string()) 119 | .bench_refs(|s| black_box_drop(Global::get(s).unwrap())); 120 | } 121 | 122 | #[divan::bench] 123 | fn resolve(b: Bencher) { 124 | let mut keys = vec![]; 125 | for x in 100000..=999999 { 126 | keys.push(Global::get_or_intern(&x.to_string())); 127 | } 128 | 129 | b.with_inputs(|| *fastrand::choice(&keys).unwrap()) 130 | .bench_values(|key| black_box_drop(key.resolve())); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /paracord-benches/benches/scoped.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | divan::Divan::from_args() 3 | .sample_count(1000) 4 | .sample_size(1000) 5 | .threads([1, 2, 0]) 6 | .main(); 7 | } 8 | 9 | mod paracord { 10 | use divan::{black_box_drop, Bencher}; 11 | use paracord::ParaCord; 12 | 13 | #[divan::bench] 14 | fn get_or_intern(b: Bencher) { 15 | b.with_singleton(ParaCord::default) 16 | .with_inputs(|_: &ParaCord| fastrand::u32(100000..=999999).to_string()) 17 | .bench_refs(|p, s| black_box_drop(p.get_or_intern(s))); 18 | } 19 | 20 | #[divan::bench] 21 | fn get(b: Bencher) { 22 | let p = ParaCord::default(); 23 | for x in 100000..=999999 { 24 | p.get_or_intern(&x.to_string()); 25 | } 26 | 27 | b.with_inputs(|| fastrand::u32(100000..=999999).to_string()) 28 | .bench_refs(|s| black_box_drop(p.get(s).unwrap())); 29 | } 30 | 31 | #[divan::bench] 32 | fn resolve(b: Bencher) { 33 | let p = ParaCord::default(); 34 | let mut keys = vec![]; 35 | for x in 100000..=999999 { 36 | keys.push(p.get_or_intern(&x.to_string())); 37 | } 38 | 39 | b.with_inputs(|| *fastrand::choice(&keys).unwrap()) 40 | .bench_values(|key| black_box_drop(p.resolve(key))); 41 | } 42 | } 43 | 44 | mod lasso { 45 | use divan::{black_box_drop, Bencher}; 46 | use foldhash::fast::RandomState; 47 | use lasso::{Spur, ThreadedRodeo}; 48 | 49 | #[divan::bench] 50 | fn get_or_intern(b: Bencher) { 51 | b.with_singleton( 52 | || ThreadedRodeo::::with_hasher(RandomState::default()), 53 | ) 54 | .with_inputs(|_: &ThreadedRodeo<_, _>| fastrand::u32(100000..=999999).to_string()) 55 | .bench_refs(|p, s| black_box_drop(p.get_or_intern(s))); 56 | } 57 | 58 | #[divan::bench] 59 | fn get(b: Bencher) { 60 | let p = ThreadedRodeo::::with_hasher(RandomState::default()); 61 | for x in 100000..=999999 { 62 | p.get_or_intern(x.to_string()); 63 | } 64 | 65 | b.with_inputs(|| fastrand::u32(100000..=999999).to_string()) 66 | .bench_refs(|s| black_box_drop(p.get(s).unwrap())); 67 | } 68 | 69 | #[divan::bench] 70 | fn resolve(b: Bencher) { 71 | let p = ThreadedRodeo::::with_hasher(RandomState::default()); 72 | let mut keys = vec![]; 73 | for x in 100000..=999999 { 74 | keys.push(p.get_or_intern(x.to_string())); 75 | } 76 | 77 | b.with_inputs(|| *fastrand::choice(&keys).unwrap()) 78 | .bench_values(|key| black_box_drop(p.resolve(&key))); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /paracord/Cargo.toml: -------------------------------------------------------------------------------- 1 | 2 | [package] 3 | name = "paracord" 4 | version = "0.1.0-rc.7" 5 | edition = "2021" 6 | license = "Apache-2.0" 7 | description = "A fast, simple, multi-threaded string interning library" 8 | authors = ["Conrad Ludgate "] 9 | repository = "https://github.com/conradludgate/paracord" 10 | rust-version = "1.73.0" 11 | 12 | [features] 13 | default = ["serde"] 14 | serde = ["dep:serde"] 15 | 16 | [dependencies] 17 | boxcar = "0.2" 18 | foldhash = "0.1" 19 | clashmap = { version = "1.2", features = ["raw-api"] } 20 | typed-arena = "2" 21 | hashbrown = { version = "0.15", default-features = false } 22 | sync_wrapper = "1" 23 | 24 | serde = { version = "1", optional = true } 25 | 26 | [dev-dependencies] 27 | serde_test = { version = "1" } 28 | rand = "0.9" 29 | rand_distr = "0.5" 30 | 31 | [package.metadata.docs.rs] 32 | all-features = true 33 | rustdoc-args = ["--cfg", "docsrs"] 34 | -------------------------------------------------------------------------------- /paracord/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! [`ParaCord`] is a lightweight, thread-safe, memory efficient [string interner](https://en.wikipedia.org/wiki/String_interning). 2 | //! 3 | //! When calling [`ParaCord::get_or_intern`], a [`Key`] is returned. This [`Key`] is guaranteed to be unique if the input string is unique, 4 | //! and is guaranteed to be the same if the input string is the same. [`Key`] is 32bits, and has a niche value which allows `Option` to 5 | //! also be 32bits. 6 | //! 7 | //! The 32bit key imposes a limitation that allocating 2^32 strings will panic. There's an additional self-imposed limitation that 8 | //! no string can be longer than 2^32 bytes long. 9 | //! 10 | //! If you don't want to intern the string, but check for it's existence, you can use [`ParaCord::get`], which returns `None` if not 11 | //! present. 12 | //! 13 | //! [`Key`]s can be exchanged back into strings using [`ParaCord::resolve`]. It's important to keep in mind that this might panic 14 | //! or return nonsense results if given a key returned by some other [`ParaCord`] instance. 15 | //! 16 | //! This string interner is not garbage collected, so strings that are allocated in the interner are not released 17 | //! until the [`ParaCord`] instance is dropped. 18 | //! 19 | //! # Examples 20 | //! 21 | //! With a self-managed `ParaCord` instance. 22 | //! 23 | //! ``` 24 | //! use paracord::ParaCord; 25 | //! 26 | //! let paracord = ParaCord::default(); 27 | //! 28 | //! let foo = paracord.get_or_intern("foo"); 29 | //! let bar = paracord.get_or_intern("bar"); 30 | //! 31 | //! assert_ne!(foo, bar); 32 | //! 33 | //! // returns the same key, no insert 34 | //! let foo2 = paracord.get_or_intern("foo"); 35 | //! assert_eq!(foo, foo2); 36 | //! 37 | //! // returns the same key, guaranteed no insert 38 | //! let foo3 = paracord.get("foo").unwrap(); 39 | //! assert_eq!(foo, foo3); 40 | //! 41 | //! // can be exchanged for the string 42 | //! assert_eq!(paracord.resolve(foo), "foo"); 43 | //! assert_eq!(paracord.resolve(bar), "bar"); 44 | //! ``` 45 | //! 46 | //! With a globally managed instance, with typed keys 47 | //! 48 | //! ``` 49 | //! paracord::custom_key!(pub struct NameKey); 50 | //! 51 | //! let foo = NameKey::new("foo"); 52 | //! let bar = NameKey::new("bar"); 53 | //! 54 | //! assert_ne!(foo, bar); 55 | //! 56 | //! // returns the same key, no insert 57 | //! let foo2 = NameKey::new("foo"); 58 | //! assert_eq!(foo, foo2); 59 | //! 60 | //! // returns the same key, guaranteed no insert 61 | //! let foo3 = NameKey::try_new_existing("foo").unwrap(); 62 | //! assert_eq!(foo, foo3); 63 | //! 64 | //! // can be exchanged for the string 65 | //! assert_eq!(foo.as_str(), "foo"); 66 | //! assert_eq!(bar.as_str(), "bar"); 67 | //! ``` 68 | #![warn( 69 | unsafe_op_in_unsafe_fn, 70 | clippy::missing_safety_doc, 71 | clippy::multiple_unsafe_ops_per_block, 72 | clippy::undocumented_unsafe_blocks 73 | )] 74 | #![cfg_attr(docsrs, feature(doc_cfg))] 75 | 76 | use core::fmt; 77 | use std::{ 78 | hash::{BuildHasher, Hash}, 79 | num::NonZeroU32, 80 | ops::Index, 81 | }; 82 | 83 | pub mod slice; 84 | 85 | mod macros; 86 | 87 | #[cfg(feature = "serde")] 88 | mod serde; 89 | #[cfg(not(feature = "serde"))] 90 | mod serde { 91 | #[doc(hidden)] 92 | #[macro_export] 93 | macro_rules! custom_key_serde { 94 | ($key:ident) => {}; 95 | } 96 | 97 | pub use custom_key_serde; 98 | } 99 | 100 | #[doc(hidden)] 101 | pub mod __private { 102 | pub use foldhash::fast::RandomState; 103 | pub mod serde { 104 | pub use crate::serde::*; 105 | } 106 | } 107 | 108 | custom_key!( 109 | /// A key that allocates in a global [`ParaCord`] instance. 110 | /// 111 | /// Custom global keys can be defined using [`custom_key`] 112 | /// 113 | /// ``` 114 | /// use paracord::DefaultKey; 115 | /// 116 | /// let key = DefaultKey::new("foo"); 117 | /// assert_eq!(key.as_str(), "foo"); 118 | /// 119 | /// let key2 = DefaultKey::try_new_existing("foo").unwrap(); 120 | /// assert_eq!(key, key2); 121 | /// ``` 122 | pub struct DefaultKey; 123 | ); 124 | 125 | /// Key type returned by [`ParaCord`]. 126 | /// 127 | /// [`Key`] implements [`core::cmp::Ord`] for use within collections like [`BTreeMap`](std::collections::BTreeMap), 128 | /// but the order is not defined to be meaningful or relied upon. Treat [`Key`]s as opaque blobs, with an unstable representation. 129 | #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Clone, Copy)] 130 | #[repr(transparent)] 131 | pub struct Key(NonZeroU32); 132 | 133 | impl Key { 134 | /// Turn the key into a u32. 135 | /// 136 | /// The only guarantee is that [`Key::try_from_repr`] is the inverse of this function, 137 | /// and will always return the same key. 138 | /// 139 | /// ``` 140 | /// use paracord::Key; 141 | /// # let paracord = paracord::ParaCord::default(); 142 | /// # let key = paracord.get_or_intern(""); 143 | /// let key2 = Key::try_from_repr(key.into_repr()).unwrap(); 144 | /// assert_eq!(key, key2); 145 | /// `````` 146 | pub fn into_repr(self) -> u32 { 147 | self.0.get() ^ u32::MAX 148 | } 149 | 150 | /// Recreate the key from a u32. 151 | /// 152 | /// The only guarantee is that [`Key::into_repr`] is the inverse of this function, 153 | /// and will always return the same u32. 154 | pub fn try_from_repr(x: u32) -> Option { 155 | NonZeroU32::new(x ^ u32::MAX).map(Self) 156 | } 157 | 158 | /// Safety: i must be less than u32::MAX 159 | unsafe fn new_unchecked(i: u32) -> Self { 160 | // SAFETY: from caller 161 | Key(unsafe { NonZeroU32::new_unchecked(i ^ u32::MAX) }) 162 | } 163 | 164 | fn from_index(i: usize) -> Self { 165 | if usize::BITS >= 32 { 166 | assert!(i < u32::MAX as usize); 167 | } 168 | 169 | // SAFETY: checked it is less than u32::MAX. 170 | unsafe { Self::new_unchecked(i as u32) } 171 | } 172 | } 173 | 174 | /// [`ParaCord`] is a lightweight, thread-safe, memory efficient [string interer](https://en.wikipedia.org/wiki/String_interning). 175 | /// 176 | /// When calling [`ParaCord::get_or_intern`], a [`Key`] is returned. This [`Key`] is guaranteed to be unique if the input string is unique, 177 | /// and is guaranteed to be the same if the input string is the same. [`Key`] is 32bits, and has a niche value which allows `Option` to 178 | /// also be 32bits. 179 | /// 180 | /// If you don't want to intern the string, but check for it's existence, you can use [`ParaCord::get`], which returns `None` if not 181 | /// present. 182 | /// 183 | /// [`Key`]s can be exchanged back into strings using [`ParaCord::resolve`]. It's important to keep in mind that this might panic 184 | /// or return nonsense results if given a key returned by some other [`ParaCord`] instance. 185 | /// 186 | /// This string interner is not garbage collected, so strings that are allocated in the interner are not released 187 | /// until the [`ParaCord`] instance is dropped. 188 | /// 189 | /// # Examples 190 | /// 191 | /// ``` 192 | /// use paracord::ParaCord; 193 | /// 194 | /// let paracord = ParaCord::default(); 195 | /// 196 | /// let foo = paracord.get_or_intern("foo"); 197 | /// let bar = paracord.get_or_intern("bar"); 198 | /// 199 | /// assert_ne!(foo, bar); 200 | /// 201 | /// // returns the same key, no insert 202 | /// let foo2 = paracord.get_or_intern("foo"); 203 | /// assert_eq!(foo, foo2); 204 | /// 205 | /// // returns the same key, guaranteed no insert 206 | /// let foo3 = paracord.get("foo").unwrap(); 207 | /// assert_eq!(foo, foo3); 208 | /// 209 | /// // can be exchanged for the string 210 | /// assert_eq!(paracord.resolve(foo), "foo"); 211 | /// assert_eq!(paracord.resolve(bar), "bar"); 212 | /// ``` 213 | pub struct ParaCord { 214 | inner: slice::ParaCord, 215 | } 216 | 217 | impl fmt::Debug for ParaCord { 218 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 219 | f.debug_map().entries(self.iter()).finish() 220 | } 221 | } 222 | 223 | impl Default for ParaCord { 224 | fn default() -> Self { 225 | Self::with_hasher(Default::default()) 226 | } 227 | } 228 | 229 | impl ParaCord { 230 | /// Create a new `ParaCord` instance with the given hasher state. 231 | /// 232 | /// # Examples 233 | /// 234 | /// ``` 235 | /// use paracord::ParaCord; 236 | /// use std::hash::RandomState; 237 | /// 238 | /// let paracord = ParaCord::with_hasher(RandomState::default()); 239 | /// 240 | /// let foo = paracord.get_or_intern("foo"); 241 | /// assert_eq!(paracord.resolve(foo), "foo"); 242 | /// ``` 243 | pub fn with_hasher(hasher: S) -> Self { 244 | Self { 245 | inner: slice::ParaCord::with_hasher(hasher), 246 | } 247 | } 248 | 249 | /// Try and get the [`Key`] associated with the given string. 250 | /// Returns [`None`] if not found. 251 | /// 252 | /// # Examples 253 | /// 254 | /// ``` 255 | /// use paracord::ParaCord; 256 | /// let paracord = ParaCord::default(); 257 | /// let foo = paracord.get_or_intern("foo"); 258 | /// 259 | /// assert_eq!(paracord.get("foo"), Some(foo)); 260 | /// assert_eq!(paracord.get("bar"), None); 261 | /// ``` 262 | pub fn get(&self, s: &str) -> Option { 263 | self.inner.get(s.as_bytes()) 264 | } 265 | 266 | /// Try and get the [`Key`] associated with the given string. 267 | /// Allocates a new key if not found. 268 | /// 269 | /// # Examples 270 | /// 271 | /// ``` 272 | /// use paracord::ParaCord; 273 | /// let paracord = ParaCord::default(); 274 | /// 275 | /// let foo = paracord.get_or_intern("foo"); 276 | /// let bar = paracord.get_or_intern("bar"); 277 | /// let foo2 = paracord.get_or_intern("foo"); 278 | /// 279 | /// assert_ne!(foo, bar); 280 | /// assert_eq!(foo, foo2); 281 | /// ``` 282 | pub fn get_or_intern(&self, s: &str) -> Key { 283 | self.inner.get_or_intern(s.as_bytes()) 284 | } 285 | } 286 | 287 | impl ParaCord { 288 | /// Try and resolve the string associated with this [`Key`]. 289 | /// 290 | /// This can only return `None` if given a key that was allocated from 291 | /// a different [`ParaCord`] instance, but it might return an arbitrary string 292 | /// as well. 293 | /// 294 | /// # Examples 295 | /// 296 | /// ``` 297 | /// use paracord::ParaCord; 298 | /// let paracord = ParaCord::default(); 299 | /// 300 | /// let foo = paracord.get_or_intern("foo"); 301 | /// assert_eq!(paracord.try_resolve(foo), Some("foo")); 302 | /// 303 | /// let paracord = ParaCord::default(); 304 | /// assert_eq!(paracord.try_resolve(foo), None); 305 | /// ``` 306 | pub fn try_resolve(&self, key: Key) -> Option<&str> { 307 | self.inner 308 | .try_resolve(key) 309 | // Safety: we insert only strings, so it's valid utf8 310 | .map(|s| unsafe { core::str::from_utf8_unchecked(s) }) 311 | } 312 | 313 | /// Resolve the string associated with this [`Key`]. 314 | /// 315 | /// # Panics 316 | /// This can panic if given a key that was allocated from 317 | /// a different [`ParaCord`] instance, but it might return an arbitrary string 318 | /// as well. 319 | /// 320 | /// # Examples 321 | /// 322 | /// ``` 323 | /// use paracord::ParaCord; 324 | /// let paracord = ParaCord::default(); 325 | /// 326 | /// let foo = paracord.get_or_intern("foo"); 327 | /// assert_eq!(paracord.resolve(foo), "foo"); 328 | /// ``` 329 | pub fn resolve(&self, key: Key) -> &str { 330 | let b = self.inner.resolve(key); 331 | 332 | // Safety: we insert only strings, so it's valid utf8 333 | unsafe { core::str::from_utf8_unchecked(b) } 334 | } 335 | 336 | /// Resolve the string associated with this [`Key`]. 337 | /// 338 | /// # Safety 339 | /// This key must have been allocated in this paracord instance, 340 | /// and [`ParaCord::clear`] must not have been called. 341 | /// 342 | /// # Examples 343 | /// 344 | /// ``` 345 | /// use paracord::ParaCord; 346 | /// let paracord = ParaCord::default(); 347 | /// 348 | /// let foo = paracord.get_or_intern("foo"); 349 | /// // Safety: `foo` was allocated within paracord just above, 350 | /// // and we never clear the paracord instance. 351 | /// assert_eq!(unsafe { paracord.resolve_unchecked(foo) }, "foo"); 352 | /// ``` 353 | pub unsafe fn resolve_unchecked(&self, key: Key) -> &str { 354 | // Safety: from caller. 355 | let b = unsafe { self.inner.resolve_unchecked(key) }; 356 | 357 | // Safety: we insert only strings, so it's valid utf8 358 | unsafe { core::str::from_utf8_unchecked(b) } 359 | } 360 | 361 | /// Determine how many strings have been allocated 362 | /// 363 | /// # Examples 364 | /// 365 | /// ``` 366 | /// use paracord::ParaCord; 367 | /// let paracord = ParaCord::default(); 368 | /// 369 | /// let _ = paracord.get_or_intern("foo"); 370 | /// assert_eq!(paracord.len(), 1); 371 | /// ``` 372 | pub fn len(&self) -> usize { 373 | self.inner.len() 374 | } 375 | 376 | /// Determine if no strings have been allocated 377 | /// 378 | /// # Examples 379 | /// 380 | /// ``` 381 | /// use paracord::ParaCord; 382 | /// let paracord = ParaCord::default(); 383 | /// 384 | /// assert!(paracord.is_empty()); 385 | /// 386 | /// let _ = paracord.get_or_intern("foo"); 387 | /// assert!(!paracord.is_empty()); 388 | /// ``` 389 | pub fn is_empty(&self) -> bool { 390 | self.inner.is_empty() 391 | } 392 | 393 | /// Get an iterator over every ([`Key`], [`&str`]) pair 394 | /// that has been allocated in this [`ParaCord`] instance. 395 | /// 396 | /// # Examples 397 | /// 398 | /// ``` 399 | /// use paracord::ParaCord; 400 | /// let paracord = ParaCord::default(); 401 | /// 402 | /// let foo = paracord.get_or_intern("foo"); 403 | /// let bar = paracord.get_or_intern("bar"); 404 | /// 405 | /// let entries: Vec<_> = paracord.iter().collect(); 406 | /// assert_eq!(entries, vec![(foo, "foo"), (bar, "bar")]); 407 | /// ``` 408 | pub fn iter(&self) -> impl Iterator { 409 | self.into_iter() 410 | } 411 | 412 | /// Deallocate all interned strings, but can retain some allocated memory 413 | /// 414 | /// # Examples 415 | /// 416 | /// ``` 417 | /// use paracord::ParaCord; 418 | /// let mut paracord = ParaCord::default(); 419 | /// 420 | /// let foo = paracord.get_or_intern("foo"); 421 | /// assert_eq!(paracord.try_resolve(foo), Some("foo")); 422 | /// 423 | /// paracord.clear(); 424 | /// assert!(paracord.is_empty()); 425 | /// 426 | /// assert_eq!(paracord.try_resolve(foo), None); 427 | /// ``` 428 | pub fn clear(&mut self) { 429 | self.inner.clear(); 430 | } 431 | 432 | #[cfg(test)] 433 | /// Determine how much space has been used to allocate all the strings. 434 | /// 435 | /// # Examples 436 | /// 437 | /// ``` 438 | /// use paracord::ParaCord; 439 | /// let mut paracord = ParaCord::default(); 440 | /// 441 | /// let _mem = paracord.current_memory_usage(); 442 | /// ``` 443 | pub(crate) fn current_memory_usage(&mut self) -> usize { 444 | self.inner.current_memory_usage() 445 | } 446 | } 447 | 448 | impl Index for ParaCord { 449 | type Output = str; 450 | 451 | fn index(&self, index: Key) -> &Self::Output { 452 | self.resolve(index) 453 | } 454 | } 455 | 456 | mod iter_private { 457 | use crate::Key; 458 | 459 | pub struct Iter<'a> { 460 | pub(crate) inner: crate::slice::iter_private::Iter<'a, u8>, 461 | } 462 | 463 | impl<'a> Iterator for Iter<'a> { 464 | type Item = (Key, &'a str); 465 | 466 | fn next(&mut self) -> Option { 467 | let (key, s) = self.inner.next()?; 468 | // Safety: we insert only strings, so it's valid utf8 469 | Some(unsafe { (key, core::str::from_utf8_unchecked(s)) }) 470 | } 471 | } 472 | } 473 | 474 | impl<'a, S> IntoIterator for &'a ParaCord { 475 | type Item = (Key, &'a str); 476 | type IntoIter = iter_private::Iter<'a>; 477 | 478 | fn into_iter(self) -> Self::IntoIter { 479 | iter_private::Iter { 480 | inner: self.inner.into_iter(), 481 | } 482 | } 483 | } 484 | 485 | #[cfg(test)] 486 | mod tests { 487 | use std::{ 488 | collections::hash_map::RandomState, 489 | sync::{Arc, Barrier}, 490 | thread, 491 | }; 492 | 493 | use crate::ParaCord; 494 | use crate::{DefaultKey, Key}; 495 | 496 | #[test] 497 | fn works() { 498 | let paracord = ParaCord::default(); 499 | 500 | let foo = paracord.get_or_intern("foo"); 501 | let bar = paracord.get_or_intern("bar"); 502 | let foo2 = paracord.get_or_intern("foo"); 503 | 504 | assert_eq!(foo, foo2); 505 | assert_ne!(foo, bar); 506 | assert_eq!(paracord.resolve(foo), "foo"); 507 | assert_eq!(paracord.resolve(bar), "bar"); 508 | } 509 | 510 | #[test] 511 | fn with_hasher() { 512 | let rodeo: ParaCord = ParaCord::with_hasher(RandomState::new()); 513 | 514 | let key = rodeo.get_or_intern("Test"); 515 | assert_eq!("Test", rodeo.resolve(key)); 516 | } 517 | 518 | #[test] 519 | fn get_or_intern() { 520 | let rodeo = ParaCord::default(); 521 | 522 | let a = rodeo.get_or_intern("A"); 523 | assert_eq!(a, rodeo.get_or_intern("A")); 524 | 525 | let b = rodeo.get_or_intern("B"); 526 | assert_eq!(b, rodeo.get_or_intern("B")); 527 | 528 | let c = rodeo.get_or_intern("C"); 529 | assert_eq!(c, rodeo.get_or_intern("C")); 530 | } 531 | 532 | #[test] 533 | #[cfg(not(miri))] 534 | fn get_or_intern_threaded() { 535 | let rodeo = Arc::new(ParaCord::default()); 536 | 537 | let moved = Arc::clone(&rodeo); 538 | thread::spawn(move || { 539 | let a = moved.get_or_intern("A"); 540 | assert_eq!(a, moved.get_or_intern("A")); 541 | 542 | let b = moved.get_or_intern("B"); 543 | assert_eq!(b, moved.get_or_intern("B")); 544 | 545 | let c = moved.get_or_intern("C"); 546 | assert_eq!(c, moved.get_or_intern("C")); 547 | }); 548 | 549 | let a = rodeo.get_or_intern("A"); 550 | assert_eq!(a, rodeo.get_or_intern("A")); 551 | 552 | let b = rodeo.get_or_intern("B"); 553 | assert_eq!(b, rodeo.get_or_intern("B")); 554 | 555 | let c = rodeo.get_or_intern("C"); 556 | assert_eq!(c, rodeo.get_or_intern("C")); 557 | } 558 | 559 | #[test] 560 | fn get() { 561 | let rodeo = ParaCord::default(); 562 | let key = rodeo.get_or_intern("A"); 563 | 564 | assert_eq!(Some(key), rodeo.get("A")); 565 | } 566 | 567 | #[test] 568 | #[cfg(not(miri))] 569 | fn get_threaded() { 570 | let rodeo = Arc::new(ParaCord::default()); 571 | let key = rodeo.get_or_intern("A"); 572 | 573 | let moved = Arc::clone(&rodeo); 574 | thread::spawn(move || { 575 | assert_eq!(Some(key), moved.get("A")); 576 | }); 577 | 578 | assert_eq!(Some(key), rodeo.get("A")); 579 | } 580 | 581 | #[test] 582 | fn resolve() { 583 | let rodeo = ParaCord::default(); 584 | let key = rodeo.get_or_intern("A"); 585 | 586 | assert_eq!("A", rodeo.resolve(key)); 587 | } 588 | 589 | #[test] 590 | #[should_panic] 591 | #[cfg(not(miri))] 592 | fn resolve_panics() { 593 | let rodeo = ParaCord::default(); 594 | rodeo.resolve(Key::try_from_repr(100).unwrap()); 595 | } 596 | 597 | #[test] 598 | #[cfg(not(miri))] 599 | fn resolve_threaded() { 600 | let rodeo = Arc::new(ParaCord::default()); 601 | let key = rodeo.get_or_intern("A"); 602 | 603 | let moved = Arc::clone(&rodeo); 604 | thread::spawn(move || { 605 | assert_eq!("A", moved.resolve(key)); 606 | }); 607 | 608 | assert_eq!("A", rodeo.resolve(key)); 609 | } 610 | 611 | #[test] 612 | #[cfg(not(any(miri)))] 613 | fn resolve_panics_threaded() { 614 | let rodeo = Arc::new(ParaCord::default()); 615 | let key = rodeo.get_or_intern("A"); 616 | 617 | let moved = Arc::clone(&rodeo); 618 | let handle = thread::spawn(move || { 619 | assert_eq!("A", moved.resolve(key)); 620 | moved.resolve(Key::try_from_repr(100).unwrap()); 621 | }); 622 | 623 | assert_eq!("A", rodeo.resolve(key)); 624 | assert!(handle.join().is_err()); 625 | } 626 | 627 | #[test] 628 | fn try_resolve() { 629 | let rodeo = ParaCord::default(); 630 | let key = rodeo.get_or_intern("A"); 631 | 632 | assert_eq!(Some("A"), rodeo.try_resolve(key)); 633 | assert_eq!(None, rodeo.try_resolve(Key::try_from_repr(100).unwrap())); 634 | } 635 | 636 | #[test] 637 | #[cfg(not(miri))] 638 | fn try_resolve_threaded() { 639 | let rodeo = Arc::new(ParaCord::default()); 640 | let key = rodeo.get_or_intern("A"); 641 | 642 | let moved = Arc::clone(&rodeo); 643 | thread::spawn(move || { 644 | assert_eq!(Some("A"), moved.try_resolve(key)); 645 | assert_eq!(None, moved.try_resolve(Key::try_from_repr(100).unwrap())); 646 | }); 647 | 648 | assert_eq!(Some("A"), rodeo.try_resolve(key)); 649 | assert_eq!(None, rodeo.try_resolve(Key::try_from_repr(100).unwrap())); 650 | } 651 | 652 | #[test] 653 | fn len() { 654 | let rodeo = ParaCord::default(); 655 | rodeo.get_or_intern("A"); 656 | rodeo.get_or_intern("B"); 657 | rodeo.get_or_intern("C"); 658 | 659 | assert_eq!(rodeo.len(), 3); 660 | } 661 | 662 | #[test] 663 | fn empty() { 664 | let rodeo = ParaCord::default(); 665 | 666 | assert!(rodeo.is_empty()); 667 | } 668 | 669 | #[test] 670 | fn drops() { 671 | let _ = ParaCord::default(); 672 | } 673 | 674 | #[test] 675 | #[cfg(not(miri))] 676 | fn drop_threaded() { 677 | let rodeo = Arc::new(ParaCord::default()); 678 | 679 | let moved = Arc::clone(&rodeo); 680 | thread::spawn(move || { 681 | let _ = moved; 682 | }); 683 | } 684 | 685 | #[test] 686 | fn memory() { 687 | let mut rodeo = ParaCord::default(); 688 | rodeo.get_or_intern("A"); 689 | rodeo.get_or_intern("B"); 690 | rodeo.get_or_intern("C"); 691 | 692 | assert!(rodeo.current_memory_usage() > 0); 693 | } 694 | 695 | #[test] 696 | fn clear() { 697 | let mut rodeo = ParaCord::default(); 698 | let k1 = rodeo.get_or_intern("A"); 699 | rodeo.clear(); 700 | 701 | assert!(rodeo.try_resolve(k1).is_none()); 702 | assert!(rodeo.is_empty()); 703 | } 704 | 705 | // #[test] 706 | // #[cfg(not(miri))] 707 | // fn debug() { 708 | // let rodeo = ParaCord::default(); 709 | // println!("{:?}", rodeo); 710 | // } 711 | 712 | #[test] 713 | fn iter() { 714 | let rodeo = ParaCord::default(); 715 | rodeo.get_or_intern("A"); 716 | rodeo.get_or_intern("B"); 717 | rodeo.get_or_intern("C"); 718 | let values: Vec<_> = rodeo.iter().map(|(k, v)| (k.into_repr(), v)).collect(); 719 | assert_eq!(values.len(), 3); 720 | assert!(values.contains(&(0, "A"))); 721 | assert!(values.contains(&(1, "B"))); 722 | assert!(values.contains(&(2, "C"))); 723 | } 724 | 725 | #[test] 726 | fn from_iter() { 727 | let rodeo: ParaCord = ["a", "b", "c", "d", "e"].iter().collect(); 728 | 729 | assert!(rodeo.get("a").is_some()); 730 | assert!(rodeo.get("b").is_some()); 731 | assert!(rodeo.get("c").is_some()); 732 | assert!(rodeo.get("d").is_some()); 733 | assert!(rodeo.get("e").is_some()); 734 | } 735 | 736 | #[test] 737 | fn index() { 738 | let rodeo = ParaCord::default(); 739 | let key = rodeo.get_or_intern("A"); 740 | 741 | assert_eq!("A", &rodeo[key]); 742 | } 743 | 744 | #[test] 745 | fn extend() { 746 | let mut rodeo = ParaCord::default(); 747 | assert!(rodeo.is_empty()); 748 | 749 | rodeo.extend(["a", "b", "c", "d", "e"].iter()); 750 | assert!(rodeo.get("a").is_some()); 751 | assert!(rodeo.get("b").is_some()); 752 | assert!(rodeo.get("c").is_some()); 753 | assert!(rodeo.get("d").is_some()); 754 | assert!(rodeo.get("e").is_some()); 755 | } 756 | 757 | // Test for race conditions on key insertion 758 | // https://github.com/Kixiron/lasso/issues/18 759 | #[test] 760 | #[cfg(not(miri))] 761 | fn get_or_intern_threaded_racy() { 762 | const THREADS: usize = 10; 763 | 764 | let mut handles = Vec::with_capacity(THREADS); 765 | let barrier = Arc::new(Barrier::new(THREADS)); 766 | let rodeo = Arc::new(ParaCord::default()); 767 | let expected = Key::try_from_repr(0).unwrap(); 768 | 769 | for _ in 0..THREADS { 770 | let moved_rodeo = Arc::clone(&rodeo); 771 | let moved_barrier = Arc::clone(&barrier); 772 | 773 | handles.push(thread::spawn(move || { 774 | moved_barrier.wait(); 775 | assert_eq!(expected, moved_rodeo.get_or_intern("A")); 776 | assert_eq!(expected, moved_rodeo.get_or_intern("A")); 777 | assert_eq!(expected, moved_rodeo.get_or_intern("A")); 778 | assert_eq!(expected, moved_rodeo.get_or_intern("A")); 779 | })); 780 | } 781 | 782 | for handle in handles { 783 | handle.join().unwrap(); 784 | } 785 | } 786 | 787 | #[test] 788 | #[cfg(feature = "serde")] 789 | fn serde() { 790 | let key = DefaultKey::new("hello"); 791 | 792 | serde_test::assert_de_tokens(&key, &[serde_test::Token::Str("hello")]); 793 | serde_test::assert_ser_tokens(&key, &[serde_test::Token::Str("hello")]); 794 | } 795 | 796 | #[test] 797 | #[cfg(not(miri))] 798 | fn memory_usage() { 799 | use rand::rngs::StdRng; 800 | use rand::{Rng, SeedableRng}; 801 | use rand_distr::Zipf; 802 | 803 | let endpoint_dist = Zipf::new(500000.0, 0.8).unwrap(); 804 | let endpoints = StdRng::seed_from_u64(272488357).sample_iter(endpoint_dist); 805 | 806 | let mut interner = ParaCord::default(); 807 | 808 | const N: usize = 1_000_000; 809 | let mut verify = Vec::with_capacity(N); 810 | for endpoint in endpoints.take(N) { 811 | let endpoint = format!("ep-string-interning-{endpoint}"); 812 | let key = interner.get_or_intern(&endpoint); 813 | verify.push((endpoint, key)); 814 | } 815 | 816 | for (s, key) in verify { 817 | assert_eq!(interner[key], s); 818 | } 819 | 820 | let mem = interner.current_memory_usage(); 821 | let len = interner.len(); 822 | 823 | // average 86 bytes per string. 824 | // average string length is 24, so 62 bytes overhead. 825 | assert_eq!(mem / len, 86); 826 | } 827 | } 828 | -------------------------------------------------------------------------------- /paracord/src/macros.rs: -------------------------------------------------------------------------------- 1 | /// Create a new custom key, with a static-backed allocator. 2 | /// 3 | /// See [`DefaultKey`](crate::DefaultKey) for docs on what this macro generates. 4 | /// 5 | /// ## Create a custom key 6 | /// 7 | /// ``` 8 | /// paracord::custom_key!( 9 | /// /// My custom key 10 | /// pub struct MyKey; 11 | /// ); 12 | /// 13 | /// let key = MyKey::new("foo"); 14 | /// assert_eq!(key.as_str(), "foo"); 15 | /// 16 | /// let key2 = MyKey::try_new_existing("foo").unwrap(); 17 | /// assert_eq!(key, key2); 18 | /// ``` 19 | /// 20 | /// ## Create a custom key with a different default hasher 21 | /// 22 | /// ``` 23 | /// use foldhash::quality::RandomState; 24 | /// 25 | /// paracord::custom_key!( 26 | /// /// My custom key 27 | /// pub struct MyKey; 28 | /// 29 | /// let hasher: RandomState; 30 | /// ); 31 | /// ``` 32 | /// 33 | /// ## Create a custom key with a different hasher and init function 34 | /// 35 | /// ``` 36 | /// use foldhash::quality::FixedState; 37 | /// 38 | /// paracord::custom_key!( 39 | /// /// My custom key 40 | /// pub struct MyKey; 41 | /// 42 | /// let hasher: FixedState = FixedState::with_seed(1); 43 | /// ); 44 | /// ``` 45 | #[macro_export] 46 | macro_rules! custom_key { 47 | ($(#[$($meta:meta)*])* $vis:vis struct $key:ident $(;)?) => { 48 | $crate::custom_key!( 49 | $(#[$($meta)*])* 50 | $vis struct $key; 51 | 52 | let hasher: $crate::__private::RandomState; 53 | ); 54 | }; 55 | ($(#[$($meta:meta)*])* $vis:vis struct $key:ident; let hasher: $s:ty $(;)?) => { 56 | $crate::custom_key!( 57 | $(#[$($meta)*])* 58 | $vis struct $key; 59 | 60 | let hasher: $s = <$s as ::core::default::Default>::default(); 61 | ); 62 | }; 63 | ($(#[$($meta:meta)*])* $vis:vis struct $key:ident; let hasher: $s:ty = $init:expr $(;)?) => { 64 | $(#[$($meta)*])* 65 | #[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Clone, Copy)] 66 | #[repr(transparent)] 67 | $vis struct $key($crate::Key); 68 | 69 | impl $key { 70 | fn paracord() -> &'static $crate::ParaCord<$s> { 71 | static S: ::std::sync::OnceLock<$crate::ParaCord<$s>> = ::std::sync::OnceLock::new(); 72 | S.get_or_init(|| $crate::ParaCord::with_hasher($init)) 73 | } 74 | 75 | /// Try and get the key associated with the given string. 76 | /// Returns [`None`] if not found. 77 | pub fn try_new_existing(s: &str) -> Option { 78 | Self::paracord().get(s).map(Self) 79 | } 80 | 81 | /// Create a new key associated with the given string. 82 | /// Returns the same key if called repeatedly. 83 | pub fn new(s: &str) -> Self { 84 | Self(Self::paracord().get_or_intern(s)) 85 | } 86 | 87 | /// Resolve the string associated with this key. 88 | pub fn as_str(&self) -> &'static str { 89 | // Safety: The key can only be constructed from the static paracord, 90 | // and the paracord will never be reset. 91 | unsafe { Self::paracord().resolve_unchecked(self.0) } 92 | } 93 | 94 | /// Determine how many keys have been allocated 95 | pub fn count() -> usize { 96 | Self::paracord().len() 97 | } 98 | 99 | /// Get an iterator over every 100 | #[doc = concat!("(`",stringify!($key),"`, `&str`)")] 101 | /// pair that has been allocated. 102 | pub fn iter() -> impl Iterator { 103 | Self::paracord().iter().map(|(k, s)| (Self(k), s)) 104 | } 105 | } 106 | 107 | /// Displays the string that this key represents. 108 | impl ::core::fmt::Display for $key { 109 | fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { 110 | f.write_str(self.as_str()) 111 | } 112 | } 113 | 114 | impl ::core::convert::AsRef for $key { 115 | fn as_ref(&self) -> &str { 116 | self.as_str() 117 | } 118 | } 119 | 120 | impl ::core::ops::Deref for $key { 121 | type Target = str; 122 | fn deref(&self) -> &str { 123 | self.as_str() 124 | } 125 | } 126 | 127 | $crate::__private::serde::custom_key_serde!($key); 128 | }; 129 | } 130 | 131 | #[cfg(test)] 132 | mod tests { 133 | #[test] 134 | fn misc() { 135 | custom_key!(pub struct Foo); 136 | 137 | let _foo = Foo::new("foo"); 138 | let foo = Foo::try_new_existing("foo").unwrap(); 139 | 140 | assert_eq!(foo.to_string(), "foo"); 141 | assert_eq!(foo.as_ref(), "foo"); 142 | assert_eq!(Foo::count(), 1); 143 | let keys: Vec<_> = Foo::iter().collect(); 144 | assert_eq!(keys, [(foo, "foo")]); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /paracord/src/serde.rs: -------------------------------------------------------------------------------- 1 | use std::hash::{BuildHasher, Hash}; 2 | 3 | use crate::{slice, Key, ParaCord}; 4 | use serde::de::{DeserializeSeed, Visitor}; 5 | 6 | pub struct SerdeVisitor<'a, S>(pub &'a ParaCord); 7 | 8 | impl Visitor<'_> for SerdeVisitor<'_, S> { 9 | type Value = Key; 10 | 11 | fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { 12 | formatter.write_str("a string value") 13 | } 14 | 15 | fn visit_str(self, v: &str) -> Result 16 | where 17 | E: serde::de::Error, 18 | { 19 | Ok(self.0.get_or_intern(v)) 20 | } 21 | } 22 | 23 | impl<'de, S: BuildHasher> DeserializeSeed<'de> for &ParaCord { 24 | type Value = Key; 25 | 26 | fn deserialize(self, deserializer: D) -> Result 27 | where 28 | D: Deserializer<'de>, 29 | { 30 | deserializer.deserialize_str(SerdeVisitor(self)) 31 | } 32 | } 33 | 34 | impl<'de, T: Deserialize<'de> + Hash + Eq + Copy, S: BuildHasher> DeserializeSeed<'de> 35 | for &slice::ParaCord 36 | { 37 | type Value = Key; 38 | 39 | fn deserialize(self, deserializer: D) -> Result 40 | where 41 | D: Deserializer<'de>, 42 | { 43 | let v = Vec::::deserialize(deserializer)?; 44 | Ok(self.get_or_intern(&v)) 45 | } 46 | } 47 | 48 | #[doc(hidden)] 49 | #[macro_export] 50 | macro_rules! custom_key_serde { 51 | ($key:ident) => { 52 | const _: () = { 53 | use $crate::__private::serde::{ 54 | Deserialize, Deserializer, SerdeVisitor, Serialize, Serializer, 55 | }; 56 | 57 | /// Serialize the key as the interned-string 58 | impl Serialize for $key { 59 | fn serialize(&self, serializer: S) -> ::core::result::Result 60 | where 61 | S: Serializer, 62 | { 63 | serializer.serialize_str(self.as_str()) 64 | } 65 | } 66 | 67 | /// Deserializes and interns a string 68 | impl<'de> Deserialize<'de> for $key { 69 | fn deserialize(deserializer: D) -> ::core::result::Result 70 | where 71 | D: Deserializer<'de>, 72 | { 73 | deserializer 74 | .deserialize_str(SerdeVisitor(Self::paracord())) 75 | .map(Self) 76 | } 77 | } 78 | }; 79 | }; 80 | } 81 | 82 | pub use { 83 | custom_key_serde, 84 | serde::{Deserialize, Deserializer, Serialize, Serializer}, 85 | }; 86 | -------------------------------------------------------------------------------- /paracord/src/slice/alloc.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | hash::{BuildHasher, Hash}, 3 | mem::MaybeUninit, 4 | }; 5 | 6 | use hashbrown::hash_table::Entry; 7 | use sync_wrapper::SyncWrapper; 8 | use typed_arena::Arena; 9 | 10 | use crate::{slice::ParaCord, Key}; 11 | 12 | use super::TableEntry; 13 | 14 | pub(super) struct Alloc(SyncWrapper>); 15 | 16 | impl Default for Alloc { 17 | fn default() -> Self { 18 | Self(SyncWrapper::new(Arena::new())) 19 | } 20 | } 21 | 22 | impl Alloc { 23 | #[cfg(test)] 24 | pub(super) fn size(&mut self) -> usize { 25 | self.0.get_mut().len() * std::mem::size_of::() 26 | } 27 | } 28 | 29 | /// Represents a `&'_ [T]`, with a length limited to u32 and with an 30 | /// undescribed lifetime because it's technically self-ref. 31 | #[derive(Clone, Copy)] 32 | #[repr(packed)] 33 | pub(super) struct InternedPtr { 34 | ptr: *const T, 35 | len: u32, 36 | } 37 | 38 | // Safety: `VecEntry` has the same safety requirements as `&[T]` 39 | unsafe impl Sync for InternedPtr {} 40 | // Safety: `VecEntry` has the same safety requirements as `&[T]` 41 | unsafe impl Send for InternedPtr {} 42 | 43 | impl InternedPtr { 44 | fn new(s: &[T]) -> Self { 45 | let len = u32::try_from(s.len()).expect("slice lengths must be less than u32::MAX"); 46 | Self { 47 | len, 48 | ptr: s.as_ptr(), 49 | } 50 | } 51 | 52 | pub(super) fn slice(&self) -> &[T] { 53 | // Safety: the ptr and len came from a &[T] to begin with. 54 | unsafe { &*core::ptr::slice_from_raw_parts(self.ptr, self.len as usize) } 55 | } 56 | } 57 | 58 | impl Alloc { 59 | #[inline] 60 | fn alloc(&mut self, s: &[T]) -> &mut [T] { 61 | /// Polyfill for [`MaybeUninit::copy_from_slice`] 62 | fn copy_from_slice<'a, T: Copy>(this: &'a mut [MaybeUninit], src: &[T]) -> &'a mut [T] { 63 | // SAFETY: &[T] and &[MaybeUninit] have the same layout 64 | let uninit_src: &[MaybeUninit] = unsafe { core::mem::transmute(src) }; 65 | 66 | this.copy_from_slice(uninit_src); 67 | 68 | // SAFETY: Valid elements have just been copied into `this` so it is initialized 69 | unsafe { slice_assume_init_mut(this) } 70 | } 71 | 72 | /// Polyfill for [`MaybeUninit::slice_assume_init_mut`] 73 | const unsafe fn slice_assume_init_mut(slice: &mut [MaybeUninit]) -> &mut [T] { 74 | // SAFETY: similar to safety notes for `slice_get_ref`, but we have a 75 | // mutable reference which is also guaranteed to be valid for writes. 76 | unsafe { &mut *(slice as *mut [MaybeUninit] as *mut [T]) } 77 | } 78 | 79 | let arena = self.0.get_mut(); 80 | 81 | // Safety: we are making sure to init all the elements without panicking. 82 | let uninit = unsafe { arena.alloc_uninitialized(s.len()) }; 83 | copy_from_slice(uninit, s) 84 | } 85 | } 86 | 87 | impl ParaCord { 88 | #[cold] 89 | pub(super) fn intern_slow(&self, s: &[T], hash: u64) -> Key { 90 | let _len = u32::try_from(s.len()).expect("slice lengths must be less than u32::MAX"); 91 | 92 | let shard = &mut *self.slice_to_keys.get_write_shard(hash); 93 | match shard.table.entry(hash, |k| s == k.slice(), |k| k.hash) { 94 | Entry::Occupied(entry) => entry.get().key, 95 | Entry::Vacant(entry) => { 96 | let key = self.keys_to_slice.push_with(|key| { 97 | let key = Key::from_index(key); 98 | let s = shard.alloc.alloc(s); 99 | let s = InternedPtr::new(s); 100 | entry.insert(TableEntry::new(s, key, hash)); 101 | s 102 | }); 103 | 104 | // SAFETY: as asserted the key is correct 105 | unsafe { Key::new_unchecked(key as u32) } 106 | } 107 | } 108 | } 109 | 110 | #[cold] 111 | pub(super) fn intern_slow_mut(&mut self, s: &[T], hash: u64) -> Key { 112 | let _len = u32::try_from(s.len()).expect("slice lengths must be less than u32::MAX"); 113 | 114 | let shard = &mut *self.slice_to_keys.get_mut(hash); 115 | match shard.table.entry(hash, |k| s == k.slice(), |k| k.hash) { 116 | Entry::Occupied(entry) => entry.get().key, 117 | Entry::Vacant(entry) => { 118 | let key = self.keys_to_slice.push_with(|key| { 119 | let key = Key::from_index(key); 120 | let s = shard.alloc.alloc(s); 121 | let s = InternedPtr::new(s); 122 | entry.insert(TableEntry::new(s, key, hash)); 123 | s 124 | }); 125 | 126 | // SAFETY: as asserted the key is correct 127 | unsafe { Key::new_unchecked(key as u32) } 128 | } 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /paracord/src/slice/mod.rs: -------------------------------------------------------------------------------- 1 | //! Support for interning more than just string slices 2 | //! 3 | //! ``` 4 | //! use paracord::slice::ParaCord; 5 | //! 6 | //! let paracord = ParaCord::default(); 7 | //! 8 | //! let foo = paracord.get_or_intern(&[1,2,3,4]); 9 | //! let bar = paracord.get_or_intern(&[5,6,7,8]); 10 | //! 11 | //! assert_ne!(foo, bar); 12 | //! 13 | //! // returns the same key, no insert 14 | //! let foo2 = paracord.get_or_intern(&[1,2,3,4]); 15 | //! assert_eq!(foo, foo2); 16 | //! 17 | //! // returns the same key, guaranteed no insert 18 | //! let foo3 = paracord.get(&[1,2,3,4]).unwrap(); 19 | //! assert_eq!(foo, foo3); 20 | //! 21 | //! // can be exchanged for the string 22 | //! assert_eq!(paracord.resolve(foo), &[1,2,3,4]); 23 | //! assert_eq!(paracord.resolve(bar), &[5,6,7,8]); 24 | //! ``` 25 | 26 | use core::fmt; 27 | use std::{ 28 | hash::{BuildHasher, Hash}, 29 | ops::Index, 30 | }; 31 | 32 | use alloc::{Alloc, InternedPtr}; 33 | use clashmap::ClashCollection; 34 | use hashbrown::HashTable; 35 | 36 | use crate::Key; 37 | 38 | mod alloc; 39 | 40 | /// [`ParaCord`] is a lightweight, thread-safe, memory efficient [string interer](https://en.wikipedia.org/wiki/String_interning). 41 | /// 42 | /// When calling [`ParaCord::get_or_intern`], a [`Key`] is returned. This [`Key`] is guaranteed to be unique if the input slice is unique, 43 | /// and is guaranteed to be the same if the input slice is the same. [`Key`] is 32bits, and has a niche value which allows `Option` to 44 | /// also be 32bits. 45 | /// 46 | /// If you don't want to intern the slice, but check for it's existence, you can use [`ParaCord::get`], which returns `None` if not 47 | /// present. 48 | /// 49 | /// [`Key`]s can be exchanged back into slices using [`ParaCord::resolve`]. It's important to keep in mind that this might panic 50 | /// or return nonsense results if given a key returned by some other [`ParaCord`] instance. 51 | /// 52 | /// This slice interner is not garbage collected, so slices that are allocated in the interner are not released 53 | /// until the [`ParaCord`] instance is dropped. 54 | /// 55 | /// # Examples 56 | /// 57 | /// ``` 58 | /// use paracord::slice::ParaCord; 59 | /// 60 | /// let paracord = ParaCord::default(); 61 | /// 62 | /// let foo = paracord.get_or_intern(&[1,2,3,4]); 63 | /// let bar = paracord.get_or_intern(&[5,6,7,8]); 64 | /// 65 | /// assert_ne!(foo, bar); 66 | /// 67 | /// // returns the same key, no insert 68 | /// let foo2 = paracord.get_or_intern(&[1,2,3,4]); 69 | /// assert_eq!(foo, foo2); 70 | /// 71 | /// // returns the same key, guaranteed no insert 72 | /// let foo3 = paracord.get(&[1,2,3,4]).unwrap(); 73 | /// assert_eq!(foo, foo3); 74 | /// 75 | /// // can be exchanged for the string 76 | /// assert_eq!(paracord.resolve(foo), &[1,2,3,4]); 77 | /// assert_eq!(paracord.resolve(bar), &[5,6,7,8]); 78 | /// ``` 79 | pub struct ParaCord { 80 | keys_to_slice: boxcar::Vec>, 81 | slice_to_keys: ClashCollection>, 82 | hasher: S, 83 | } 84 | 85 | impl fmt::Debug for ParaCord { 86 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 87 | f.debug_map().entries(self.iter()).finish() 88 | } 89 | } 90 | 91 | struct Collection { 92 | table: HashTable>, 93 | alloc: Alloc, 94 | } 95 | 96 | impl Default for Collection { 97 | fn default() -> Self { 98 | Self { 99 | table: Default::default(), 100 | alloc: Default::default(), 101 | } 102 | } 103 | } 104 | 105 | struct TableEntry { 106 | hash: u64, 107 | ptr: InternedPtr, 108 | key: Key, 109 | } 110 | 111 | impl TableEntry { 112 | fn new(ptr: InternedPtr, key: Key, hash: u64) -> Self { 113 | Self { hash, key, ptr } 114 | } 115 | 116 | fn slice(&self) -> &[T] { 117 | self.ptr.slice() 118 | } 119 | } 120 | 121 | impl Default for ParaCord { 122 | fn default() -> Self { 123 | Self::with_hasher(Default::default()) 124 | } 125 | } 126 | 127 | impl ParaCord { 128 | /// Create a new `ParaCord` instance with the given hasher state. 129 | /// 130 | /// # Examples 131 | /// 132 | /// ``` 133 | /// use paracord::slice::ParaCord; 134 | /// use std::hash::RandomState; 135 | /// 136 | /// let paracord = ParaCord::with_hasher(RandomState::default()); 137 | /// 138 | /// let foo = paracord.get_or_intern(&[1,2,3,4]); 139 | /// assert_eq!(paracord.resolve(foo), &[1,2,3,4]); 140 | /// ``` 141 | pub fn with_hasher(hasher: S) -> Self { 142 | Self { 143 | keys_to_slice: boxcar::Vec::default(), 144 | slice_to_keys: ClashCollection::default(), 145 | hasher, 146 | } 147 | } 148 | } 149 | 150 | impl ParaCord { 151 | /// Try and get the [`Key`] associated with the given slice. 152 | /// Returns [`None`] if not found. 153 | /// 154 | /// # Examples 155 | /// 156 | /// ``` 157 | /// use paracord::slice::ParaCord; 158 | /// let paracord = ParaCord::default(); 159 | /// let foo = paracord.get_or_intern(&[1,2,3,4]); 160 | /// 161 | /// assert_eq!(paracord.get(&[1,2,3,4]), Some(foo)); 162 | /// assert_eq!(paracord.get(&[5,6,7,8]), None); 163 | /// ``` 164 | pub fn get(&self, s: &[T]) -> Option { 165 | let hash = self.hasher.hash_one(s); 166 | let shard = self.slice_to_keys.get_read_shard(hash); 167 | shard.table.find(hash, |k| s == k.slice()).map(|k| k.key) 168 | } 169 | } 170 | 171 | impl ParaCord { 172 | /// Try and get the [`Key`] associated with the given slice. 173 | /// Allocates a new key if not found. 174 | /// 175 | /// # Examples 176 | /// 177 | /// ``` 178 | /// use paracord::slice::ParaCord; 179 | /// let paracord = ParaCord::default(); 180 | /// 181 | /// let foo = paracord.get_or_intern(&[1,2,3,4]); 182 | /// let bar = paracord.get_or_intern(&[5,6,7,8]); 183 | /// let foo2 = paracord.get_or_intern(&[1,2,3,4]); 184 | /// 185 | /// assert_ne!(foo, bar); 186 | /// assert_eq!(foo, foo2); 187 | /// ``` 188 | pub fn get_or_intern(&self, s: &[T]) -> Key { 189 | let hash = self.hasher.hash_one(s); 190 | 191 | let key = { 192 | let shard = self.slice_to_keys.get_read_shard(hash); 193 | shard.table.find(hash, |k| s == k.slice()).map(|k| k.key) 194 | }; 195 | 196 | let Some(key) = key else { 197 | return self.intern_slow(s, hash); 198 | }; 199 | key 200 | } 201 | } 202 | 203 | impl ParaCord { 204 | /// Try and resolve the slice associated with this [`Key`]. 205 | /// 206 | /// This can only return `None` if given a key that was allocated from 207 | /// a different [`ParaCord`] instance, but it might return an arbitrary slice 208 | /// as well. 209 | pub fn try_resolve(&self, key: Key) -> Option<&[T]> { 210 | let s = self.keys_to_slice.get(key.into_repr() as usize)?; 211 | Some(s.slice()) 212 | } 213 | 214 | /// Resolve the slice associated with this [`Key`]. 215 | /// 216 | /// # Panics 217 | /// This can panic if given a key that was allocated from 218 | /// a different [`ParaCord`] instance, but it might return an arbitrary slice 219 | /// as well. 220 | pub fn resolve(&self, key: Key) -> &[T] { 221 | self.keys_to_slice[key.into_repr() as usize].slice() 222 | } 223 | 224 | /// Resolve the slice associated with this [`Key`]. 225 | /// 226 | /// # Safety 227 | /// This key must have been allocated in this paracord instance, 228 | /// and [`ParaCord::clear`] must not have been called. 229 | pub unsafe fn resolve_unchecked(&self, key: Key) -> &[T] { 230 | // Safety: If the key was allocated in self, then key is inbounds. 231 | unsafe { self.keys_to_slice.get_unchecked(key.into_repr() as usize) }.slice() 232 | } 233 | } 234 | 235 | impl ParaCord { 236 | /// Determine how many slices have been allocated 237 | pub fn len(&self) -> usize { 238 | self.keys_to_slice.count() 239 | } 240 | 241 | /// Determine if no slices have been allocated 242 | pub fn is_empty(&self) -> bool { 243 | self.keys_to_slice.is_empty() 244 | } 245 | 246 | /// Get an iterator over every ([`Key`], `&[T]`) pair 247 | /// that has been allocated in this [`ParaCord`] instance. 248 | pub fn iter(&self) -> impl Iterator { 249 | self.into_iter() 250 | } 251 | 252 | /// Deallocate all interned slices, but can retain some allocated memory 253 | pub fn clear(&mut self) { 254 | self.keys_to_slice.clear(); 255 | self.slice_to_keys.shards_mut().iter_mut().for_each(|s| { 256 | s.get_mut().table.clear(); 257 | drop(core::mem::take(&mut s.get_mut().alloc)) 258 | }); 259 | } 260 | 261 | #[cfg(test)] 262 | /// Determine how much space has been used to allocate all the slices. 263 | pub(crate) fn current_memory_usage(&mut self) -> usize { 264 | let keys_size = self.keys_to_slice.count() * core::mem::size_of::<*const str>(); 265 | 266 | let shards_size = { 267 | let acc = core::mem::size_of_val(self.slice_to_keys.shards()); 268 | self.slice_to_keys 269 | .shards_mut() 270 | .iter_mut() 271 | .fold(acc, |acc, shard| { 272 | let shard = shard.get_mut(); 273 | acc + shard.table.allocation_size() + shard.alloc.size() 274 | }) 275 | }; 276 | 277 | size_of::() + keys_size + shards_size 278 | } 279 | } 280 | 281 | impl, S: BuildHasher + Default> FromIterator 282 | for ParaCord 283 | { 284 | fn from_iter>(iter: A) -> Self { 285 | let iter = iter.into_iter(); 286 | let len = iter.size_hint().0; 287 | 288 | let mut this = Self { 289 | keys_to_slice: boxcar::Vec::with_capacity(len), 290 | slice_to_keys: ClashCollection::default(), 291 | hasher: S::default(), 292 | }; 293 | this.extend(iter); 294 | this 295 | } 296 | } 297 | 298 | impl, S: BuildHasher> Extend for ParaCord { 299 | fn extend>(&mut self, iter: A) { 300 | // assumption, the iterator has mostly unique entries, thus this should always use the slow insert mode. 301 | for s in iter { 302 | let s = s.as_ref(); 303 | let hash = self.hasher.hash_one(s); 304 | self.intern_slow_mut(s, hash); 305 | } 306 | } 307 | } 308 | 309 | impl, S: BuildHasher + Default> FromIterator for crate::ParaCord { 310 | fn from_iter>(iter: A) -> Self { 311 | let iter = iter.into_iter(); 312 | let len = iter.size_hint().0; 313 | 314 | let mut this = Self { 315 | inner: ParaCord { 316 | keys_to_slice: boxcar::Vec::with_capacity(len), 317 | slice_to_keys: ClashCollection::default(), 318 | hasher: S::default(), 319 | }, 320 | }; 321 | this.extend(iter); 322 | this 323 | } 324 | } 325 | 326 | impl, S: BuildHasher> Extend for crate::ParaCord { 327 | fn extend>(&mut self, iter: A) { 328 | // assumption, the iterator has mostly unique entries, thus this should always use the slow insert mode. 329 | for s in iter { 330 | let s = s.as_ref().as_bytes(); 331 | let hash = self.inner.hasher.hash_one(s); 332 | self.inner.intern_slow_mut(s, hash); 333 | } 334 | } 335 | } 336 | 337 | impl Index for ParaCord { 338 | type Output = [T]; 339 | 340 | fn index(&self, index: Key) -> &Self::Output { 341 | self.resolve(index) 342 | } 343 | } 344 | 345 | pub(crate) mod iter_private { 346 | use crate::Key; 347 | 348 | use super::InternedPtr; 349 | 350 | pub struct Iter<'a, T> { 351 | pub(super) inner: boxcar::Iter<'a, InternedPtr>, 352 | } 353 | 354 | impl<'a, T> Iterator for Iter<'a, T> { 355 | type Item = (Key, &'a [T]); 356 | 357 | fn next(&mut self) -> Option { 358 | let (key, s) = self.inner.next()?; 359 | // SAFETY: we assume the key is correct given its existence in the set 360 | Some(unsafe { (Key::new_unchecked(key as u32), s.slice()) }) 361 | } 362 | } 363 | } 364 | 365 | impl<'a, T, S> IntoIterator for &'a ParaCord { 366 | type Item = (Key, &'a [T]); 367 | type IntoIter = iter_private::Iter<'a, T>; 368 | 369 | fn into_iter(self) -> Self::IntoIter { 370 | iter_private::Iter { 371 | inner: self.keys_to_slice.iter(), 372 | } 373 | } 374 | } 375 | 376 | #[cfg(test)] 377 | mod tests { 378 | use super::ParaCord; 379 | 380 | fn is_send() {} 381 | fn is_sync() {} 382 | 383 | fn inner_check_send_sync() { 384 | is_send::>(); 385 | is_sync::>(); 386 | } 387 | 388 | #[test] 389 | fn check_send_sync() { 390 | inner_check_send_sync::<()>(); 391 | } 392 | } 393 | --------------------------------------------------------------------------------