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

chore(performance#38): Performance improvements #1871

Merged
merged 2 commits into from
Jan 20, 2023
Merged
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
21 changes: 12 additions & 9 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const { isValidHTTPToken, isomorphicDecode } = require('./util')

const encoder = new TextEncoder()

// Regex
const HTTP_TOKEN_CODEPOINTS = /^[!#$%&'*+-.^_|~A-z0-9]+$/
const HTTP_WHITESPACE_REGEX = /(\u000A|\u000D|\u0009|\u0020)/ // eslint-disable-line
// https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
const HTTP_QUOTED_STRING_TOKENS = /^(\u0009|\x{0020}-\x{007E}|\x{0080}-\x{00FF})+$/ // eslint-disable-line

// https://fetch.spec.whatwg.org/#data-url-processor
/** @param {URL} dataURL */
function dataURLProcessor (dataURL) {
Expand Down Expand Up @@ -217,7 +223,7 @@ function parseMIMEType (input) {
// 4. If type is the empty string or does not solely
// contain HTTP token code points, then return failure.
// https://mimesniff.spec.whatwg.org/#http-token-code-point
if (type.length === 0 || !/^[!#$%&'*+-.^_|~A-z0-9]+$/.test(type)) {
if (type.length === 0 || !HTTP_TOKEN_CODEPOINTS.test(type)) {
return 'failure'
}

Expand All @@ -244,7 +250,7 @@ function parseMIMEType (input) {

// 9. If subtype is the empty string or does not solely
// contain HTTP token code points, then return failure.
if (subtype.length === 0 || !/^[!#$%&'*+-.^_|~A-z0-9]+$/.test(subtype)) {
if (subtype.length === 0 || !HTTP_TOKEN_CODEPOINTS.test(subtype)) {
return 'failure'
}

Expand All @@ -258,9 +264,7 @@ function parseMIMEType (input) {
/** @type {Map<string, string>} */
parameters: new Map(),
// https://mimesniff.spec.whatwg.org/#mime-type-essence
get essence () {
return `${this.type}/${this.subtype}`
}
essence: `${type}/${subtype}`
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the main driver for the improvements. Tried to find something in the spec that indicates this is a getter but sadly couldn't find any point indicating it. I might overlook it, but @KhafraDev can you confirm this approach is ok?

Copy link
Member

@ronag ronag Jan 20, 2023

Choose a reason for hiding this comment

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

This won't work as it can be overwritten... it must be a getter

Copy link
Member Author

Choose a reason for hiding this comment

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

That was what I was missing then. Does that mean that the mime type object can be altered after creation, right?

This comment was marked as outdated.

Copy link
Member

Choose a reason for hiding this comment

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

Actually there is more stuff here than should get getters?

Copy link
Member

Choose a reason for hiding this comment

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

I might be wrong, but then it becomes a case of consistency, if you change subtype then essence need to be automatically updated, which is why a getter is necessary either way.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually there is more stuff here than should get getters?

You mean the PR itself, then yes? Otherwise, I might be missing something

I might be wrong, but then it becomes a case of consistency, if you change subtype then essence need to be automatically updated, which is why a getter is necessary either way

Yeah, once you said that about that must be a getter, that is what I started to see, as most likely the issue is not on the getter itself but the recomputation of the string on every get call.

Copy link
Member Author

@metcoder95 metcoder95 Jan 20, 2023

Choose a reason for hiding this comment

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

It seems is actually more about the string computation, as if I replace it with the following class that computes the essence on instantiation, and on set/get (kinda caching), the results remains stable.

Class:

class MimeType {
  #type = ''
  #subtype = ''
  #essence = ''
  #parameters = new Map()

  constructor ({ type, subtype }) {
    this.#type = type
    this.#subtype = subtype
    this.#essence = `${type}/${subtype}`
  }

  set type (value) {
    this.#type = value
    this.#essence = `${type}/${subtype}`
  }

  get type () {
    this.#type
  }

  set subtype (value) {
    this.#subtype = value
    this.#essence = `${type}/${subtype}`
  }

  get subtype () {
    return this.#subtype
  }

  get essence () {
    return this.#essence
  }

  get parameters () {
    return this.#parameters
  }
}

Scenario

const str = 'application/json; charset=utf-8'

suite
  .add('util#MIMEType', function () {
    new util.MIMEType(str)
  })
  .add('undici#parseMIMEType', function () {
    parseMIMEType(str).essence
  })
  .add('undici#parseMIMEType(original)', function () {
    parseMIMETypeOriginal(str).essence
  })
  .add('fast-content-type-parse#parse', function () {
    fastContentType.parse(str)
  })
  .add('fast-content-type-parse#safeParse', function () {
    fastContentType.safeParse(str)
  })
  .on('cycle', function (event) {
    console.log(String(event.target))
  })
  .on('complete', function () {
    console.log('Fastest is ' + this.filter('fastest').map('name'))
  })
  .run({ async: true })

Copy link
Member Author

Choose a reason for hiding this comment

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

Results:

util#MIMEType x 1,309,932 ops/sec ±0.48% (94 runs sampled)
undici#parseMIMEType x 2,331,911 ops/sec ±0.32% (96 runs sampled)
undici#parseMIMEType(original) x 1,501,260 ops/sec ±0.50% (99 runs sampled)

}

// 11. While position is not past the end of input:
Expand All @@ -272,7 +276,7 @@ function parseMIMEType (input) {
// whitespace from input given position.
collectASequenceOfCodePoints(
// https://fetch.spec.whatwg.org/#http-whitespace
(char) => /(\u000A|\u000D|\u0009|\u0020)/.test(char), // eslint-disable-line
char => HTTP_WHITESPACE_REGEX.test(char),
input,
position
)
Expand Down Expand Up @@ -355,9 +359,8 @@ function parseMIMEType (input) {
// then set mimeType’s parameters[parameterName] to parameterValue.
if (
parameterName.length !== 0 &&
/^[!#$%&'*+-.^_|~A-z0-9]+$/.test(parameterName) &&
// https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
!/^(\u0009|\x{0020}-\x{007E}|\x{0080}-\x{00FF})+$/.test(parameterValue) && // eslint-disable-line
HTTP_TOKEN_CODEPOINTS.test(parameterName) &&
!HTTP_QUOTED_STRING_TOKENS.test(parameterValue) &&
!mimeType.parameters.has(parameterName)
) {
mimeType.parameters.set(parameterName, parameterValue)
Expand Down