initialize method

void initialize({
  1. required int poolSize,
  2. required int inputFloats,
  3. required CompiledModel create(),
  4. void onFirstModel(
    1. CompiledModel firstModel
    )?,
})

Builds poolSize slots (clamped to at least 1), disposing any existing ones first.

create is called once per slot to produce a fresh CompiledModel; each slot also allocates a reusable input buffer of inputFloats float32 values. onFirstModel, if given, is called with the first model built — use it to resolve I/O tensor indices once.

Implementation

void initialize({
  required int poolSize,
  required int inputFloats,
  required CompiledModel Function() create,
  void Function(CompiledModel firstModel)? onFirstModel,
}) {
  dispose();
  final int n = poolSize < 1 ? 1 : poolSize;
  try {
    for (int i = 0; i < n; i++) {
      final CompiledModel model = create();
      try {
        if (i == 0) onFirstModel?.call(model);
      } catch (_) {
        model.close();
        rethrow;
      }
      _slots.add(_CompiledSlot(model, Float32List(inputFloats)));
    }
  } catch (_) {
    // Close any slots already built so a failed setup (e.g. an unsupported
    // model rejected by onFirstModel) leaves no leaked native models, and the
    // caller can fall back to another engine.
    dispose();
    rethrow;
  }
}