Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc (C++)

Code before:
    ORT_RETURN_IF(QNN_SUCCESS != status, "setPowerConfig failed for RPC control latency.");
  }

  return Status::OK();
}

Status QnnBackendManager::SetPerThreadHtpPowerConfigs(const std::thread::id& thread_id, bool pre_run) {
  PerThreadHtpPowerConfigs_t htp_power_configs;
  if (!GetPerThreadHtpPowerConfigMapping(thread_id, htp_power_configs)) {
    return Status::OK();
  }

  auto htp_power_config_id = htp_power_configs.power_config_id;
  if (pre_run) {
    if (htp_power_configs.pre_run_perf_mode.has_value()) {
      ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, htp_power_configs.pre_run_perf_mode.value()));
    }

    if (htp_power_configs.rpc_configs.has_value()) {
      ORT_RETURN_IF_ERROR(SetRpcPowerConfigs(htp_power_config_id,
                                             htp_power_configs.rpc_configs.value().rpc_control_latency,
                                             htp_power_configs.rpc_configs.value().rpc_polling_time));
    }
  } else if (htp_power_configs.post_run_perf_mode.has_value()) {
    ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, htp_power_configs.post_run_perf_mode.value()));
  }

  return Status::OK();
}

Status QnnBackendManager::AddPerThreadHtpPowerConfigMapping(const std::thread::id& thread_id,
                                                            const PerThreadHtpPowerConfigs_t& htp_power_configs) {
  std::lock_guard<std::mutex> lock(per_thread_power_configs_mutex_);

  auto res = per_thread_power_configs_.find(thread_id);
  ORT_RETURN_IF(res != per_thread_power_configs_.end(), "Trying to set HtpPowerConfigs for thread id ", thread_id,
                " but one already exists!");

  per_thread_power_configs_.emplace(thread_id, std::move(htp_power_configs));

  return Status::OK();
}

bool QnnBackendManager::GetPerThreadHtpPowerConfigMapping(const std::thread::id& thread_id,
                                                          PerThreadHtpPowerConfigs_t& htp_power_configs) {
  std::lock_guard<std::mutex> lock(per_thread_power_configs_mutex_);

  auto it = per_thread_power_configs_.find(thread_id);
  if (it == per_thread_power_configs_.end()) {
    return false;
  }

Review comment (performance):
```suggestion
    if (htp_power_configs.pre_run_perf_mode.has_value()) {
      ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, *htp_power_configs.pre_run_perf_mode));
    }

    if (htp_power_configs.rpc_configs.has_value()) {
      ORT_RETURN_IF_ERROR(SetRpcPowerConfigs(htp_power_config_id,
                                             htp_power_configs.rpc_configs->rpc_control_latency,
                                             htp_power_configs.rpc_configs->rpc_polling_time));
    }
  } else if (htp_power_configs.post_run_perf_mode.has_value()) {
    ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, *htp_power_configs.post_run_perf_mode));
  }
```

nit: optional::value() checks whether it contains a value. that's not necessary here because of the earlier call to optional::has_value().

Code after:
    ORT_RETURN_IF(QNN_SUCCESS != status, "setPowerConfig failed for RPC control latency.");
  }

  return Status::OK();
}

Status QnnBackendManager::SetPerThreadHtpPowerConfigs(const std::thread::id& thread_id, bool pre_run) {
  PerThreadHtpPowerConfigs_t htp_power_configs;
  if (!GetPerThreadHtpPowerConfigMapping(thread_id, htp_power_configs)) {
    return Status::OK();
  }

  auto htp_power_config_id = htp_power_configs.power_config_id;
  if (pre_run) {
    if (htp_power_configs.pre_run_perf_mode.has_value()) {
      ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, *htp_power_configs.pre_run_perf_mode));
    }

    if (htp_power_configs.rpc_configs.has_value()) {
      ORT_RETURN_IF_ERROR(SetRpcPowerConfigs(htp_power_config_id,
                                             htp_power_configs.rpc_configs->rpc_control_latency,
                                             htp_power_configs.rpc_configs->rpc_polling_time));
    }
  } else if (htp_power_configs.post_run_perf_mode.has_value()) {
    ORT_RETURN_IF_ERROR(SetHtpPowerConfig(htp_power_config_id, *htp_power_configs.post_run_perf_mode));
  }

  return Status::OK();
}

Status QnnBackendManager::AddPerThreadHtpPowerConfigMapping(const std::thread::id& thread_id,
                                                            const PerThreadHtpPowerConfigs_t& htp_power_configs) {
  std::lock_guard<std::mutex> lock(per_thread_power_configs_mutex_);

  auto res = per_thread_power_configs_.find(thread_id);
  ORT_RETURN_IF(res != per_thread_power_configs_.end(), "Trying to set HtpPowerConfigs for thread id ", thread_id,
                " but one already exists!");

  per_thread_power_configs_.emplace(thread_id, std::move(htp_power_configs));

  return Status::OK();
}

bool QnnBackendManager::GetPerThreadHtpPowerConfigMapping(const std::thread::id& thread_id,
                                                          PerThreadHtpPowerConfigs_t& htp_power_configs) {
  std::lock_guard<std::mutex> lock(per_thread_power_configs_mutex_);

  auto it = per_thread_power_configs_.find(thread_id);
  if (it == per_thread_power_configs_.end()) {
    return false;
  }