Commit 7f76c37
committed
Optimize PrimaryKey memory: serialize CqlValue into compact byte buffer
Replace PrimaryKey(Vec<CqlValue>) with a serialized byte buffer
representation backed by a new InvariantKey type. CqlValue is 72 bytes
per element due to enum sizing, wasting significant memory for typical
primary key types like Int (4 bytes of data, 68 bytes of padding).
InvariantKey stores values as Arc<[u8]> with a 1-byte type tag followed
by the minimal binary encoding for each value:
Int: 5 bytes (was 72) — 14× smaller
BigInt: 9 bytes (was 72) — 8× smaller
Uuid: 17 bytes (was 72) — 4× smaller
Text(s): 5+len (was 72) — variable
For a single Int primary key column, total per-row memory drops from
~96 bytes (24 Vec overhead + 72 CqlValue) to ~22 bytes (16 Arc<[u8]> +
6 heap), a 4× improvement. With millions of indexed rows stored in the
BiMap, this substantially reduces RSS.
Design:
- invariant_key.rs: reusable encoding engine with InvariantKey,
InvariantKeyBuilder, and InvariantKeyIter types.
- primary_key.rs: thin newtype providing primary-key-specific semantics.
- O(1) clone via Arc; values decoded on demand via get(index) or iter(),
acceptable for primary keys with 1–3 columns.
- Hash/Eq operate on raw bytes instead of format!("{:?}"), which is both
faster and avoids Debug format instability.
- hash_prefix() uses a separate hash domain from Hash::hash (documented).
- Variable-length encoding guards against u32 overflow via try_into().
- InvariantKeyIter implements ExactSizeIterator and FusedIterator.
Changes:
- Add invariant_key.rs with encode/decode, Hash, Eq, Debug, Iterator
- Add primary_key.rs as a newtype over InvariantKey
- Update usearch.rs: closures return owned CqlValue instead of refs
- Update httproutes.rs: use PrimaryKey::len()/get() API
- Remove old PrimaryKey struct and format-based Hash impl from lib.rs
Fixes: VECTOR-5261 parent eb3c999 commit 7f76c37
File tree
11 files changed
+1018
-92
lines changed- crates/vector-store
- src
- index
- tests/integration
11 files changed
+1018
-92
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
451 | 453 | | |
452 | 454 | | |
453 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
454 | 465 | | |
455 | 466 | | |
456 | 467 | | |
| |||
752 | 763 | | |
753 | 764 | | |
754 | 765 | | |
755 | | - | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
756 | 771 | | |
757 | 772 | | |
758 | 773 | | |
| |||
825 | 840 | | |
826 | 841 | | |
827 | 842 | | |
828 | | - | |
829 | | - | |
| 843 | + | |
| 844 | + | |
830 | 845 | | |
831 | 846 | | |
832 | 847 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
609 | | - | |
| 609 | + | |
610 | 610 | | |
611 | 611 | | |
612 | 612 | | |
613 | | - | |
| 613 | + | |
614 | 614 | | |
615 | 615 | | |
616 | 616 | | |
617 | 617 | | |
618 | | - | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
619 | 623 | | |
620 | 624 | | |
621 | 625 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
897 | 897 | | |
898 | 898 | | |
899 | 899 | | |
900 | | - | |
901 | | - | |
902 | | - | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
903 | 903 | | |
904 | 904 | | |
905 | 905 | | |
906 | 906 | | |
907 | 907 | | |
908 | | - | |
| 908 | + | |
909 | 909 | | |
910 | 910 | | |
911 | 911 | | |
| |||
924 | 924 | | |
925 | 925 | | |
926 | 926 | | |
927 | | - | |
| 927 | + | |
928 | 928 | | |
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | 932 | | |
933 | | - | |
| 933 | + | |
934 | 934 | | |
935 | 935 | | |
936 | 936 | | |
937 | | - | |
| 937 | + | |
938 | 938 | | |
939 | 939 | | |
940 | 940 | | |
| |||
946 | 946 | | |
947 | 947 | | |
948 | 948 | | |
949 | | - | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
950 | 952 | | |
951 | 953 | | |
952 | | - | |
| 954 | + | |
953 | 955 | | |
954 | 956 | | |
955 | | - | |
| 957 | + | |
956 | 958 | | |
957 | 959 | | |
958 | | - | |
| 960 | + | |
959 | 961 | | |
960 | 962 | | |
961 | | - | |
| 963 | + | |
962 | 964 | | |
963 | 965 | | |
964 | | - | |
| 966 | + | |
965 | 967 | | |
966 | 968 | | |
967 | 969 | | |
968 | 970 | | |
969 | | - | |
| 971 | + | |
970 | 972 | | |
971 | 973 | | |
972 | 974 | | |
| |||
976 | 978 | | |
977 | 979 | | |
978 | 980 | | |
979 | | - | |
| 981 | + | |
980 | 982 | | |
981 | 983 | | |
982 | 984 | | |
| |||
1083 | 1085 | | |
1084 | 1086 | | |
1085 | 1087 | | |
| 1088 | + | |
1086 | 1089 | | |
1087 | 1090 | | |
1088 | 1091 | | |
| |||
1107 | 1110 | | |
1108 | 1111 | | |
1109 | 1112 | | |
1110 | | - | |
| 1113 | + | |
1111 | 1114 | | |
1112 | 1115 | | |
1113 | 1116 | | |
| |||
1166 | 1169 | | |
1167 | 1170 | | |
1168 | 1171 | | |
1169 | | - | |
| 1172 | + | |
1170 | 1173 | | |
1171 | 1174 | | |
1172 | 1175 | | |
1173 | 1176 | | |
1174 | 1177 | | |
1175 | 1178 | | |
1176 | | - | |
| 1179 | + | |
1177 | 1180 | | |
1178 | 1181 | | |
1179 | 1182 | | |
1180 | 1183 | | |
1181 | 1184 | | |
1182 | 1185 | | |
1183 | | - | |
| 1186 | + | |
| 1187 | + | |
1184 | 1188 | | |
1185 | 1189 | | |
1186 | 1190 | | |
| |||
1205 | 1209 | | |
1206 | 1210 | | |
1207 | 1211 | | |
1208 | | - | |
| 1212 | + | |
1209 | 1213 | | |
1210 | 1214 | | |
1211 | 1215 | | |
1212 | 1216 | | |
1213 | | - | |
| 1217 | + | |
| 1218 | + | |
1214 | 1219 | | |
1215 | 1220 | | |
1216 | 1221 | | |
1217 | 1222 | | |
1218 | 1223 | | |
1219 | | - | |
| 1224 | + | |
| 1225 | + | |
1220 | 1226 | | |
1221 | 1227 | | |
1222 | 1228 | | |
| |||
1233 | 1239 | | |
1234 | 1240 | | |
1235 | 1241 | | |
1236 | | - | |
| 1242 | + | |
| 1243 | + | |
1237 | 1244 | | |
1238 | 1245 | | |
1239 | 1246 | | |
| |||
1243 | 1250 | | |
1244 | 1251 | | |
1245 | 1252 | | |
1246 | | - | |
| 1253 | + | |
| 1254 | + | |
1247 | 1255 | | |
1248 | 1256 | | |
1249 | 1257 | | |
| |||
1267 | 1275 | | |
1268 | 1276 | | |
1269 | 1277 | | |
1270 | | - | |
| 1278 | + | |
1271 | 1279 | | |
1272 | 1280 | | |
1273 | 1281 | | |
| |||
1302 | 1310 | | |
1303 | 1311 | | |
1304 | 1312 | | |
1305 | | - | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
1306 | 1318 | | |
1307 | 1319 | | |
1308 | 1320 | | |
| |||
1312 | 1324 | | |
1313 | 1325 | | |
1314 | 1326 | | |
1315 | | - | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
1316 | 1332 | | |
1317 | 1333 | | |
1318 | 1334 | | |
| |||
1457 | 1473 | | |
1458 | 1474 | | |
1459 | 1475 | | |
| 1476 | + | |
1460 | 1477 | | |
1461 | 1478 | | |
1462 | 1479 | | |
1463 | 1480 | | |
1464 | | - | |
| 1481 | + | |
1465 | 1482 | | |
1466 | 1483 | | |
1467 | | - | |
1468 | | - | |
1469 | | - | |
1470 | | - | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
1471 | 1488 | | |
1472 | 1489 | | |
1473 | 1490 | | |
1474 | | - | |
| 1491 | + | |
1475 | 1492 | | |
1476 | 1493 | | |
1477 | 1494 | | |
| |||
0 commit comments