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

perf: optimization of request instantiation #3107

Merged
merged 17 commits into from
Jun 6, 2024

Conversation

tsctx
Copy link
Member

@tsctx tsctx commented Apr 13, 2024

  1. Reuse fetch optimization logic in core.
  2. Avoid lowercasing.

@codecov-commenter
Copy link

codecov-commenter commented Apr 13, 2024

Codecov Report

Attention: Patch coverage is 98.21429% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 94.16%. Comparing base (ad9b5bd) to head (4cf7be2).

Files Patch % Lines
lib/web/fetch/util.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3107      +/-   ##
==========================================
- Coverage   94.17%   94.16%   -0.02%     
==========================================
  Files          90       90              
  Lines       24320    24320              
==========================================
- Hits        22904    22901       -3     
- Misses       1416     1419       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Uzlopak Uzlopak changed the title perf: optimization of request creation perf: optimization of request instantiation Apr 13, 2024
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina requested a review from KhafraDev April 13, 2024 09:49
lib/core/request.js Outdated Show resolved Hide resolved
lib/core/util.js Outdated Show resolved Hide resolved
Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

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

Is there a reason to combine method validation and normalization into a single method? This instantly breaks the second we need one and not the other.

@tsctx
Copy link
Member Author

tsctx commented Apr 13, 2024

Is there a reason to combine method validation and normalization into a single method?

The reason for wanting to normalize and verify at the same time is that the case used in the verification can be reused.

This instantly breaks the second we need one and not the other.

Yes, it is. The function was not changed, but a new one was created.

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

Can you share the bench results?

@tsctx
Copy link
Member Author

tsctx commented Apr 14, 2024

Can you share the bench results?

import Request from "../../lib/core/request.js";
import DecoratorHandler from "../../lib/handler/decorator-handler.js";

import { bench, group, run } from 'mitata'

const handler = new DecoratorHandler({})

bench('new Request()', () => {
    return new Request('https://localhost', { path: "/", method: "get", body: null }, handler);
})

await run()
  • main
benchmark          time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------- -----------------------------
new Request()     114 ns/iter   (89.75 ns … 2'124 ns)    124 ns    256 ns  1'619 ns```
  • this patch
benchmark          time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------- -----------------------------
new Request()   90.41 ns/iter   (70.85 ns … 1'729 ns)    106 ns    205 ns    717 ns

@Uzlopak
Copy link
Contributor

Uzlopak commented Apr 14, 2024

Thank you for the benchmark. Can you add it to the benchmark folder please?

lib/web/fetch/request.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

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

validate and normalize are two different things, please split them as the spec states to do

@luanmuniz
Copy link

This is just a comment from someone who doesn't know how this repo works, so feel free to ignore it:

I see optimization, and I just want to say that I found Map() to be more performant than plain objects. It could be a slight performance improvement to have normalizedMethodRecordsBase as Map() instead of a plain object.

@mcollina mcollina merged commit 7cc2413 into nodejs:main Jun 6, 2024
30 checks passed
@tsctx tsctx deleted the perf/optimization-of-request-creation branch June 6, 2024 10:11
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.

None yet

8 participants