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

reduce PathGen::path_gen memory allocation. #960

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions http-ws/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ impl Flags {
}
}

impl Default for Codec {
fn default() -> Self {
unimplemented!("please use Codec::new")
}
}

impl Codec {
/// Create new WebSocket frames decoder.
pub const fn new() -> Codec {
Expand Down
14 changes: 14 additions & 0 deletions http/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ pub struct HttpServiceBuilder<
pub(crate) _body: PhantomData<fn(V, St)>,
}

impl Default
for HttpServiceBuilder<
marker::Http,
net::Stream,
tls::NoOpTlsAcceptorBuilder,
DEFAULT_HEADER_LIMIT,
DEFAULT_READ_BUF_LIMIT,
DEFAULT_WRITE_BUF_LIMIT,
>
{
fn default() -> Self {
unimplemented!("please use HttpServiceBuilder::new")
}
}
impl
HttpServiceBuilder<
marker::Http,
Expand Down
2 changes: 1 addition & 1 deletion http/src/util/middleware/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod router_impl {
where
I: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down
24 changes: 12 additions & 12 deletions http/src/util/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<Obj> Router<Obj> {
F::Response: Service<Req>,
Req: IntoObject<F::Route<F>, Arg, Object = Obj>,
{
let path = builder.path_gen(String::from(path));
let path = builder.path_gen(path);
assert!(self
.routes
.insert(path, Req::into_object(F::route_gen(builder)))
Expand Down Expand Up @@ -141,8 +141,8 @@ pub trait PathGen {
/// path generator.
///
/// default to passthrough of original prefix path.
fn path_gen(&mut self, prefix: String) -> String {
prefix
fn path_gen(&mut self, prefix: &str) -> String {
String::from(prefix)
}
}

Expand All @@ -162,7 +162,8 @@ impl<Obj> PathGen for Router<Obj>
where
Obj: PathGen,
{
fn path_gen(&mut self, mut path: String) -> String {
fn path_gen(&mut self, path: &str) -> String {
let mut path = String::from(path);
if path.ends_with("/*") {
path.pop();
}
Expand All @@ -171,12 +172,11 @@ where
path.pop();
}

if let Some(prefix) = self.prefix.replace(path.len()) {
self.prefix = Some(path.len() + prefix);
}
let prefix = self.prefix.get_or_insert(0);
*prefix += path.len();

self.routes.iter_mut().for_each(|(_, v)| {
v.path_gen(path.clone());
v.path_gen(path.as_str());
});

path.push_str("/*");
Expand Down Expand Up @@ -230,7 +230,7 @@ impl<F, S, M> PathGen for PipelineT<F, S, M>
where
F: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.first.path_gen(prefix)
}
}
Expand All @@ -253,7 +253,7 @@ impl<S> PathGen for RouterMapErr<S>
where
S: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down Expand Up @@ -324,7 +324,7 @@ mod object {
pub struct RouteObject<Arg, S, E>(pub Box<dyn RouteService<Arg, Response = S, Error = E> + Send + Sync>);

impl<Arg, S, E> PathGen for RouteObject<Arg, S, E> {
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down Expand Up @@ -362,7 +362,7 @@ where
where
T: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down
6 changes: 6 additions & 0 deletions router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub struct Router<T> {
root: Node<T>,
}

impl<T> Default for Router<T> {
fn default() -> Self {
unimplemented!("please use Router::new");
}
}

impl<T> Router<T> {
/// Construct a new router.
pub const fn new() -> Self {
Expand Down
6 changes: 6 additions & 0 deletions service/src/middleware/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ use crate::service::Service;
/// ```
pub struct Group<S, E>(PhantomData<fn(S, E)>);

impl<S, E> Default for Group<S, E> {
fn default() -> Self {
unimplemented!("please use Group::new");
}
}

impl<S, E> Group<S, E> {
pub const fn new() -> Self {
Self(PhantomData)
Expand Down
6 changes: 6 additions & 0 deletions unsafe_collection/src/bound_queue/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub struct StackQueue<T, const N: usize> {
inner: Bounded<[MaybeUninit<T>; N]>,
}

impl<T, const N: usize> Default for StackQueue<T, N> {
fn default() -> Self {
unimplemented!("please use StackQueue::new");
}
}

impl<T, const N: usize> StackQueue<T, N> {
#[inline]
pub const fn new() -> Self {
Expand Down
6 changes: 6 additions & 0 deletions unsafe_collection/src/small_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ pub struct SmallBoxedStr {
inner: Inner,
}

impl Default for SmallBoxedStr {
fn default() -> Self {
unimplemented!("please use SmallBoxedStr::new")
}
}

impl SmallBoxedStr {
#[inline]
pub const fn new() -> Self {
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl<R, F> PathGen for App<R, F>
where
R: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.router.path_gen(prefix)
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
where
I: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<Obj> PathGen for AppRouter<Obj>
where
Router<Obj>: PathGen,
{
fn path_gen(&mut self, prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
self.0.path_gen(prefix)
}
}
Expand Down
3 changes: 2 additions & 1 deletion web/src/service/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl ServeDir {
}

impl PathGen for ServeDir {
fn path_gen(&mut self, mut prefix: String) -> String {
fn path_gen(&mut self, prefix: &str) -> String {
let mut prefix = String::from(prefix);
if prefix.ends_with('/') {
prefix.pop();
}
Expand Down
Loading