Skip to content

Recipes by workload

The features section is organised by what a thing is. This section is organised by what you are doing, because the right API usually follows from the workload rather than from the operation.

Four workloads

The workload decides the API

The same operation, four times:

WorkloadTokenizer callWhat you optimiseWatch out for
Interactivetokenize()Latency and readabilityDoing O(n) work over a whole corpus per request
Streamingtokens()Peak memory, time to first resultAny collect() in the middle of the pipeline
Batchtokenize_into()Allocations per documentForgetting buf.clear() at the top of the loop
Parallelmap_init(Vec::new, …) + tokenize_into()CPU utilisationTasks too small to pay for scheduling

None of those rows is "the fast one" — they are answers to different questions. See Choosing the right API.

Problem recipes

Complete programs for common tasks:

Before you optimise

Every recipe here that uses a performance-oriented API also says what it costs you in code complexity, because that is a real cost. Two rules of thumb:

  • Optimise the sequential version first. A tokenize_into loop that removes ten million allocations may make threads unnecessary — and it composes with them if not.
  • Measure before and after. Batch is the one workload where these techniques reliably show up. If the numbers do not move, put the simple version back.

If you are not in the workload a recipe was written for, take the simpler version from Your first program instead.

Released under the MIT License.