Skip to content

Comments

perf(transformer/styled_components): replace hashmap with array#12170

Merged
graphite-app[bot] merged 1 commit intomainfrom
07-09-perf_transformer_styled_components_replace_hashmap_with_array
Jul 10, 2025
Merged

perf(transformer/styled_components): replace hashmap with array#12170
graphite-app[bot] merged 1 commit intomainfrom
07-09-perf_transformer_styled_components_replace_hashmap_with_array

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Jul 9, 2025

Follow-on after #12066.

helpers was an FxHashMap<String, SymbolId>, but we know all the possible keys, and there's only 6 of them. So use a small array [Option<SymbolId>; 6] instead, and convert from strings to indexes into that array via a StyledComponentsHelper enum.

Array (24 bytes) is smaller than an FxHashMap, and involves no allocations, as well as avoiding hashing strings.

Note: Using an enum rather than an untyped usize index, as with an enum, compiler can statically prove indexing into the array can never be out of bounds, so it skips bounds checks.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance labels Jul 9, 2025
Copy link
Member Author

overlookmotel commented Jul 9, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 9, 2025

CodSpeed Instrumentation Performance Report

Merging #12170 will not alter performance

Comparing 07-09-perf_transformer_styled_components_replace_hashmap_with_array (bcd5094) with 07-09-refactor_transformer_styled_components_move_clippy_attr (e222e26)

Summary

✅ 34 untouched benchmarks

@overlookmotel overlookmotel marked this pull request as ready for review July 9, 2025 22:35
@overlookmotel overlookmotel requested a review from Dunqing as a code owner July 9, 2025 22:35
@overlookmotel
Copy link
Member Author

overlookmotel commented Jul 9, 2025

I also came up with this alternative, which I imagine is more performant. It uses the fact that there are certain bit patterns in the first 3 letters of these strings which are unique (I used a little script to find them). https://godbolt.org/z/4KhzKTTaz

But I don't think it's worth the complication. We could at some point try to codegen code like this as a lightweight replacement for hashmaps and phf! hashmaps for small maps. Just recording this here in case we want to come back to this.

#[derive(Copy, Clone)]
#[repr(u8)]
enum StyledComponentsHelper {
    CreateGlobalStyle = 0,
    Css = 1,
    UseTheme = 2,
    WithTheme = 3,
    InjectGlobal = 4,
    Keyframes = 5,
}

const STRS: &[&str] = &[
    "createGlobalStyle", "css", "useTheme", "withTheme", "injectGlobal", "keyframes"
];

impl StyledComponentsHelper {
    fn from_str(name: &str) -> Option<Self> {
        if name.len() < 3 {
            return None;
        }
        // 3rd and 4th bits of 1st byte and 5th bit of 3rd byte make a number
        // between 0..=5 which is different for each string
        let bytes = name.as_bytes();
        let index = ((bytes[0] >> 1) & 6) | ((bytes[2] >> 4) & 1);
        if index >= 6 {
            return None;
        }
        if name != STRS[index as usize] {
            return None;
        }
        // This compiles down to a no-op
        Some(match index {
            0 => StyledComponentsHelper::CreateGlobalStyle,
            1 => StyledComponentsHelper::Css,
            2 => StyledComponentsHelper::UseTheme,
            3 => StyledComponentsHelper::WithTheme,
            4 => StyledComponentsHelper::InjectGlobal,
            5 => StyledComponentsHelper::Keyframes,
            _ => unreachable!(),
        })
    }
}

Copy link
Member

@Dunqing Dunqing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current approach looks better, and the readability is good as well!

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Jul 10, 2025

Merge activity

Follow-on after #12066.

`helpers` was an `FxHashMap<String, SymbolId>`, but we know all the possible keys, and there's only 6 of them. So use a small array `[Option<SymbolId>; 6]` instead, and convert from strings to indexes into that array via a `StyledComponentsHelper` enum.

Array (24 bytes) is smaller than an `FxHashMap`, and involves no allocations, as well as avoiding hashing strings.

Note: Using an enum rather than an untyped `usize` index, as with an enum, compiler can statically prove indexing into the array can never be out of bounds, so it skips bounds checks.
@graphite-app graphite-app bot force-pushed the 07-09-refactor_transformer_styled_components_move_clippy_attr branch from 2d8af41 to e222e26 Compare July 10, 2025 00:40
@graphite-app graphite-app bot force-pushed the 07-09-perf_transformer_styled_components_replace_hashmap_with_array branch from a5ab429 to bcd5094 Compare July 10, 2025 00:41
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
Base automatically changed from 07-09-refactor_transformer_styled_components_move_clippy_attr to main July 10, 2025 00:48
@graphite-app graphite-app bot merged commit bcd5094 into main Jul 10, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 07-09-perf_transformer_styled_components_replace_hashmap_with_array branch July 10, 2025 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants