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(ext/fetch): Use the WebIDL conversion to DOMString rather than USVString for Response constructor #12201

Merged
merged 2 commits into from
Sep 25, 2021
Merged

Conversation

lmalheiro
Copy link
Contributor

Fixes performance issue #12090

  • Replaces conversion to USVString by DOMString at the BodyInit WebIDL conversion.

  • Adds benchmark response_string which produced the following results before and after the change:

    • using USVString:
    Benchmark #13: /home/luismalheiro/projects/deno_pr/target/release/deno run cli/tests/testdata/response_string_perf.js 
      Time (mean ± σ):     432.9 ms ±  26.8 ms    [User: 308.8 ms, System: 192.8 ms]
      Range (min … max):   409.5 ms … 485.4 ms    10 runs
    
    • using DOMString:
    Benchmark #13: /home/luismalheiro/projects/deno_pr/target/release/deno run cli/tests/testdata/response_string_perf.js 
      Time (mean ± σ):     175.8 ms ±  17.6 ms    [User: 168.6 ms, System: 73.9 ms]
      Range (min … max):   147.3 ms … 211.6 ms    18 runs
    

    Notice that the difference will increase non-linearly for larger strings.

This pull request replaces #12093

@lucacasonato
Copy link
Member

This is not correct though. USVString normalization doesn't happen at all now.

@andreubotella
Copy link
Contributor

This is not correct though. USVString normalization doesn't happen at all now.

The BodyInit type is only used in the Response constructor and in RequestInit, which itself is only used in the Request constructor (and in the fetch function, which defers to the Request constructor). In both of these cases, the BodyInit parameter is passed to extractBody before doing anything else with it – and extractBody will UTF-8 encode strings with replacement, effectively performing the USV normalization.

@@ -393,7 +393,7 @@
return webidl.converters["ArrayBufferView"](V, opts);
}
}
return webidl.converters["USVString"](V, opts);
return webidl.converters["DOMString"](V, opts);
Copy link
Contributor

Choose a reason for hiding this comment

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

BodyInit is a WebIDL union type defined in the fetch specification. As I suggested in #12168 (comment), you should probably define a different WebIDL type (BodyInit_DOMString for example), to avoid confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreubotella, what would you suggest to do with the nullable converter BodyInit? ? Should I rename it too?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.

Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @lmalheiro and @andreubotella

@lucacasonato lucacasonato merged commit b095157 into denoland:main Sep 25, 2021
@lmalheiro lmalheiro deleted the fix_response_performance branch September 25, 2021 13:33
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.

3 participants