Skip to content

Refactoring (v3) - #96

Merged
Uzlopak merged 17 commits into
masterfrom
major
Dec 12, 2023
Merged

Refactoring (v3)#96
Uzlopak merged 17 commits into
masterfrom
major

Conversation

@Eomm

@Eomm Eomm commented Dec 9, 2023

Copy link
Copy Markdown
Member

fixes #95, #88

New Bench:

➜  process-warning git:(major) ✗ node benchmarks/warn.js
warn x 156,880,997 ops/sec ±0.26% (100 runs sampled)
(node:5524) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5524) [FST_ERROR_CODE_2] FastifyWarning: message

Master branch:

➜  process-warning git:(master) ✗ node benchmarks/warn.js
warn x 62,825,315 ops/sec ±0.21% (100 runs sampled)
(node:5718) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5718) [FST_ERROR_CODE_3] FastifyWarning: message

TODO

  • docs
  • jsdocs
  • typescript

@Uzlopak

Uzlopak commented Dec 9, 2023

Copy link
Copy Markdown
Contributor

@Eomm
How about not using .emit() method at all but directly return the function?

const { createWarning } = require('..')

const CUSTDEP001 = createWarning('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning')

CUSTDEP001()

?

@Eomm

Eomm commented Dec 9, 2023

Copy link
Copy Markdown
Member Author

And how do you check if it has been emitted?

@Uzlopak

Uzlopak commented Dec 9, 2023

Copy link
Copy Markdown
Contributor

CUSTDEP001.emitted ?

@mcollina

mcollina commented Dec 9, 2023

Copy link
Copy Markdown
Member

I don't think switching to a class is needed, using a closure or function would be sufficient and avoid less changes.

@Eomm

Eomm commented Dec 9, 2023

Copy link
Copy Markdown
Member Author

I don't think switching to a class is needed, using a closure or function would be sufficient and avoid less changes.

Did a quick test without class, and the performance was worse.
Here is the code and the results:

NO CLASS:
➜  process-warning git:(major) ✗ node benchmarks/warn.js
warn x 65,740,447 ops/sec ±0.13% (101 runs sampled)
(node:7193) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7193) [FST_ERROR_CODE_2] FastifyWarning: message

class:
➜  process-warning git:(major) ✗ node benchmarks/warn.js
warn x 152,302,444 ops/sec ±1.18% (97 runs sampled)
(node:8600) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:8600) [FST_ERROR_CODE_2] FastifyWarning: message

CUSTDEP001.emitted ?

TBH I don't like too much props attached to a function. I can't see any advantages, plus I expect the typings are more hard to write

@jsumners

jsumners commented Dec 9, 2023

Copy link
Copy Markdown
Member

TBH I don't like too much props attached to a function.

I find this odd when considering how JavaScript works. A function is an object like any other.

@Eomm

Eomm commented Dec 9, 2023

Copy link
Copy Markdown
Member Author

TBH I don't like too much props attached to a function.

I find this odd when considering how JavaScript works. A function is an object like any other.

Yeah, I like it too:
image

in this case, where we don't have constraints, and we are defining a new API, so I think that having a more expressive warninstance.emit() function is more evident than warn('foo', 'var') because it tells me what is happening and anyone that use it in whatever repository, can search for emit() to find where a warning is emitted.

Comment thread index.js Outdated

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@Eomm

Eomm commented Dec 10, 2023

Copy link
Copy Markdown
Member Author

Need help for TS 😅

@Uzlopak

Uzlopak commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

Ok, will do now the TS part

@Uzlopak

Uzlopak commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

@Eomm

Fixed typings

@Eomm

Eomm commented Dec 10, 2023

Copy link
Copy Markdown
Member Author

Good to go or strong against this API?

Comment thread README.md Outdated
@Eomm
Eomm marked this pull request as ready for review December 10, 2023 13:59
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
@Uzlopak

This comment was marked as outdated.

@Uzlopak Uzlopak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread README.md Outdated
@Uzlopak

Uzlopak commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

Imho object param for constructor/factry is preferable

I am inconclusive regarding the payload for instanciating. Could mean that we need to create unnecessary intermediary arrays.

@mcollina

Copy link
Copy Markdown
Member

I would generically prefer a functional pattern instead of a classical one:

const emit = createWarning(..)

emit(...)

if (emit.emitted) { ... }

This is just personal preference.

@Uzlopak

Uzlopak commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

Me too, but There is a performance bottleneck to implement it as functional, because the function name has to be set via Object.defineProperty. So we get from 220 mio ops/s and get 86 mio ops/s on my machine.

@jsumners

Copy link
Copy Markdown
Member

I would generically prefer a functional pattern instead of a classical one:

const emit = createWarning(..)

emit(...)

if (emit.emitted) { ... }

This is just personal preference.

This is what the PR implements, correct?

Me too, but There is a performance bottleneck to implement it as functional, because the function name has to be set via Object.defineProperty. So we get from 220 mio ops/s and get 86 mio ops/s on my machine.

I don't follow. What has to be defined?

Personally, I don't think there is any need to sacrifice good api/developer experience in the name of "performance" here. These entities are going to be created at application initialization more often than not, and once created will perform as well as any other event emitter.

@Uzlopak

Uzlopak commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

See here:
#97

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
@Eomm

Eomm commented Dec 12, 2023

Copy link
Copy Markdown
Member Author

This is what the PR implements, correct?

A bit different, there is the emit method to call:

const warning = createWarning('FastifyWarning', 'FSTWRN001', 'Hello %s', { unlimited: true })
warning.emit('world')

if (warning.emitted) { ... }

Fixing the input object

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@Uzlopak

Uzlopak commented Dec 12, 2023

Copy link
Copy Markdown
Contributor

@Eomm
@mcollina
Should I invest more time on #97
I guess i coule fix the perf issue. But if you say this PR solves the issue sufficiently, then I am ok with it.

@Uzlopak

Uzlopak commented Dec 12, 2023

Copy link
Copy Markdown
Contributor

I could remove the bottleneck in #97

Comment thread README.md Outdated
Comment thread README.md
Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread README.md Outdated
Comment thread test/index.test.js
Comment thread test/index.test.js
Comment thread test/index.test.js
Comment thread test/issue-88.test.js
Comment thread test/jest.test.js Outdated
Comment thread index.js Outdated
Comment thread index.js Outdated
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
Comment thread README.md Outdated
Comment thread index.js Outdated
@Uzlopak

Uzlopak commented Dec 12, 2023

Copy link
Copy Markdown
Contributor

I think the last open discussion point is if unlimited should be true by default or not.

I have no strong opinion on this.

@Uzlopak

Uzlopak commented Dec 12, 2023

Copy link
Copy Markdown
Contributor

@jsumners
Pardon me.

I closed some of your remarks regarding the FST_ prefix in the Readme.md, because I thought they were addressed already as they were marked as Outdated.
Now i checked again and saw that some changes were not applied.

@Eomm
Can you please take care of them?

@jsumners

Copy link
Copy Markdown
Member

I'll do a pass on the docs and examples after this is merged. None of my feedback has been blocking.

@Uzlopak Uzlopak changed the title Major Refactoring (v3) Dec 12, 2023
@Uzlopak
Uzlopak merged commit 0378576 into master Dec 12, 2023
@Uzlopak
Uzlopak deleted the major branch December 12, 2023 19:54
@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reimplementation

4 participants