Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize some codes with global static variables #476

Merged
merged 1 commit into from
Jun 30, 2024

Conversation

SKTT1Ryze
Copy link
Contributor

🤔 This is a ...

  • New feature
  • Bug fix
  • Performance optimization
  • Enhancement feature
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

💡 Background and solution

Hello, everyone. I was learning the project this morning and found some code can be optimized by removing the unnecessary RwLock<T> and OnceLock<T>. In the following code, RUNTIME_CONTROLLER can be wrapped with OnceCell<T> only because it is initialized once and accessed in single thread.

// [`OnceLock<T>`] and [`RwLock<T>`] are unnecessary.
static RUNTIME_CONTROLLER: OnceLock<std::sync::RwLock<RuntimeController>> =
    OnceLock::new();

// we can use [`OnceCell<T>`] instead.
static RUNTIME_CONTROLLER: OnceCell<RuntimeController> = OnceCell::new();

What's more, &static str can be created with include_str! for more concise coding style.

Thank you! : )

☑️ Self-Check before Merge

Self-Tested with cargo run and curl -x http://127.0.0.1:7890 https://httpbin.yba.dev/ip.

⚠️ Please check all items below before requesting a reviewing. ⚠️

  • Doc is updated/provided or not needed
  • Changelog is provided or not needed

@SKTT1Ryze SKTT1Ryze requested a review from ibigbug June 30, 2024 05:11
@SKTT1Ryze SKTT1Ryze enabled auto-merge (squash) June 30, 2024 06:16
@SKTT1Ryze SKTT1Ryze merged commit 5e24ea8 into master Jun 30, 2024
20 checks passed
@SKTT1Ryze SKTT1Ryze deleted the optimize/static_variables branch June 30, 2024 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants