# Refinement

> refine re-runs the neighbor selection over entries already in the graph and answers a usearch_refine_report_t of the nodes re-selected, those whose neighbors changed, the islands found and reconnected, the neighborhoods re-stitched with the links added and dropped, the verdict, and the opening and reachability health checks around the round, so a batch built at a low expansion factor reaches the quality of a slower build afterwards.

`#include <usearch/types.hpp>``#include <usearch/dense_index.hpp>` 3.0.0-rc.1 · cabb6603

`refine` re-runs the neighbor selection over entries already in the graph and answers a `usearch_refine_report_t` of the nodes re-selected, those whose neighbors `changed`, the islands found and reconnected, the neighborhoods re-stitched with the links added and dropped, the `verdict`, and the `opening` and `reachability` health checks around the round, so a batch built at a low expansion factor reaches the quality of a slower build afterwards. A `verdict` other than `usearch_refine_improved_k` is what settled looks like. On a RAM handle searches may run throughout, on a VRAM one they may not, and insertions never may.

```cpp
us::dense_index_on<usearch_completion_blocking_k> const receiver = index.on(executor);
for (;;) {
    auto [report, status] = receiver.refine({.refine = {.expansion_factor = 128}});
    if (status.failed() || report.verdict != usearch_refine_improved_k) break;
}
```

On a loaded NVMe handle `memory_limit` bounds the bytes a round holds for the one part it rewrites: zero rounds the part in RAM, and a smaller budget streams its rounds from the drives.

A `dense_refine_plan_t` carries a round across calls: a stop leaves it live, the next call holding it continues where it stopped, `phase` reports the phase the call stopped in, and `refine_plan_finish` closes it.

```cpp
us::dense_refine_plan_t plan;
while (receiver.refine({.progress = {before_the_deadline}, .plan = &plan}) == us::status_code_t::cancelled_k)
    std::printf("stopped in %s at %zu of %zu\n", usearch_phase_name(plan.phase().phase), plan.phase().completed,
                plan.phase().total);
auto [report, status] = receiver.refine_plan_finish(std::move(plan));
```

A deferred call reads and writes the plan it carries, which stays where it is until the call is final.

```cpp
us::deferred<usearch_refine_report_t> call = index.deferred(executor).refine({.plan = &plan});
auto [answer, status] = co_await call.wait(loop); // or: call.wait()
```

### Resharding

`reshard` moves the entries of its `sources` into its `targets`, keeping their links rather than re-inserting their entries, then runs a round on every target that took entries; the receiver supplies the executor. Several sources gather into the one target among them, one source cuts into several empty targets, and one handle alone recuts its own shards in place under `shard_capacity`. Every handle stays yours: a source the targets do not name ends empty and live. Readers of a RAM target may keep searching throughout, while a VRAM one admits no overlapping search.

```cpp
usearch_dense_index_t const gathered[3] = {index.handle(), first_shard.handle(), second_shard.handle()};
us::status_t const gather = index.on(executor).reshard(gathered, {gathered, 1}).status;
usearch_dense_index_t const source[1] = {index.handle()}, parts[2] = {left.handle(), right.handle()};
us::status_t const cut = index.on(executor).reshard(source, parts).status; // two empty targets take a part each
```

A stopped `reshard` cuts only its closing rounds, every entry already moved, and reports `status_code_t::cancelled_k`; a `dense_reshard_plan_t` continues it as a refine plan does, and `reshard_plan_finish` closes it.

## API

### Types

#### `refine_config`

The knobs of [`refine`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#refine) and the `plan_type_` it opens or continues.

[types.hpp:807 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/types.hpp#L807)

```cpp
template <typename plan_type_>
struct refine_config
```

#### Fields

| `refine` | `usearch_refine_options_t` | The walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each. |
| - | - | - |
| `progress` | `progress_t` | Hears every barrier of every phase, and may cancel the round there. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |
| `plan` | `plan_type_ *` | The plan a cancel leaves live; `nullptr` runs one round to its end. |

#### `reshard_config`

The knobs of a modality's [`reshard`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#reshard), whose handles recut nothing in place, and the `plan_type_` it opens or continues.

[types.hpp:825 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/types.hpp#L825)

```cpp
template <typename plan_type_>
struct reshard_config
```

#### Fields

| `refine` | `usearch_refine_options_t` | The walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each. |
| - | - | - |
| `seed` | `std::uint64_t` | Picks the part roots of a split: equal seeds over an unchanged source split equally. |
| `progress` | `progress_t` | Hears every barrier of the closing rounds, and may cancel them there, never a move. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |
| `plan` | `plan_type_ *` | The plan a cancel leaves live; `nullptr` moves and runs to the end. |

#### `dense_reshard_config_t`

The placement [`reshard`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#reshard) recuts toward, the knobs of its closing rounds and its plan.

[dense_index.hpp:144 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L144)

```cpp
struct dense_reshard_config_t
```

#### Fields

| `ram` | `usearch_ram_options_t const *` | The placement of a RAM handle recut in place; `nullptr` keeps it. |
| - | - | - |
| `vram` | `usearch_vram_options_t const *` | The placement of a VRAM handle recut in place; `nullptr` keeps it. |
| `nvme` | `usearch_nvme_options_t const *` | The placement of an NVMe handle recut in place; `nullptr` keeps it. |
| `refine` | `usearch_refine_options_t` | The walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each. |
| `seed` | `std::uint64_t` | Picks the part roots of a split: equal seeds over an unchanged source split equally. |
| `progress` | `progress_t` | Hears every barrier of the closing rounds, and may cancel them there, never a move. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |
| `plan` | `dense_reshard_plan_t *` | The plan a cancel leaves live; `nullptr` moves and runs to the end. |
