1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
//! This crate implements an LRU (least-recently-used) cache that is limited by
//! the total size of its entries. As more entries are added than fit in the
//! specified memory bound, the least-recently-used ones are ejected. The cache
//! supports average-case O(1) insertion, retrieval, and removal.
//!
//! Note that the memory required for each entry is only an estimate and some
//! auxiliary structure is disregarded. With some data structures (such as the
//! [HashMap](std::collections::HashMap) or
//! [HashSet](std::collections::HashSet)), some internal data is not
//! accessible, so the required memory is even more underestimated. Therefore,
//! the actual data structure can take more memory than was assigned, however
//! this should not be an excessive amount in most cases.
//!
//! # Motivating example
//!
//! Imagine we are building a web server that sends large responses to clients.
//! To reduce the load, they are split into sections and the client is given a
//! token to access the different sections individually. However, recomputing
//! the sections on each request leads to too much server load, so they need to
//! be cached. An LRU cache is useful in this situation, as clients are most
//! likely to request new sections temporally localized.
//!
//! Now consider the situation when most responses are very small, but some may
//! be large. This would either lead to the cache being conservatively sized
//! and allow for less cached responses than would normally be possible, or to
//! the cache being liberally sized and potentially overflow memory if too many
//! large responses have to be cached. To prevent this, the cache is designed
//! with an upper bound on its memory instead of the number of elements.
//!
//! The code below shows how the basic structure might look like.
//!
//! ```
//! use lru_mem::LruCache;
//!
//! struct WebServer {
//! cache: LruCache<u128, Vec<String>>
//! }
//!
//! fn random_token() -> u128 {
//! // A cryptographically secure random token.
//! 42
//! }
//!
//! fn generate_sections(input: String) -> Vec<String> {
//! // A complicated set of sections that is highly variable in size.
//! vec![input.clone(), input]
//! }
//!
//! impl WebServer {
//! fn new(max_size: usize) -> WebServer {
//! // Create a new web server with a cache that holds at most max_size
//! // bytes of elements.
//! WebServer {
//! cache: LruCache::new(max_size)
//! }
//! }
//!
//! fn on_query(&mut self, input: String) -> u128 {
//! // Generate sections, store them in the cache, and return token.
//! let token = random_token();
//! let sections = generate_sections(input);
//! self.cache.insert(token, sections)
//! .expect("sections do not fit in the cache");
//!
//! token
//! }
//!
//! fn on_section_request(&mut self, token: u128, index: usize)
//! -> Option<&String> {
//! // Lookup the token and get the section with given index.
//! self.cache.get(&token).and_then(|s| s.get(index))
//! }
//! }
//! ```
//!
//! For further details on how to use the cache, see the [LruCache] struct.
use std::borrow::Borrow;
use std::fmt::{self, Debug, Formatter};
use std::hash::{BuildHasher, Hash};
use std::mem;
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::raw::RawTable;
use hashbrown::TryReserveError;
use entry::{Entry, EntryPtr, UnhingedEntry};
pub use entry::entry_size;
pub use error::{InsertError, MutateError, TryInsertError};
pub use iter::{Drain, IntoIter, IntoKeys, IntoValues, Iter, Keys, Values};
pub use mem_size::{HeapSize, MemSize, ValueSize};
mod entry;
mod error;
mod iter;
mod mem_size;
/// An LRU (least-recently-used) cache that stores values associated with keys.
/// Insertion, retrieval, and removal all have average-case complexity in O(1).
/// The cache has an upper memory bound, which is set at construction time.
/// This is enforced using estimates on the memory requirement of each
/// key-value-pair. Note that some auxiliary data structures may allocate more
/// memory. So, this data structure may require more than the given limit.
///
/// Each time a new entry is added with [LruCache::insert], it is checked
/// whether it fits in the given memory bound. If it does not, the
/// least-recently-used element is dropped from the cache, until the new entry
/// fits.
///
/// Note that both the key type `K` and the value type `V` must implement the
/// [MemSize] trait to allow for size estimation in normal usage. In addition,
/// the key type `K` is required to implement [Hash] and [Eq] for most
/// meaningful operations.
///
/// Furthermore, the hasher type `S` must implement the [BuildHasher] trait for
/// non-trivial functionality.
///
/// Mutable access is not allowed directly, since it may change the size of an
/// entry. It must be done either by removing the element using
/// [LruCache::remove] and inserting it again, or passing a mutating closure to
/// [LruCache::mutate].
pub struct LruCache<K, V, S = DefaultHashBuilder> {
table: RawTable<Entry<K, V>>,
// The seal is a dummy entry that is simultaneously in front of the head
// and behind the tail of the list. You can imagine it as connecting the
// list to a cycle.
// Having a dummy entry makes some operations (in particular unhinging)
// simpler. The cache would also work with two dummy entries, but only the
// next-field of the head and prev-field of the tail would be used. So, we
// use one dummy entry to act as both. It has to be ensured that we never
// iterate over this seal, so the edge case in which the cache is empty has
// to be considered in every situation where we iterate over the elements.
// This system is inspired by the lru-crate: https://crates.io/crates/lru
seal: EntryPtr<K, V>,
current_size: usize,
max_size: usize,
hash_builder: S
}
impl<K, V> LruCache<K, V> {
/// Creates a new, empty LRU cache with the given maximum memory size.
///
/// # Arguments
///
/// * `max_size`: The maximum number of bytes that the sum of the memory
/// estimates of all entries may occupy. It is important to note that this
/// bound may be exceeded in total memory requirement of the created data
/// structure.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// // Create an LRU cache with 16 KiB memory limit
/// let cache: LruCache<String, String> = LruCache::new(16 * 1024);
/// ```
pub fn new(max_size: usize) -> LruCache<K, V> {
LruCache::with_table_and_hasher(max_size, RawTable::new(),
DefaultHashBuilder::default())
}
/// Creates a new, empty LRU cache with the given maximum memory size and
/// the specified initial capacity.
///
/// # Arguments
///
/// * `max_size`: The maximum number of bytes that the sum of the memory
/// estimates of all entries may occupy. It is important to note that this
/// bound may be exceeded in total memory requirement of the created data
/// structure.
/// * `capacity`: A lower bound on the number of elements that the cache
/// will be able to hold without reallocating.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// // Create an LRU with 4 KiB memory limit that can hold at least 8
/// // elements without reallocating.
/// let cache: LruCache<String, String> = LruCache::with_capacity(4096, 8);
/// ```
pub fn with_capacity(max_size: usize, capacity: usize) -> LruCache<K, V> {
LruCache::with_table_and_hasher(max_size,
RawTable::with_capacity(capacity), DefaultHashBuilder::default())
}
}
impl<K, V, S> LruCache<K, V, S> {
fn with_table_and_hasher(max_size: usize, table: RawTable<Entry<K, V>>,
hash_builder: S) -> LruCache<K, V, S> {
let seal = EntryPtr::new_seal();
LruCache {
table,
seal,
current_size: 0,
max_size,
hash_builder
}
}
/// Creates a new, empty LRU cache with the given maximum memory size which
/// will use the given hash builder to hash keys.
///
/// # Arguments
///
/// * `max_size`: The maximum number of bytes that the sum of the memory
/// estimates of all entries may occupy. It is important to note that this
/// bound may be exceeded in total memory requirement of the created data
/// structure.
/// * `hash_builder`: The hasher used to hash keys. It should implement the
/// [BuildHasher] trait to allow operations being applied to the cache.
///
/// # Example
///
/// ```
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use lru_mem::LruCache;
///
/// // Create an LRU with 4 KiB memory limit that uses s for hashing keys.
/// let s = DefaultHashBuilder::default();
/// let cache: LruCache<String, String> = LruCache::with_hasher(4096, s);
/// ```
pub fn with_hasher(max_size: usize, hash_builder: S) -> LruCache<K, V, S> {
LruCache::with_table_and_hasher(max_size, RawTable::new(),
hash_builder)
}
/// Creates a new, empty LRU cache with the given maximum memory size and
/// the specified initial capacity which will use the given hash builder to
/// hash keys.
///
/// # Arguments
///
/// * `max_size`: The maximum number of bytes that the sum of the memory
/// estimates of all entries may occupy. It is important to note that this
/// bound may be exceeded in total memory requirement of the created data
/// structure.
/// * `capacity`: A lower bound on the number of elements that the cache
/// will be able to hold without reallocating.
/// * `hash_builder`: The hasher used to hash keys. It should implement the
/// [BuildHasher] trait to allow operations being applied to the cache.
///
/// # Example
///
/// ```
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use lru_mem::LruCache;
///
/// // Create an LRU with 4 KiB memory limit that can hold at least 8
/// // elements without reallocating that uses s for hashing keys.
/// let s = DefaultHashBuilder::default();
/// let cache: LruCache<String, String> =
/// LruCache::with_capacity_and_hasher(4096, 8, s);
/// ```
pub fn with_capacity_and_hasher(max_size: usize, capacity: usize,
hash_builder: S) -> LruCache<K, V, S> {
LruCache::with_table_and_hasher(max_size,
RawTable::with_capacity(capacity), hash_builder)
}
/// Gets the maximum number of bytes that the sum of the memory estimates
/// of all entries may occupy. It is important to note that this bound may
/// be exceeded in total memory requirement of the created data structure.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let cache: LruCache<String, String> = LruCache::new(65536);
/// assert_eq!(65536, cache.max_size());
/// ```
pub fn max_size(&self) -> usize {
self.max_size
}
/// Gets the current estimated memory of all entries contained in this
/// cache, in bytes.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> = LruCache::new(1024);
/// assert_eq!(0, cache.current_size());
///
/// // The exact amount of occupied memory depends not only on the values,
/// // but also some auxiliary data of the internal structures.
/// cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
/// assert!(cache.current_size() > 0);
/// ```
pub fn current_size(&self) -> usize {
self.current_size
}
/// Gets the number of entries contained in this cache.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> = LruCache::new(1024);
/// assert_eq!(0, cache.len());
///
/// cache.insert("apple".to_owned(), "banana".to_owned()).unwrap();
/// assert_eq!(1, cache.len());
/// ```
pub fn len(&self) -> usize {
self.table.len()
}
/// Indicates whether this cache is empty, i.e. its length
/// ([LruCache::len]) is zero.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> = LruCache::new(1024);
/// assert!(cache.is_empty());
///
/// cache.insert("apple".to_owned(), "banana".to_owned()).unwrap();
/// assert!(!cache.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the number of elements the cache can hold without reallocating.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> =
/// LruCache::with_capacity(1024, 10);
/// assert!(cache.capacity() >= 10);
/// ```
pub fn capacity(&self) -> usize {
self.table.capacity()
}
/// Returns a reference to the cache's [BuildHasher].
///
/// # Example
///
/// ```
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use lru_mem::LruCache;
///
/// let hasher = DefaultHashBuilder::default();
/// let cache: LruCache<String, String> =
/// LruCache::with_hasher(4096, hasher);
/// let hasher: &DefaultHashBuilder = cache.hasher();
/// ```
pub fn hasher(&self) -> &S {
&self.hash_builder
}
/// Removes all elements from this cache.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.clear();
///
/// assert_eq!(None, cache.get("lemon"));
/// assert_eq!(0, cache.len());
/// assert_eq!(0, cache.current_size());
/// ```
pub fn clear(&mut self) {
for entry in self.table.drain() {
unsafe { entry.drop(); }
}
self.current_size = 0;
self.seal.get_mut().next = self.seal;
self.seal.get_mut().prev = self.seal;
}
/// Creates an iterator over the entries (keys and values) contained in
/// this cache, ordered from least- to most-recently-used. The values are
/// not touched, i.e. the usage history is not altered in any way. That is,
/// the semantics are as in [LruCache::peek].
///
/// The memory requirement for any key or value may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut iter = cache.iter();
///
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// iter.next());
/// assert_eq!(Some((&"grapefruit".to_owned(), &"bitter".to_owned())),
/// iter.next_back());
/// assert_eq!(Some((&"lemon".to_owned(), &"sour".to_owned())),
/// iter.next());
/// assert_eq!(None, iter.next());
/// assert_eq!(None, iter.next_back());
/// ```
pub fn iter(&self) -> Iter<'_, K, V> {
Iter::new(self)
}
/// Creates an iterator over the keys contained in this cache, ordered from
/// least- to most-recently-used. The values are not touched, i.e. the
/// usage history is not altered in any way. That is, the semantics are as
/// in [LruCache::peek].
///
/// The memory requirement for any key may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut keys = cache.keys();
///
/// assert_eq!(Some(&"apple".to_owned()), keys.next());
/// assert_eq!(Some(&"grapefruit".to_owned()), keys.next_back());
/// assert_eq!(Some(&"lemon".to_owned()), keys.next());
/// assert_eq!(None, keys.next());
/// assert_eq!(None, keys.next_back());
/// ```
pub fn keys(&self) -> Keys<'_, K, V> {
Keys::new(self)
}
/// Creates an iterator over the values contained in this cache, ordered
/// from least- to most-recently-used. The values are not touched, i.e. the
/// usage history is not altered in any way. That is, the semantics are as
/// in [LruCache::peek].
///
/// The memory requirement for any value may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut values = cache.values();
///
/// assert_eq!(Some(&"sweet".to_owned()), values.next());
/// assert_eq!(Some(&"bitter".to_owned()), values.next_back());
/// assert_eq!(Some(&"sour".to_owned()), values.next());
/// assert_eq!(None, values.next());
/// assert_eq!(None, values.next_back());
/// ```
pub fn values(&self) -> Values<'_, K, V> {
Values::new(self)
}
/// Creates an iterator that drains entries from this cache. Both key and
/// value of each entry are returned. The cache is cleared afterward.
///
/// Note it is important for the drain to be dropped in order to ensure
/// integrity of the data structure. Preventing it from being dropped, e.g.
/// using [mem::forget](mem::forget), can result in unexpected behavior of
/// the cache.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut vec = cache.drain().collect::<Vec<_>>();
///
/// assert_eq!(&("apple".to_owned(), "sweet".to_owned()), &vec[0]);
/// assert_eq!(&("lemon".to_owned(), "sour".to_owned()), &vec[1]);
/// assert_eq!(&("grapefruit".to_owned(), "bitter".to_owned()), &vec[2]);
/// assert!(cache.is_empty());
/// ```
pub fn drain(&mut self) -> Drain<'_, K, V, S> {
Drain::new(self)
}
/// Creates an iterator that takes ownership of the cache and iterates over
/// its keys, ordered from least- to most-recently-used.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut keys = cache.into_keys().collect::<Vec<_>>();
///
/// assert_eq!(&"apple".to_owned(), &keys[0]);
/// assert_eq!(&"lemon".to_owned(), &keys[1]);
/// assert_eq!(&"grapefruit".to_owned(), &keys[2]);
/// ```
pub fn into_keys(self) -> IntoKeys<K, V, S> {
IntoKeys::new(self)
}
/// Creates an iterator that takes ownership of the cache and iterates over
/// its values, ordered from least- to most-recently-used.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("grapefruit".to_owned(), "bitter".to_owned()).unwrap();
/// let mut values = cache.into_values().collect::<Vec<_>>();
///
/// assert_eq!(&"sweet".to_owned(), &values[0]);
/// assert_eq!(&"sour".to_owned(), &values[1]);
/// assert_eq!(&"bitter".to_owned(), &values[2]);
/// ```
pub fn into_values(self) -> IntoValues<K, V, S> {
IntoValues::new(self)
}
}
fn make_hash<K, S>(hash_builder: &S, val: &K) -> u64
where
K: Hash + ?Sized,
S: BuildHasher,
{
use core::hash::Hasher;
let mut state = hash_builder.build_hasher();
val.hash(&mut state);
state.finish()
}
fn make_insert_hash<K, S>(hash_builder: &S, val: &K) -> u64
where
K: Hash,
S: BuildHasher,
{
use core::hash::Hasher;
let mut state = hash_builder.build_hasher();
val.hash(&mut state);
state.finish()
}
fn make_hasher<K, V, S>(hash_builder: &S) -> impl Fn(&Entry<K, V>) -> u64 + '_
where
K: Hash,
S: BuildHasher
{
move |val| make_hash::<K, S>(hash_builder, unsafe { val.key() })
}
fn equivalent_key<Q, K, V>(k: &Q) -> impl Fn(&Entry<K, V>) -> bool + '_
where
K: Borrow<Q>,
Q: ?Sized + Eq,
{
move |x| k.eq(unsafe { x.key() }.borrow())
}
impl<K, V, S> LruCache<K, V, S>
where
K: Eq + Hash,
S: BuildHasher
{
fn remove_from_table<Q>(&mut self, key: &Q) -> Option<Entry<K, V>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
let hash = make_hash::<Q, S>(&self.hash_builder, key);
self.table.remove_entry(hash, equivalent_key(key))
}
fn get_from_table<Q>(&self, key: &Q) -> Option<&Entry<K, V>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
let hash = make_hash::<Q, S>(&self.hash_builder, key);
self.table.get(hash, equivalent_key(key))
}
fn get_mut_from_table<Q>(&mut self, key: &Q) -> Option<&mut Entry<K, V>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
let hash = make_hash::<Q, S>(&self.hash_builder, key);
self.table.get_mut(hash, equivalent_key(key))
}
#[inline]
fn insert_into_table_with_hash(&mut self, hash: u64, entry: Entry<K, V>)
-> Result<EntryPtr<K, V>, Entry<K, V>> {
match self.table.try_insert_no_grow(hash, entry) {
Ok(bucket) => Ok(EntryPtr::new(bucket.as_ptr())),
Err(entry) => Err(entry)
}
}
/// Assumes that there is no entry with the same key in the table. If
/// insertion works, returns a pointer to the entry inside the table.
/// Otherwise, returns the entry input into this function.
fn insert_into_table(&mut self, entry: Entry<K, V>)
-> Result<EntryPtr<K, V>, Entry<K, V>> {
let key = unsafe { entry.key() };
let hash = make_insert_hash::<K, S>(&self.hash_builder, key);
self.insert_into_table_with_hash(hash, entry)
}
fn set_head(&mut self, mut entry: EntryPtr<K, V>) {
entry.insert(self.seal, self.seal.get().next);
}
fn touch_ptr(&mut self, entry: EntryPtr<K, V>) {
unsafe { entry.unhinge(); }
self.set_head(entry);
}
/// Safety: Requires the key and value of the entry to be initialized.
unsafe fn remove_metadata(&mut self, entry: Entry<K, V>) -> (K, V) {
let entry = entry.unhinge();
self.current_size -= entry.size();
entry.into_key_value()
}
/// Safety: Requires the key of the entry pointed to by the pointer to be
/// initialized, and the key and value of the entry located at that key in
/// the hash table to be initialized.
#[inline]
unsafe fn remove_ptr(&mut self, entry: EntryPtr<K, V>) -> (K, V) {
let entry = self.remove_from_table(entry.get().key()).unwrap();
self.remove_metadata(entry)
}
fn eject_to_target(&mut self, target: usize) {
while self.current_size > target {
self.remove_lru();
}
}
fn insert_untracked(&mut self, entry: Entry<K, V>) {
let entry_ptr = unsafe {
self.insert_into_table(entry).unwrap_unchecked()
};
self.set_head(entry_ptr);
}
fn try_reallocate(&mut self, new_capacity: usize) -> Result<(), TryReserveError> {
let hasher = make_hasher(&self.hash_builder);
let mut old_table = RawTable::try_with_capacity(new_capacity)?;
mem::swap(&mut self.table, &mut old_table);
for entry in old_table.into_iter() {
let mut prev_entry = entry.prev;
let mut next_entry = entry.next;
let bucket = self.table.insert(hasher(&entry), entry, &hasher);
let entry_ptr = EntryPtr::new(bucket.as_ptr());
prev_entry.get_mut().next = entry_ptr;
next_entry.get_mut().prev = entry_ptr;
}
Ok(())
}
fn reallocate(&mut self, new_capacity: usize) {
self.try_reallocate(new_capacity).unwrap()
}
fn lru_ptr(&self) -> Option<EntryPtr<K, V>> {
let lru = self.seal.get().prev;
if lru == self.seal {
None
}
else {
Some(lru)
}
}
fn mru_ptr(&self) -> Option<EntryPtr<K, V>> {
let mru = self.seal.get().next;
if mru == self.seal {
None
}
else {
Some(mru)
}
}
/// Removes the least-recently-used value from this cache. This returns
/// both key and value of the removed value. If this cache is empty, `None`
/// is returned.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some(("apple".to_owned(), "sweet".to_owned())),
/// cache.remove_lru());
/// assert_eq!(1, cache.len());
/// ```
pub fn remove_lru(&mut self) -> Option<(K, V)> {
self.lru_ptr().map(|ptr| unsafe { self.remove_ptr(ptr) })
}
/// Gets a reference to the least-recently-used entry from this cache. This
/// returns both key and value of the entry. If the cache is empty, `None`
/// is returned.
///
/// This method also marks the value as most-recently-used. If you want the
/// usage history to not be updated, use [LruCache::peek_lru] instead.
///
/// The memory requirement of the key and value may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// cache.get_lru());
/// assert_eq!(Some((&"lemon".to_owned(), &"sour".to_owned())),
/// cache.get_lru());
/// ```
pub fn get_lru(&mut self) -> Option<(&K, &V)> {
self.lru_ptr().map(|entry| {
unsafe {
self.touch_ptr(entry);
let entry = entry.get_extended();
(entry.key(), entry.value())
}
})
}
/// Gets a reference to the least-recently-used entry from this cache. This
/// returns both key and value of the entry. If the cache is empty, `None`
/// is returned.
///
/// This method does not mark the value as most-recently-used. If you want
/// the usage history to be updated, use [LruCache::get_lru] instead.
///
/// The memory requirement of the key and value may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// cache.peek_lru());
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// cache.peek_lru());
/// ```
pub fn peek_lru(&self) -> Option<(&K, &V)> {
self.lru_ptr().map(|ptr|
unsafe {
let entry = ptr.get_extended();
(entry.key(), entry.value())
})
}
/// Removes the most-recently-used value from the cache. This returns both
/// key and value of the removed entry. If the cache is empty, `None` is
/// returned.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some(("lemon".to_owned(), "sour".to_owned())),
/// cache.remove_mru());
/// assert_eq!(1, cache.len());
/// ```
pub fn remove_mru(&mut self) -> Option<(K, V)> {
self.mru_ptr().map(|ptr| unsafe { self.remove_ptr(ptr) })
}
/// Gets a reference to the most-recently-used entry from this cache. This
/// returns both key and value of the entry. If the cache is empty, `None`
/// is returned.
///
/// Note that, for the most-recently-used entry, it does not matter whether
/// the usage history is updated, since it was most-recently-used before.
/// So, there is no need for a `get_mru` method.
///
/// The memory requirement of the key and value may not be changed.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some((&"lemon".to_owned(), &"sour".to_owned())),
/// cache.peek_mru());
/// ```
pub fn peek_mru(&self) -> Option<(&K, &V)> {
self.mru_ptr().map(|ptr|
unsafe {
let entry = ptr.get_extended();
(entry.key(), entry.value())
})
}
fn new_capacity(&self, additional: usize)
-> Result<usize, TryReserveError> {
self.len().checked_add(additional)
.ok_or(TryReserveError::CapacityOverflow)
}
/// Reserves capacity for at least `additional` new entries to be inserted
/// into the cache. The collection may reserve more space to avoid frequent
/// reallocations.
///
/// # Arguments
///
/// * `additional`: The number of new entries beyond the ones already
/// contained in the cache for which space should be reserved.
///
/// # Panics
///
/// If the new allocation size overflows [usize].
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> = LruCache::new(1024);
/// cache.insert("key".to_owned(), "value".to_owned()).unwrap();
/// cache.reserve(10);
/// assert!(cache.capacity() >= 11);
/// ```
pub fn reserve(&mut self, additional: usize) {
let new_capacity = self.new_capacity(additional).unwrap();
if self.capacity() < new_capacity {
self.reallocate(new_capacity);
}
}
/// Tries to reserve capacity for at least `additional` new entries to be
/// inserted into the cache. The collection may reserve more space to avoid
/// frequent reallocations.
///
/// # Arguments
///
/// * `additional`: The number of new entries beyond the ones already
/// contained in the cache for which space should be reserved.
///
/// # Errors
///
/// If the capacity overflows, or the allocator reports an error, then an
/// error is returned.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> = LruCache::new(1024);
/// cache.insert("key".to_owned(), "value".to_owned()).unwrap();
/// cache.try_reserve(10).expect("out of memory");
/// assert!(cache.capacity() >= 11);
/// ```
pub fn try_reserve(&mut self, additional: usize)
-> Result<(), TryReserveError> {
let new_capacity = self.new_capacity(additional)?;
if self.capacity() < new_capacity {
self.try_reallocate(new_capacity)
}
else {
Ok(())
}
}
/// Shrinks the capacity of the cache with a lower bound. The capacity will
/// remain at least as large as both the [length](LruCache::len) and the
/// given lower bound while maintaining internal constraints of the hash
/// table.
///
/// If the capacity is less than the given lower bound, this method is
/// no-op.
///
/// # Arguments
///
/// * `min_capacity`: A lower bound on the capacity after the operation.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> =
/// LruCache::with_capacity(1024, 10);
/// assert!(cache.capacity() >= 10);
/// cache.shrink_to(5);
/// assert!(cache.capacity() >= 5);
/// ```
pub fn shrink_to(&mut self, min_capacity: usize) {
let new_capacity = self.len().max(min_capacity);
if self.capacity() > new_capacity {
self.reallocate(new_capacity);
}
}
/// Shrinks the capacity of the cache as much as possible. It will drop
/// down as much as possible while maintaining internal constraints of the
/// hash table.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache: LruCache<String, String> =
/// LruCache::with_capacity(1024, 10);
/// cache.insert("key".to_owned(), "value".to_owned()).unwrap();
/// cache.shrink_to_fit();
/// assert!(cache.capacity() >= 1);
/// ```
pub fn shrink_to_fit(&mut self) {
self.shrink_to(0)
}
/// Sets a new memory limit for this cache. If this is below the current
/// size (see [LruCache::current_size]), the least-recently-used element
/// will be repeatedly ejected until the limit is satisfied.
///
/// Note that reducing the memory limit to a small fraction of the previous
/// maximum may lead to large amounts of unused capacity in the underlying
/// data structure. If this is a problem, use [LruCache::shrink_to] or
/// [LruCache::shrink_to_fit] to avoid this.
///
/// # Arguments
///
/// * `max_size`: The new maximum number of bytes that the sum of the
/// memory estimates of all entries may occupy.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.set_max_size(cache.current_size() - 1);
///
/// assert_eq!(1, cache.len());
/// assert!(cache.max_size() < 1024);
/// ```
pub fn set_max_size(&mut self, max_size: usize) {
self.eject_to_target(max_size);
self.max_size = max_size;
}
/// Sets the entry with the given key as most-recently-used, i.e. all other
/// entries currently contained in the cached will be dropped before this
/// one (unless others are touched/used afterwards). If there is no value
/// associated with the given key, this method is no-op.
///
/// # Arguments
///
/// * `key`: The key of the entry to touch.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.touch(&"apple".to_owned());
///
/// assert_eq!(Some(("lemon".to_owned(), "sour".to_owned())),
/// cache.remove_lru());
/// ```
pub fn touch<Q>(&mut self, key: &Q)
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
if let Some(entry) = self.get_mut_from_table(key) {
let entry_ptr = EntryPtr::new(entry as *mut Entry<K, V>);
self.touch_ptr(entry_ptr);
}
}
/// Gets references to the key and value of the entry associated with the
/// given key. If there is no entry for that key, `None` is returned.
///
/// This method also marks the entry as most-recently-used (see
/// [LruCache::touch]). If you do not want the usage history to be updated,
/// use [LruCache::peek_entry] instead.
///
/// The memory requirement of the key and value may not be changed.
///
/// # Arguments
///
/// * `key`: The key of the entry to get.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// cache.get_entry("apple"));
/// assert_eq!(Some(("lemon".to_owned(), "sour".to_owned())),
/// cache.remove_lru());
/// ```
pub fn get_entry<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
if let Some(entry) = self.get_mut_from_table(key) {
let entry_ptr = EntryPtr::new(entry as *mut Entry<K, V>);
self.touch_ptr(entry_ptr);
let entry = unsafe { entry_ptr.get_extended() };
Some(unsafe { (entry.key(), entry.value()) })
}
else {
None
}
}
/// Gets a reference to the value associated with the given key. If there
/// is no value for that key, `None` is returned.
///
/// This method also marks the value as most-recently-used (see
/// [LruCache::touch]). If you do not want the usage history to be updated,
/// use [LruCache::peek] instead.
///
/// The memory requirement of the value may not be changed.
///
/// # Arguments
///
/// * `key`: The key of the value to get.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some(&"sweet".to_owned()), cache.get("apple"));
/// assert_eq!(Some(("lemon".to_owned(), "sour".to_owned())),
/// cache.remove_lru());
/// ```
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
self.get_entry(key).map(|(_, v)| v)
}
/// Gets references to the key and value of the entry associated with the
/// given key. If there is no entry for that key, `None` is returned.
///
/// This method does not mark the value as most-recently-used. If you want
/// the usage history to be updated, use [LruCache::get_entry] instead.
///
/// The memory requirement of the key and value may not be changed.
///
/// # Arguments
///
/// * `key`: The key of the entry to peek.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some((&"apple".to_owned(), &"sweet".to_owned())),
/// cache.peek_entry("apple"));
/// assert_eq!(Some(("apple".to_owned(), "sweet".to_owned())),
/// cache.remove_lru());
/// ```
pub fn peek_entry<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
self.get_from_table(key).map(|e| unsafe { (e.key(), e.value()) })
}
/// Gets a reference to the value associated with the given key. If there
/// is no value for that key, `None` is returned.
///
/// This method does not mark the value as most-recently-used. If you want
/// the usage history to be updated, use [LruCache::get] instead.
///
/// The memory requirement of the value may not be changed.
///
/// # Arguments
///
/// * `key`: The key of the value to peek.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some(&"sweet".to_owned()), cache.peek("apple"));
/// assert_eq!(Some(("apple".to_owned(), "sweet".to_owned())),
/// cache.remove_lru());
/// ```
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
self.get_from_table(key).map(|e| unsafe { e.value() })
}
/// Indicates whether this cache contains an entry associated with the
/// given key. If there is one, it is _not_ marked as most-recently-used.
///
/// # Arguments
///
/// * `key`: The key of the value to search.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
///
/// assert!(cache.contains("apple"));
/// assert!(!cache.contains("banana"));
/// ```
pub fn contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
let hash = make_hash::<Q, S>(&self.hash_builder, key);
self.table.find(hash, equivalent_key(key)).is_some()
}
/// Removes the entry associated with the given key from this cache. If the
/// cache does not contain the given key, `None` is returned.
///
/// # Arguments
///
/// * `key`: The key of the value to remove.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.remove("apple");
///
/// assert_eq!(0, cache.len());
/// ```
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
self.remove_from_table(key)
.map(|entry| unsafe { self.remove_metadata(entry) })
}
/// Removes and returns the value associated with the given key from this
/// cache. If there is no such value, `None` is returned.
///
/// # Argument
///
/// * `key`: The key of the value to remove.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Some("sour".to_owned()), cache.remove("lemon"));
/// assert_eq!(1, cache.len());
/// ```
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized
{
self.remove_entry(key).map(|(_, v)| v)
}
/// Retains only the elements which satisfy the predicate. In other words,
/// removes all entries `(k, v)` such that `pred(&k, &v)` returns `false`.
/// The elements are visited ordered from least-recently-used to
/// most-recently-used.
///
/// For all retained entries, i.e. those where `pred` returns `true`, the
/// memory requirement of the key and value may not be changed.
///
/// # Arguments
///
/// * `pred`: A function which takes as input references to the key and
/// value of an entry and decides whether it should remain in the map
/// (`true`) or not (`false`).
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
/// cache.insert("banana".to_owned(), "sweet".to_owned()).unwrap();
/// cache.retain(|_, v| v.as_str() == "sweet");
///
/// assert_eq!(2, cache.len());
/// assert!(cache.get("apple").is_some());
/// assert!(cache.get("lemon").is_none());
/// assert!(cache.get("banana").is_some());
/// ```
pub fn retain<F>(&mut self, mut pred: F)
where
F: FnMut(&K, &V) -> bool
{
let mut tail = self.seal.get().prev;
while tail != self.seal {
unsafe {
let entry = tail.get();
let key = entry.key();
if !pred(key, entry.value()) {
self.remove_entry(key);
}
tail = entry.prev;
}
}
}
}
struct EntryTooLarge<K, V> {
key: K,
value: V,
entry_size: usize,
max_size: usize
}
impl<K, V> From<EntryTooLarge<K, V>> for InsertError<K, V> {
fn from(data: EntryTooLarge<K, V>) -> InsertError<K, V> {
InsertError::EntryTooLarge {
key: data.key,
value: data.value,
entry_size: data.entry_size,
max_size: data.max_size
}
}
}
impl<K, V> From<EntryTooLarge<K, V>> for TryInsertError<K, V> {
fn from(data: EntryTooLarge<K, V>) -> TryInsertError<K, V> {
TryInsertError::EntryTooLarge {
key: data.key,
value: data.value,
entry_size: data.entry_size,
max_size: data.max_size
}
}
}
impl<K, V, S> LruCache<K, V, S>
where
K: Eq + Hash + MemSize,
V: MemSize,
S: BuildHasher
{
fn prepare_insert(&mut self, key: K, value: V)
-> Result<UnhingedEntry<K, V>, EntryTooLarge<K, V>> {
let entry = UnhingedEntry::new(key, value);
let entry_size = entry.size();
if entry_size > self.max_size {
let (key, value) = entry.into_key_value();
Err(EntryTooLarge {
key,
value,
entry_size,
max_size: self.max_size
})
}
else {
Ok(entry)
}
}
fn insert_unchecked(&mut self, entry: UnhingedEntry<K, V>, hash: u64) {
let size = entry.size();
let mut entry = Entry::new(entry, self.seal, self.seal.get().next);
loop {
match self.insert_into_table_with_hash(hash, entry) {
Ok(entry_ptr) => {
self.current_size += size;
self.set_head(entry_ptr);
return;
},
Err(returned_entry) => {
entry = returned_entry;
self.reallocate((self.table.capacity() * 2).max(1));
// The seal pointer stays constant through reallocation, so
// only entry.next has to be set.
entry.next = self.seal.get().next;
}
}
}
}
/// Inserts a new entry into this cache. This is initially the
/// most-recently-used entry. If there was an entry with the given key
/// before, it is removed and its value returned. Otherwise, `None` is
/// returned. If inserting this entry would violate the memory limit,
/// the least-recently-used values are ejected from the cache until it
/// fits.
///
/// If you want to know before calling this method whether elements would
/// be ejected, you can use [entry_size] to obtain the memory usage that
/// would be assigned to the created entry and check using
/// [LruCache::current_size] and [LruCache::max_size] whether it fits.
///
/// # Arguments
///
/// * `key`: The key by which the inserted entry will be identified.
/// * `value`: The value to store in the inserted entry.
///
/// # Errors
///
/// Raises an [InsertError::EntryTooLarge] if the entry alone would already
/// be too large to fit inside the cache's size limit. That is, even if all
/// other entries were ejected, it still would not be able to be inserted.
/// If this occurs, the entry was not inserted.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(2, cache.len());
/// ```
pub fn insert(&mut self, key: K, value: V)
-> Result<Option<V>, InsertError<K, V>> {
let entry = self.prepare_insert(key, value)?;
// Deduplicate keys, make space
let key = entry.key();
let hash = make_insert_hash::<K, S>(&self.hash_builder, key);
let result = self.table.remove_entry(hash, equivalent_key(key))
.map(|e| unsafe { self.remove_metadata(e).1 });
self.eject_to_target(self.max_size - entry.size());
// Insert entry at head of list
self.insert_unchecked(entry, hash);
Ok(result)
}
/// Tries to insert a new entry into this cache. This is initially the
/// most-recently-used entry. If there was an entry with the given key
/// before or it does not fit within the memory requirement, an appropriate
/// error is raised (see below).
///
/// # Arguments
///
/// * `key`: The key by which the inserted entry will be identified.
/// * `value`: The value to store in the inserted entry.
///
/// # Errors
///
/// * Raises an [TryInsertError::EntryTooLarge] if the entry alone would
/// already be too large to fit inside the cache's size limit.
/// * Otherwise, raises a [TryInsertError::WouldEjectLru] if the entry does
/// not fit within the remaining free memory of the cache, i.e. the
/// difference between [LruCache::max_size] and [LruCache::current_size].
/// * Otherwise, raises an [TryInsertError::OccupiedEntry] if there was
/// already an entry with the given key.
///
/// If any error was raised, the entry was not inserted into the cache.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// let result = cache.try_insert("apple".to_owned(), "sweet".to_owned());
/// assert!(result.is_ok());
/// let result = cache.try_insert("apple".to_owned(), "sour".to_owned());
/// assert!(!result.is_ok());
/// ```
pub fn try_insert(&mut self, key: K, value: V)
-> Result<(), TryInsertError<K, V>> {
let entry = self.prepare_insert(key, value)?;
// Check that the entry fits
let free_memory = self.max_size - self.current_size;
let entry_size = entry.size();
if entry_size > free_memory {
let (key, value) = entry.into_key_value();
return Err(TryInsertError::WouldEjectLru {
key,
value,
entry_size,
free_memory
})
}
// Check that the entry is not occupied
let key = entry.key();
let hash = make_insert_hash::<K, S>(&self.hash_builder, key);
if self.table.find(hash, equivalent_key(key)).is_some() {
let (key, value) = entry.into_key_value();
return Err(TryInsertError::OccupiedEntry {
key,
value
})
}
self.insert_unchecked(entry, hash);
Ok(())
}
/// Applies a mutating function to the value associated with the given key.
/// The result of that function is returned. If there is no value for the
/// given key, `None` is returned, and the operation is never called.
/// Otherwise, the entry is marked as most-recently-used by this method.
///
/// Note that the operation may also change the size of the value. After it
/// terminates, the internal sizes are updated and, if necessary,
/// least-recently-used entries are ejected to restore the memory
/// requirement. If the operation increases the size beyond the limit of
/// this cache, an error is raised (see below).
///
/// # Arguments
///
/// * `key`: The key of the value to mutate.
/// * `op`: An operation that takes as input a mutable reference to the
/// value, mutates it, and returns the desired result. This is forwarded by
/// this method to the caller.
///
/// # Errors
///
/// Raises an [MutateError::EntryTooLarge] if the operation expanded the
/// value so much that the entry no longer fit inside the memory limit of
/// the cache. If that is the case, the entry is removed and its parts
/// returned in the error data.
///
/// # Example
///
/// ```
/// use lru_mem::LruCache;
///
/// let mut cache = LruCache::new(1024);
/// cache.insert("apple".to_owned(), "sweet".to_owned()).unwrap();
/// cache.insert("lemon".to_owned(), "sour".to_owned()).unwrap();
///
/// assert_eq!(Ok(Some(())),
/// cache.mutate("apple", |s| s.push_str(" and sour")));
/// assert_eq!(Some(&"sweet and sour".to_owned()), cache.peek("apple"));
/// ```
pub fn mutate<Q, R, F>(&mut self, key: &Q, op: F)
-> Result<Option<R>, MutateError<K, V>>
where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
F: FnOnce(&mut V) -> R
{
let max_size = self.max_size;
if let Some(entry) = self.get_mut_from_table(key) {
let new_value_size;
let result;
let old_value_size;
unsafe {
old_value_size = entry.value().mem_size();
result = op(entry.value_mut());
new_value_size = entry.value().mem_size();
}
if new_value_size > old_value_size {
// The operation was expanding; we must ensure it still fits.
let diff = new_value_size - old_value_size;
let new_entry_size = entry.size + diff;
if new_entry_size > max_size {
// The entry is too large after the operation; eject it and
// raise according error.
let old_entry_size = entry.size;
let (key, value) = self.remove_entry(key).unwrap();
return Err(MutateError::EntryTooLarge {
key,
value,
old_entry_size,
new_entry_size,
max_size
});
}
entry.size = new_entry_size;
let entry_ptr = EntryPtr::new(entry as *mut Entry<K, V>);
self.current_size += diff;
self.touch_ptr(entry_ptr);
self.eject_to_target(max_size);
}
else {
// The operation was non-expanding; everything is ok.
let diff = old_value_size - new_value_size;
entry.size -= diff;
let entry_ptr = EntryPtr::new(entry as *mut Entry<K, V>);
self.current_size -= diff;
self.touch_ptr(entry_ptr);
}
Ok(Some(result))
}
else {
Ok(None)
}
}
}
impl<K, V, S> IntoIterator for LruCache<K, V, S> {
type Item = (K, V);
type IntoIter = IntoIter<K, V, S>;
fn into_iter(self) -> IntoIter<K, V, S> {
IntoIter::new(self)
}
}
impl<K, V, S> Clone for LruCache<K, V, S>
where
K: Clone + Eq + Hash,
V: Clone,
S: BuildHasher + Clone
{
fn clone(&self) -> LruCache<K, V, S> {
let max_size = self.max_size;
let capacity = self.capacity();
let hash_builder = self.hash_builder.clone();
let mut clone = LruCache::with_capacity_and_hasher(
max_size, capacity, hash_builder);
clone.current_size = self.current_size;
let mut next = self.seal.get().prev;
while next != self.seal {
let entry = unsafe { next.get().clone() };
next = entry.prev;
clone.insert_untracked(entry);
}
clone
}
}
impl<K, V, S> Drop for LruCache<K, V, S> {
fn drop(&mut self) {
for entry in self.table.drain() {
unsafe { entry.drop() };
}
unsafe {
self.seal.drop_seal();
}
}
}
impl<K: Debug, V: Debug, S> Debug for LruCache<K, V, S> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
// Since the LruCache contains raw pointers, it is not automatically marked as
// Send and Sync. We will provide manual implementations as well as arguments
// why that is ok.
// It is implicitly assumed that every LruCache only contains pointers to
// memory that belongs to the cache itself. So, if an LruCache is sent to
// another thread, that memory can now only be accessed by that thread. In
// other words, two LruCaches or anything related (e.g. iterators) can never
// access the same memory. Therefore, sending them is no issue.
unsafe impl<K: Send, V: Send, S: Send> Send for LruCache<K, V, S> { }
// If an immutable reference to an LruCache exists, there is simultaneously no
// mutable reference to the same cache. By design of the cache, any operations
// applied to that reference will allow no writing access to any of its memory.
// Those that yield any possibility of writing, such as cloning, are restricted
// to newly allocated memory. Therefore, sending references is no issue, and by
// definition of Sync, LruCache may implement it.
unsafe impl<K: Sync, V: Sync, S: Sync> Sync for LruCache<K, V, S> { }
#[cfg(test)]
mod tests {
use std::hash::Hasher;
use std::sync::{Arc, Mutex};
use super::*;
pub(crate) fn singleton_test_cache() -> LruCache<&'static str, &'static str> {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache
}
pub(crate) fn large_test_cache() -> LruCache<&'static str, &'static str> {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
cache.insert("ahoy", "mars").unwrap();
cache.insert("hi", "venus").unwrap();
cache.insert("good morning", "jupiter").unwrap();
cache
}
#[test]
fn cache_correctly_inserts_with_sufficient_capacity() {
let mut cache = LruCache::new(1024);
assert_eq!(0, cache.len());
assert_eq!(0, cache.current_size());
assert_eq!(1024, cache.max_size());
assert_eq!(Ok(None),
cache.insert("key1".to_owned(), "value1".to_owned()));
let new_size = cache.current_size();
assert_eq!(1, cache.len());
assert!(new_size > 0);
assert_eq!(Some(&"value1".to_owned()), cache.get("key1"));
assert_eq!(Ok(None),
cache.insert("key2".to_owned(), "value2".to_owned()));
assert_eq!(2, cache.len());
assert!(cache.current_size() > new_size);
assert_eq!(Some(&"value1".to_owned()), cache.get("key1"));
assert_eq!(Some(&"value2".to_owned()), cache.get("key2"));
}
#[test]
fn cache_deduplicates_keys() {
let mut cache = LruCache::new(1024);
assert_eq!(Ok(None),
cache.insert("key".to_owned(), "value".to_owned()));
let expected_size = cache.current_size;
assert_eq!(Ok(Some("value".to_owned())),
cache.insert("key".to_owned(), "value".to_owned()));
assert_eq!(expected_size, cache.current_size());
assert_eq!(1, cache.len());
}
fn string_with_size(size: usize) -> String {
let mut s = String::with_capacity(size);
for _ in 0..size {
s.push('0');
}
s
}
#[test]
fn cache_ejects_lru_if_overflowing() {
let mut cache = LruCache::new(2048);
// On 256-bit arch, each entry requires an extra 289 bytes per entry
// in addition to the size of the value in bytes (see test case
// "entry_correctly_computes_size", value_str_bytes = 1).
// On 16-bit arch, this would be 19 bytes.
// The numbers in this test case accommodate anywhere from 15 to 696
// extra bytes (676 * 3 + 15 = 2050, 676 * 2 + 696 = 2048).
cache.insert("a".to_owned(), string_with_size(676)).unwrap();
cache.insert("b".to_owned(), string_with_size(676)).unwrap();
let expected_size = cache.current_size();
cache.insert("c".to_owned(), string_with_size(676)).unwrap();
assert_eq!(expected_size, cache.current_size());
assert_eq!(2, cache.len());
assert!(cache.peek("a").is_none());
assert!(cache.peek("b").is_some());
assert!(cache.peek("c").is_some());
assert_eq!(Some("b"), cache.peek_lru().map(|(key, _)| key.as_str()));
assert_eq!(Some("c"), cache.peek_mru().map(|(key, _)| key.as_str()));
}
#[test]
fn getting_sets_most_recently_used() {
let mut cache = LruCache::new(2048);
// See the argument why the sizes are like this in test case
// "cache_ejects_lru_if_overflowing".
cache.insert("a".to_owned(), string_with_size(674)).unwrap();
cache.insert("b".to_owned(), string_with_size(674)).unwrap();
cache.get(&"a".to_owned());
cache.insert("c".to_owned(), string_with_size(674)).unwrap();
assert!(cache.get("a").is_some());
assert!(cache.get("b").is_none());
assert!(cache.get("c").is_some());
assert_eq!("a", unsafe { cache.seal.get().prev.get().key() });
assert_eq!("c", unsafe { cache.seal.get().next.get().key() });
cache.get("a");
assert_eq!("c", unsafe { cache.seal.get().prev.get().key() });
assert_eq!("a", unsafe { cache.seal.get().next.get().key() });
}
#[test]
fn empty_cache_has_no_lru_and_mru() {
let cache = LruCache::<&str, &str>::new(1024);
assert!(cache.peek_lru().is_none());
assert!(cache.peek_mru().is_none());
}
#[test]
fn get_lru_sets_most_recently_used() {
let mut cache = LruCache::new(2048);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
let lru = cache.get_lru();
assert_eq!(Some((&"hello", &"world")), lru);
cache.set_max_size(cache.current_size() - 1);
assert!(cache.contains("hello"));
assert!(!cache.contains("greetings"));
}
#[test]
fn peeking_does_not_set_most_recently_used() {
let mut cache = LruCache::new(2048);
cache.insert("a".to_owned(), string_with_size(674)).unwrap();
cache.insert("b".to_owned(), string_with_size(674)).unwrap();
cache.peek(&"a".to_owned());
cache.insert("c".to_owned(), string_with_size(674)).unwrap();
assert!(cache.get("a").is_none());
assert!(cache.get("b").is_some());
assert!(cache.get("c").is_some());
cache.peek_entry(&"b".to_owned());
cache.insert("d".to_owned(), string_with_size(674)).unwrap();
assert!(cache.get("b").is_none());
assert!(cache.get("c").is_some());
assert!(cache.get("d").is_some());
}
#[test]
fn cache_rejects_too_large_entry() {
let mut cache = LruCache::new(256);
let key = "This is a pretty long key, especially considering that \
keys should normally be rather small to avoid long hashing times."
.to_owned();
let value = "Although the key alone has insufficient size, together \
with this string it pushes pushes the total memory requirement of the \
entry over the capacity of the cache.".to_owned();
assert!(matches!(cache.insert(key, value),
Err(InsertError::EntryTooLarge { .. })));
}
#[test]
fn precisely_fitting_entry_does_not_eject_lru() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
cache.insert("greetings".to_owned(), "moon".to_owned()).unwrap();
let key = "ahoy".to_owned();
let mut value = "mars".to_owned();
value.shrink_to_fit();
let required_size = cache.max_size() - cache.current_size();
let additional_bytes = required_size - entry_size(&key, &value);
let additional_data = vec![b's'; additional_bytes];
value.push_str(&String::from_utf8(additional_data).unwrap());
value.shrink_to_fit();
assert_eq!(required_size, entry_size(&key, &value));
cache.insert(key, value).unwrap();
assert_eq!(3, cache.len());
}
#[test]
fn auto_reallocation_doubles_capacity() {
let mut cache = LruCache::with_capacity(4096, 10);
let capacity = cache.capacity();
for index in 0..capacity {
cache.insert(index, 0).unwrap();
}
cache.insert(capacity, 0).unwrap();
assert_eq!(2 * capacity, cache.capacity());
}
#[test]
fn try_insert_works_as_insert_if_ok() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
cache.try_insert("greetings".to_owned(), "moon".to_owned()).unwrap();
assert_eq!(2, cache.len());
assert_eq!("world".to_owned(), cache.remove_lru().unwrap().1);
assert_eq!("moon".to_owned(), cache.remove_lru().unwrap().1);
}
#[test]
fn try_insert_fails_on_duplication() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
let result = cache.try_insert("hello".to_owned(), "moon".to_owned());
assert!(matches!(result, Err(TryInsertError::OccupiedEntry { .. })));
assert_eq!(1, cache.len());
assert_eq!(&"world".to_owned(), cache.get("hello").unwrap());
}
#[test]
fn try_insert_fails_if_eject_required() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
cache.insert("greetings".to_owned(), "moon".to_owned()).unwrap();
cache.set_max_size(cache.current_size());
let result = cache.try_insert("ahoy".to_owned(), "mars".to_owned());
assert!(matches!(result, Err(TryInsertError::WouldEjectLru { .. })));
assert_eq!(2, cache.len());
assert!(!cache.contains("ahoy"));
}
#[test]
fn try_insert_fails_if_too_large() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
let value = String::from_utf8(vec![b'0'; 1024]).unwrap();
let result = cache.try_insert("key".to_owned(), value);
assert!(matches!(result, Err(TryInsertError::EntryTooLarge { .. })));
assert_eq!(1, cache.len());
assert!(!cache.contains("key"));
}
#[test]
fn removing_works() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
cache.insert("ahoy", "mars").unwrap();
assert_eq!(Some(("hello", "world")), cache.remove_entry("hello"));
assert_eq!(None, cache.remove("hello"));
}
#[test]
fn removing_mru_works() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
cache.insert("ahoy", "mars").unwrap();
cache.remove_mru();
assert_eq!(2, cache.len());
assert!(cache.contains("hello"));
assert!(cache.contains("greetings"));
}
#[test]
fn clearing_works() {
let mut cache = large_test_cache();
cache.clear();
assert!(cache.is_empty());
assert!(cache.peek_lru().is_none());
assert!(cache.peek_mru().is_none());
assert!(cache.iter().next().is_none());
}
#[test]
fn retain_does_not_affect_empty_cache() {
let mut cache: LruCache<u64, Vec<u8>> = LruCache::new(1024);
cache.retain(|k, v| v.len() as u64 == *k);
assert_eq!(0, cache.len());
}
#[test]
fn retain_works_if_all_match() {
let mut cache: LruCache<u64, Vec<u8>> = LruCache::new(1024);
cache.insert(1, vec![0]).unwrap();
cache.insert(5, vec![2, 3, 5, 7, 11]).unwrap();
cache.insert(4, vec![1, 4, 9, 16]).unwrap();
cache.retain(|k, v| v.len() as u64 == *k);
assert_eq!(3, cache.len());
assert!(cache.contains(&1));
assert!(cache.contains(&4));
assert!(cache.contains(&5));
}
#[test]
fn retain_works_if_none_match() {
let mut cache: LruCache<u64, Vec<u8>> = LruCache::new(1024);
cache.insert(1, vec![0, 1]).unwrap();
cache.insert(5, vec![2, 3, 5, 7, 11, 13]).unwrap();
cache.insert(4, vec![1, 4, 9, 16, 25]).unwrap();
cache.retain(|k, v| v.len() as u64 == *k);
assert_eq!(0, cache.len());
assert!(!cache.contains(&1));
assert!(!cache.contains(&4));
assert!(!cache.contains(&5));
}
#[test]
fn retain_works_if_some_match() {
let mut cache: LruCache<u64, Vec<u8>> = LruCache::new(1024);
cache.insert(1, vec![0, 1]).unwrap();
cache.insert(5, vec![2, 3, 5, 7, 11]).unwrap();
cache.insert(4, vec![1, 4, 9, 16]).unwrap();
cache.retain(|k, v| v.len() as u64 == *k);
assert_eq!(2, cache.len());
assert!(!cache.contains(&1));
assert!(cache.contains(&4));
assert!(cache.contains(&5));
}
#[test]
fn retain_sets_lru_and_mru_if_necessary() {
let mut cache = LruCache::new(1024);
cache.insert(1, 0).unwrap();
cache.insert(2, 0).unwrap();
cache.insert(3, 0).unwrap();
cache.insert(4, 0).unwrap();
cache.retain(|&k, _| k != 1 && k != 4);
assert_eq!(Some((&2, &0)), cache.peek_lru());
assert_eq!(Some((&3, &0)), cache.peek_mru());
}
#[test]
fn contains_works() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
assert!(cache.contains("hello"));
assert!(!cache.contains("greetings"));
}
#[test]
fn increasing_max_size_keeps_all_elements() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
cache.set_max_size(2048);
assert_eq!(2, cache.len());
assert_eq!(2048, cache.max_size());
}
#[test]
fn decreasing_max_size_below_current_size_drops_elements() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
cache.set_max_size(cache.current_size() - 1);
assert_eq!(1, cache.len());
assert!(cache.current_size() < cache.max_size());
assert!(cache.max_size() < 1024);
}
#[test]
fn cache_correctly_applies_mutation() {
let mut cache = LruCache::new(1024);
cache.insert("hello".to_owned(), "world".to_owned()).unwrap();
cache.insert("greetings".to_owned(), "moon".to_owned()).unwrap();
let old_size = cache.current_size();
let result = cache.mutate("greetings", |s| {
s.push_str(", from 384400 km away");
s.shrink_to_fit();
s.len()
});
assert_eq!(Ok(Some(25)), result);
assert_eq!(2, cache.len());
assert_eq!(old_size + 21, cache.current_size());
assert_eq!(Some(&"moon, from 384400 km away".to_owned()),
cache.get("greetings"));
}
#[test]
fn cache_rejects_too_expanding_mutation() {
let mut cache = LruCache::new(1024);
cache.insert("hello", vec![0u8; 32]).unwrap();
cache.insert("greetings", vec![0u8; 32]).unwrap();
let old_size = cache.current_size();
let result = cache.mutate("hello", |v| {
v.append(&mut vec![0u8; 1000]);
});
assert!(matches!(result, Err(MutateError::EntryTooLarge { .. })));
assert_eq!(1, cache.len());
assert!(cache.current_size() < old_size);
assert_eq!(None, cache.get("hello"));
}
#[test]
fn non_expanding_mutation_works() {
let mut cache = LruCache::new(1024);
cache.insert("hello", vec![0u8; 32]).unwrap();
cache.insert("greetings", vec![0u8; 32]).unwrap();
let old_size = cache.current_size();
let result = cache.mutate("hello", |v| {
*v = Vec::new();
});
assert!(matches!(result, Ok(Some(_))));
assert!(cache.current_size() < old_size);
assert_eq!(2, cache.len());
}
#[test]
fn mutation_on_non_existent_element_is_never_called() {
let mut cache = LruCache::<&str, &str>::new(1024);
let result = cache.mutate("hello", |_| { panic!("mutation was called") });
assert_eq!(Ok(None), result);
}
#[test]
fn reserving_adds_capacity() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
let additional = cache.capacity() - cache.len() + 5;
cache.reserve(additional);
assert!(cache.capacity() >= additional + 2);
assert_eq!(2, cache.len());
assert_eq!(Some((&"hello", &"world")), cache.peek_lru());
assert_eq!(Some((&"greetings", &"moon")), cache.peek_mru());
let additional = additional + 10;
assert!(cache.try_reserve(additional).is_ok());
assert!(cache.capacity() >= additional + 2);
}
#[test]
fn reserving_does_not_change_anything_when_capacity_is_not_exceeded() {
let mut cache = LruCache::with_capacity(1024, 5);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
let capacity_before = cache.capacity();
assert!(cache.try_reserve(3).is_ok());
assert_eq!(capacity_before, cache.capacity());
}
#[test]
fn reserving_fails_on_internal_overflow() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
let try_reserve_result = cache.try_reserve(usize::MAX);
assert_eq!(Err(TryReserveError::CapacityOverflow), try_reserve_result);
}
#[test]
fn reserving_fails_on_external_overflow() {
// In this test, the number of entries does not overflow usize, but the
// number of bytes does. Hence, lru-mem does not raise an error, but
// hashbrown does.
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
let additional = usize::MAX / mem::size_of::<Entry<&str, &str>>();
let try_reserve_result = cache.try_reserve(additional);
assert_eq!(Err(TryReserveError::CapacityOverflow), try_reserve_result);
}
#[test]
fn shrinking_does_not_drop_below_len() {
let mut cache = large_test_cache();
cache.shrink_to(4);
assert!(cache.capacity() >= 5);
assert_eq!(5, cache.len());
cache.insert("hey", "mercury").unwrap();
cache.insert("what's up", "saturn").unwrap();
cache.shrink_to_fit();
assert!(cache.capacity() >= 7);
assert_eq!(7, cache.len());
}
#[test]
fn shrinking_large_cache_decreases_capacity() {
let mut cache = LruCache::with_capacity(1024, 256);
cache.shrink_to(64);
assert!(cache.capacity() < 256);
assert!(cache.capacity() >= 64);
cache.insert("hey", "mercury").unwrap();
cache.insert("what's up", "saturn").unwrap();
cache.shrink_to_fit();
assert!(cache.capacity() < 64);
assert!(cache.capacity() >= 2);
}
#[test]
fn cache_created_with_capacity_does_not_reallocate_before_capacity_is_reached() {
let mut cache = LruCache::with_capacity(4096, 10);
let capacity_before = cache.capacity();
assert!(capacity_before >= 10);
for i in 0..10 {
cache.insert(i, "value").unwrap();
}
assert_eq!(capacity_before, cache.capacity());
}
struct MockHasher {
hash_requests: Arc<Mutex<u32>>
}
impl Hasher for MockHasher {
fn finish(&self) -> u64 {
*self.hash_requests.lock().unwrap() += 1;
0
}
fn write(&mut self, _bytes: &[u8]) { }
}
struct MockBuildHasher {
hash_requests: Arc<Mutex<u32>>
}
impl BuildHasher for MockBuildHasher {
type Hasher = MockHasher;
fn build_hasher(&self) -> Self::Hasher {
MockHasher {
hash_requests: Arc::clone(&self.hash_requests)
}
}
}
#[test]
fn cache_uses_given_hasher() {
let build_hasher = MockBuildHasher {
hash_requests: Arc::new(Mutex::new(0))
};
let mut cache = LruCache::with_hasher(1024, build_hasher);
assert_eq!(0, *cache.hasher().hash_requests.lock().unwrap());
cache.insert("hello", "world").unwrap();
assert_eq!(1, *cache.hasher().hash_requests.lock().unwrap());
cache.insert("greetings", "moon").unwrap();
cache.insert("ahoy", "mars").unwrap();
assert_eq!(3, *cache.hasher().hash_requests.lock().unwrap());
}
#[test]
fn clone_creates_independent_cache() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.insert("greetings", "moon").unwrap();
let mut clone = cache.clone();
clone.insert("ahoy", "mars").unwrap();
cache.remove(&"hello");
cache.touch(&"greetings");
assert_eq!(1, cache.len());
assert_eq!(None, cache.get(&"hello"));
assert_eq!(Some(&"moon"), cache.get(&"greetings"));
assert_eq!(None, cache.get(&"ahoy"));
assert_eq!(3, clone.len());
assert_eq!(Some(&"world"), clone.get(&"hello"));
assert_eq!(Some(&"moon"), clone.get(&"greetings"));
assert_eq!(Some(&"mars"), clone.get(&"ahoy"));
assert!(clone.current_size() > cache.current_size());
let cache_drained = cache.drain().collect::<Vec<_>>();
let cache_drained_expected = vec![("greetings", "moon")];
let clone_drained = clone.drain().collect::<Vec<_>>();
let clone_drained_expected = vec![
("hello", "world"),
("greetings", "moon"),
("ahoy", "mars")
];
assert_eq!(cache_drained_expected, cache_drained);
assert_eq!(clone_drained_expected, clone_drained);
}
#[test]
fn touching_in_singleton_works() {
// Note: This weirdly specific test case isolates a previous bug.
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
cache.touch("hello");
let mut iter = cache.keys();
assert_eq!(Some(&"hello"), iter.next());
assert_eq!(None, iter.next());
}
#[test]
fn empty_cache_formats_for_debug_correctly() {
let cache = LruCache::<&str, &str>::new(1024);
assert_eq!("{}", format!("{:?}", cache));
}
#[test]
fn singleton_cache_formats_for_debug_correctly() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
assert_eq!("{\"hello\": \"world\"}", format!("{:?}", cache));
}
#[test]
fn larger_cache_formats_for_debug_correctly() {
let mut cache = LruCache::new(1024);
cache.insert(2, 4).unwrap();
cache.insert(3, 9).unwrap();
cache.insert(5, 25).unwrap();
cache.insert(7, 49).unwrap();
cache.insert(11, 121).unwrap();
cache.touch(&2);
assert_eq!("{3: 9, 5: 25, 7: 49, 11: 121, 2: 4}", format!("{:?}", cache));
}
}