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

Switch to the HashMap's Entry API #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jalil-salame
Copy link

Improvement:

  • When doing if get_mut(..) else .insert it doesn't rehash the key
  • It is arguably more readable

Problems:

  • When filling up the df HashMap, it clones the string unnecessarily:

    Old code:

    if let Some(freq) = self.df.get_mut(t) {
        *freq += 1;
    } else {
        // `t` is only cloned when the key is not present
        self.df.insert(t.clone(), 1);
    }

    New code:

    // Always clones `t`
    self.df.entry(t.clone())
        .and_modify(|freq| *freq += 1)
        .or_insert(1);
  • Uses closures so performance in debug builds might degrade

Improvement:
- When doing `if get_mut(..) else .insert` it doesn't rehash the key
- It is arguably more readable

Problems:
- When filling up the `df` HashMap, it clones the string unnecessarily:

    Old code:
    ```rust
    if let Some(freq) = self.df.get_mut(t) {
        *freq += 1;
    } else {
        // `t` is only cloned when the key is not present
        self.df.insert(t.clone(), 1);
    }
    ```

    ```rust
    // Always clones `t`
    self.df.entry(t.clone())
        .and_modify(|freq| *freq += 1)
        .or_insert(1);
    ```
- Uses closures so performance in `debug` builds might degrade
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.

1 participant