How to keep pattern match branch order

I have write some code like

pub fn range_char(ch: char) -> bool {
    match ch as u32 {
        0x4e00..=0x9fff => return true,
        0xff0c => {
            return true;
        }
        0x3002 => {
            return true;
        }
        0x3400..=0x4dbf => return true, // CJK Unified Ideographs Extension A
        0x20000..=0x2a6df => return true, // CJK Unified Ideographs Extension B
        0x2a700..=0x2b73f => return true, // CJK Unified Ideographs Extension C
        0x2b740..=0x2b81f => return true, // CJK Unified Ideographs Extension D
        0x2b820..=0x2ceaf => return true, // CJK Unified Ideographs Extension E
        0x3300..=0x33ff => return true, // https://en.wikipedia.org/wiki/CJK_Compatibility
        0xfe30..=0xfe4f => return true, // https://en.wikipedia.org/wiki/CJK_Compatibility_Forms
        0xf900..=0xfaff => return true, // https://en.wikipedia.org/wiki/CJK_Compatibility_Ideographs
        0x2f800..=0x2fa1f => return true, // https://en.wikipedia.org/wiki/CJK_Compatibility_Ideographs_Supplement
        0x00b7 => return true,            //·
        0x00d7 => return true,            //×
        0x2014 => return true,            //—
        0x2018 => return true,            //‘
        0x2019 => return true,            //’
        0x201c => return true,            //“
        0x201d => return true,            //”
        0x2026 => return true,            //…
        0x3001 => return true,            //、
        0x300a => return true,            //《
        0x300b => return true,            //》
        0x300e => return true,            //『
        0x300f => return true,            //』
        0x3010 => return true,            //【
        0x3011 => return true,            //】
        0xff01 => return true,            //!
        0xff08 => return true,            //(
        0xff09 => return true,            //)
        0xff1a => return true,            //:
        0xff1f => return true,            //?
        _ => false,
    }
}


You could see the asm code, the branch has been reordered, but the pattern match branch order is Deliberately designed, high frequency branch is put in the front.
How should i avoid rust compiler reorder my pattern match branch?
a simplify compared version:

First you can try to replace it with a sequence of if-returns. If it doesn't work, I don't think this level of optimizations can reliably be written in high level languages like C and Rust due to its own abstractions. There's asm! macro in nightly. If you want to stay in stable, you can always write some asm file and link to it.

if statement work for me, but pattern matching looks better, so i wonder if there is a way to avoid pattern matching reorder the branch.

Short of using profile-guided optimization, not that I know of.

If 'if' expressions compile to the right code for you, and you want your source code to look more concise like a 'match' expression, consider defining a macro.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.