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

Possible optimisation: Intern the UserName in Name #288

Open
ChrisJefferson opened this issue Apr 3, 2024 · 0 comments
Open

Possible optimisation: Intern the UserName in Name #288

ChrisJefferson opened this issue Apr 3, 2024 · 0 comments

Comments

@ChrisJefferson
Copy link
Contributor

I had a quick run of a profiler to see where time was being spent, and about half the time was spent copying String in UserName in Name.

The Name enum looks like:

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Name {
    UserName(String),
    MachineName(i32),
}

I quickly hacked this to:

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Name {
    UserName(u32),
    MachineName(i32),
}

and then stored the names in a static global lookup table (which was just a vec<String>). I made no effort to de-duplicate the strings, or clean up strings once they are no longer being used. I only hacked enough to make the rewrite-tests pass, but they seemed to be about twice as quick.

It's of course possible I horribly messed things up, but I think this is such a frequently used enum it's probably worth opimising like this. If we did de-duplicate the names (which wouldn't be that hard, store for example a vec<String> for the names, and a HashMap<String, u32> to go from Strings back to indices in the Vec, then always give out the same int)`, then equality of strings becomes equality of the int)

At this point it might not be worth the work for a 2x speedup, but one reason to think about it is right now it's probably not too much work (mainly adding some accessors and a constructor to Name is 95% of the work).

I'm happy to advise, or even share my horrible code, but my code is definately not suitable for merging! (I chucked an unsafe in just to make Rust stop complaining, which wasn't required :) )

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

No branches or pull requests

1 participant