/
/
/
1"""Shared on-disk writing for the plugin's usearch indexes."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING, Any
6
7if TYPE_CHECKING:
8 from pathlib import Path
9
10
11def save_index(index: Any, path: Path) -> None:
12 """
13 Write a usearch index to disk. Must be called from a worker thread.
14
15 :param index: The usearch index to serialize.
16 :param path: Destination file, overwritten in place.
17 """
18 # usearch's own save(path) holds the GIL for the entire write, so handing it to
19 # asyncio.to_thread still freezes the event loop for the duration. Passing no
20 # path returns the serialized bytes instead of writing them, which costs a
21 # fraction of that; the bytes then go out through plain file I/O, which does
22 # release the GIL.
23 path.write_bytes(index.save())
24