Skip to content

Commit

Permalink
📝 some optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Sep 4, 2024
1 parent 71588aa commit 4ce0042
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/gen_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn cold() {}
// }

#[inline]
fn unlikely(b: bool) -> bool {
pub(crate) fn unlikely(b: bool) -> bool {
if b {
cold()
}
Expand Down
1 change: 1 addition & 0 deletions src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl ContextStack {
}

#[inline]
#[cold]
fn type_error<A>(msg: &str) -> ! {
error!("{msg}, expected type: {}", std::any::type_name::<A>());
std::panic::panic_any(Error::TypeErr)
Expand Down
12 changes: 6 additions & 6 deletions src/yield_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::any::Any;
use std::sync::atomic;

use crate::gen_impl::Generator;
use crate::gen_impl::{unlikely, Generator};
use crate::reg_context::RegContext;
use crate::rt::{is_generator, Context, ContextStack, Error};

Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn raw_yield_now(env: &ContextStack, cur: &mut Context) {
#[inline]
fn raw_yield<T: Any>(env: &ContextStack, context: &mut Context, v: T) {
// check the context
if !context.is_generator() {
if unlikely(!context.is_generator()) {
panic!("yield from none generator context");
}

Expand All @@ -54,7 +54,7 @@ fn raw_yield<T: Any>(env: &ContextStack, context: &mut Context, v: T) {
raw_yield_now(env, context);

// here we just panic to exit the func
if context._ref != 1 {
if unlikely(context._ref != 1) {
std::panic::panic_any(Error::Cancel);
}
}
Expand All @@ -80,7 +80,7 @@ pub fn get_yield<A: Any>() -> Option<A> {
#[inline]
fn raw_get_yield<A: Any>(context: &mut Context) -> Option<A> {
// check the context
if !context.is_generator() {
if unlikely(!context.is_generator()) {
{
error!("get yield from none generator context");
std::panic::panic_any(Error::ContextErr);
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn yield_from<A: Any, T: Any>(mut g: Generator<A, T>) -> Option<A> {
let env = ContextStack::current();
let context = env.top();
let mut p = context.get_para();
while !g.is_done() {
while unlikely(!g.is_done()) {
match g.raw_send(p) {
None => return None,
Some(r) => raw_yield(&env, context, r),
Expand All @@ -134,7 +134,7 @@ pub fn co_yield_with<T: Any>(v: T) {
// }

// here we just panic to exit the func
if context._ref != 1 {
if unlikely(context._ref != 1) {
std::panic::panic_any(Error::Cancel);
}

Expand Down

0 comments on commit 4ce0042

Please sign in to comment.