Large registry
Numbers measured here, in your browser, on this machine. Register a few thousand synthetic commands - or serve them from a static source - open the palette and type: every keystroke matches all of them. The table below times the same matcher and ranker the palette runs, over the same data, so you can see what a keystroke costs before you feel it.
MaxResults rows are
rendered, and Virtualize renders just the visible ones when that limit is raised.
Registering, or serving from a source
var many = Enumerable.Range(1, 2000).Select(i => BaletteCmd.New($"Command {i}")
.Id($"bulk:{i}")
.Group("Bulk")
.Subtitle($"Synthetic entry #{i}")
.Priority(BaletteCommandPriority.Lowest)
.Handler(_ => Task.FromResult(BaletteCommandResult.Toast($"Ran command {i}")))
.Build());
// Every one of them a command of the registry...
_bulk = Palette.Register(many); // ... and later: _bulk.Dispose();
// ... or none of them: a source the default mode asks once something is typed.
_catalog = Palette.RegisterSource(new BaletteStaticSource("Catalog", many.ToList(), maxResults: 50));
Registered commands are part of the registry: listed on the empty query, found by every keystroke, reachable by
Find, ExecuteAsync and their shortcuts. A BaletteStaticSource keeps a large list
out of the registry instead: it is asked only once something is typed, matches its items with the palette's own
matcher and weights, and hands back only its best maxResults (50 by default), listed under its own name -
right for a catalogue of thousands of records, where only what was searched for should show. Both handles come back
from the service; this page disposes them when you leave, so the registry is back to its usual size on every other
page.
What keeps a keystroke cheap
- The registry is read once, not on every keystroke: the palette keeps a snapshot and takes a new one only when the
registry, the commands declared in its markup or its
Commandsparameter changed. PassCommandsa new collection to change it, or callRefresh()after changing one in place. - Global shortcuts are re-evaluated when the snapshot changes, when the palette opens or closes, after a command ran
and on
Refresh()- not while typing. - The query is parsed into terms once per keystroke, not once per command.
- The matcher allocates nothing per field: it reuses per-thread buffers, rejects non-subsequences before the dynamic programming starts, and avoids LINQ.
- Ranking only sorts what it keeps: with
MaxResultsit holds the best rows in a heap and sorts those. - Results from async sources reuse the registry's matches for the same query instead of matching again.
- With very large lists, also turn on
Virtualize- recommended above about 500 visible rows. While it is on, every row isoptions.RowHeight(44 px) tall, whatever--bit-balette-row-heightsays, and each option carriesaria-posinsetandaria-setsize, since rows cannot be wrapped in their groups.
builder.Services.AddBitBaletteServices(options =>
{
options.MaxResults = 1000; // rows kept per query
options.Virtualize = true; // render only the visible ones
options.RowHeight = 44; // every row's height while virtualized, in pixels
});
// A list changed in place is not noticed: hand Commands a new collection, or say so.
_commands.Add(BaletteCmd.New("Command 2001").Build());
Palette.Refresh();
The benchmarks in Tests/Bit.Balette.Benchmarks measure the matcher, the ranker and a whole keystroke
over registries of different sizes, up to 20,000 commands.