Repository: paritytech/substrate
File: utils/frame/remote-externalities/src/lib.rs (Rust)

Code before:
			_ => panic!("Unexpected mode: Online"),
		}
	}
}

// RPC methods
impl<B: BlockT> Builder<B>
where
	B::Hash: DeserializeOwned,
	B::Header: DeserializeOwned,
{
	const MAX_PARALLELISM: usize = 4;
	const BATCH_SIZE_INCREASE_FACTOR: f32 = 1.10;
	const BATCH_SIZE_DECREASE_FACTOR: f32 = 0.50;
	const INITIAL_BATCH_SIZE: usize = 5000;
	// NOTE: increasing this value does not seem to impact speed all that much.
	const DEFAULT_KEY_DOWNLOAD_PAGE: u32 = 1000;

	/// Get the number of threads to use.
	/// Cap the number of threads. Performance improvement beyond a small number of threads is
	/// negligible, and too many threads can create issues with the HttpClient.
	fn threads() -> NonZeroUsize {
		let avaliable = thread::available_parallelism()
			.unwrap_or(NonZeroUsize::new(1usize).expect("1 is non-zero; qed"))
			.get();
		assert!(avaliable > 0, "avaliable parallelism must be greater than 0");
		return NonZeroUsize::new(min(avaliable, Self::MAX_PARALLELISM))
			.expect("avaliable is non-zero and MAX_PARRELISM is non-zero; qed")
	}

	async fn rpc_get_storage(
		&self,
		key: StorageKey,
		maybe_at: Option<B::Hash>,
	) -> Result<Option<StorageData>, &'static str> {
		trace!(target: LOG_TARGET, "rpc: get_storage");
		self.as_online().rpc_client().storage(key, maybe_at).await.map_err(|e| {
			error!(target: LOG_TARGET, "Error = {:?}", e);
			"rpc get_storage failed."
		})
	}

	/// Get the latest finalized head.
	async fn rpc_get_head(&self) -> Result<B::Hash, &'static str> {
		trace!(target: LOG_TARGET, "rpc: finalized_head");

		// sadly this pretty much unreadable...
		ChainApi::<(), _, B::Header, ()>::finalized_head(self.as_online().rpc_client())
			.await
			.map_err(|e| {
				error!(target: LOG_TARGET, "Error = {:?}", e);

Review comment (suggestion):
You could also add an env var check like `TRY_RUNTIME_MAX_THREADS`, that would be easier than to add a CLI argument, but a bit hacky.

Code after:
			_ => panic!("Unexpected mode: Online"),
		}
	}
}

// RPC methods
impl<B: BlockT> Builder<B>
where
	B::Hash: DeserializeOwned,
	B::Header: DeserializeOwned,
{
	const DEFAULT_PARALLELISM: usize = 4;
	const BATCH_SIZE_INCREASE_FACTOR: f32 = 1.10;
	const BATCH_SIZE_DECREASE_FACTOR: f32 = 0.50;
	const INITIAL_BATCH_SIZE: usize = 5000;
	// NOTE: increasing this value does not seem to impact speed all that much.
	const DEFAULT_KEY_DOWNLOAD_PAGE: u32 = 1000;

	/// Get the number of threads to use.
	/// Cap the number of threads. Performance improvement beyond a small number of threads is
	/// negligible, and too many threads can create issues with the HttpClient.
	fn threads() -> NonZeroUsize {
		let avaliable = thread::available_parallelism()
			.unwrap_or(NonZeroUsize::new(1usize).expect("1 is non-zero; qed"))
			.get();
		assert!(avaliable > 0, "avaliable parallelism must be greater than 0");

		let requested: usize = match std::env::var("TRY_RUNTIME_MAX_THREADS") {
			Ok(n) => n.parse::<usize>().expect("TRY_RUNTIME_MAX_THREADS must be a number"),
			Err(_) => Self::DEFAULT_PARALLELISM,
		};
		assert!(requested > 0, "TRY_RUNTIME_MAX_THREADS must be greater than 0");
		return NonZeroUsize::new(min(requested, avaliable))
			.expect("requested and avaliable are non-zero; qed")
	}

	async fn rpc_get_storage(
		&self,
		key: StorageKey,
		maybe_at: Option<B::Hash>,
	) -> Result<Option<StorageData>, &'static str> {
		trace!(target: LOG_TARGET, "rpc: get_storage");
		self.as_online().rpc_client().storage(key, maybe_at).await.map_err(|e| {
			error!(target: LOG_TARGET, "Error = {:?}", e);
			"rpc get_storage failed."
		})
	}

	/// Get the latest finalized head.
	async fn rpc_get_head(&self) -> Result<B::Hash, &'static str> {
		trace!(target: LOG_TARGET, "rpc: finalized_head");