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

feat(socketio/ns): store ns path and rooms as Cow<'static, str> #124

Merged
merged 23 commits into from Oct 30, 2023

Conversation

Totodore
Copy link
Owner

@Totodore Totodore commented Oct 21, 2023

Store ns path and rooms as Cow<'static, str> :

  • Change ns path from String to Cow
  • Change rooms in LocalAdapter from String to Cow
  • Change events callbacks in Socket from String to Cow
  • Pre-allocate a String buffer with a size_hint function when encoding to avoid string reallocations
  • Decode Packets from bytes slice rather than String to avoid Char iterators (removes deps itertools)
  • Benchmark memory consumption before and after

@Totodore Totodore self-assigned this Oct 21, 2023
@Totodore Totodore added the enhancement New feature or request label Oct 21, 2023
@Totodore Totodore linked an issue Oct 21, 2023 that may be closed by this pull request
socketioxide/src/client.rs Fixed Show fixed Hide fixed
socketioxide/src/handler.rs Fixed Show fixed Hide fixed
socketioxide/src/handler.rs Fixed Show fixed Hide fixed
socketioxide/src/packet.rs Fixed Show fixed Hide fixed
socketioxide/src/packet.rs Fixed Show fixed Hide fixed
@Totodore Totodore changed the title feat(socketio/ns): store ns path as Cow<'static, str> feat(socketio/ns): store ns path and rooms as Cow<'static, str> Oct 21, 2023
socketioxide/src/operators.rs Fixed Show fixed Hide fixed
socketioxide/src/operators.rs Fixed Show fixed Hide fixed
socketioxide/src/packet.rs Fixed Show fixed Hide fixed
socketioxide/src/packet.rs Fixed Show fixed Hide fixed
socketioxide/src/packet.rs Fixed Show fixed Hide fixed
@Totodore
Copy link
Owner Author

Benchmark results: benchmark-feat-cow-namespaces-and-rooms.zip
Generated with: cargo bench --bench packet_encode --bench packet_decode --all-features

@Totodore
Copy link
Owner Author

One of the remaining things that won't be improved in this PR is the "int to string" conversion. It is currently using to_string which allocates.

One of the solution was to use this function, however it inserts digits in reverse order:

fn insert_number(mut v: usize, res: &mut String) {
    debug_assert!(v > 0);
    while v > 0 {
        let n = (v % 10) as u8;
        v /= 10;
        res.push((n + 0x30) as char);
    }
}

@Totodore Totodore merged commit 8de2238 into main Oct 30, 2023
9 checks passed
@Totodore Totodore deleted the feat-cow-namespace-and-rooms branch October 30, 2023 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Use a Cow<T> rather than a String to store events and namespace
1 participant