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

Backport QueryBuilder performance patches #5

Merged

Conversation

frangz
Copy link
Collaborator

@frangz frangz commented Nov 14, 2022

Description of change

This cherry-picks @draaglom's QueryBuilder performance patches from the main repo into our fork.

Pull-Request Checklist

Original changes:

typeorm#8955
typeorm#8956
typeorm#9032

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

frangz and others added 6 commits November 10, 2022 17:08
Motivation: the query builder (and within it, replacePropertyNames and
associated functions) is pretty CPU intensive. For our workload, it's
one of the hottest functions in our entire stack.

While improved in typeorm#4760,
There are still outstanding issues relating to perf e.g. typeorm#3857

As we all know though, the first step in optimization is to measure
systematically ;)

https://wiki.c2.com/?ProfileBeforeOptimizing

On my machine, this benchmark runs in ~3500ms or about 0.35ms/query.
This tells us there's a way to go - on my stack, that's about 1/3 of a
typical query's latency!
Context: the query builder is pretty CPU intensive, and can be slow -
e.g. typeorm#3857

One of the things which makes this slow is `escapeRegExp` in the query
builder: we freshly construct the same RegExp once per
`replacePropertyName` invocation (many times per overall query!) and
since the RegExp itself is constant -- we can lift it out and construct
it once.

Over-all this saves about 8% on our query build times as measured by
 typeorm#8955.
Digging further into typeorm#3857.

See also typeorm#8955, typeorm#8956.

As [previously
discussed](typeorm#3857 (comment)),
the query builder currently suffers from poor performance in two ways:
quadratic numbers of operations with respect to total table/column
counts, and poor constant factor performance (regexps can be expensive
to build/run!)

The constant-factor performance is the more tractable problem: no longer
quadratically looping would be a chunky rewrite of the query builder,
but we can locally refactor to be a bunch cheaper in terms of regexp
operations.

This change cuts the benchmark time here in ~half (yay!).

We achieve this by simplifying the overall replacement regexp (we don't
need our column names in there, since we already have a plain object
where they're the keys to match against) so compilation of that is much
cheaper, plus skipping the need to `escapeRegExp` every column as a
result.
@@ -5,6 +5,9 @@ How to make changes to our fork:
- use the `loyaltylion/changes` branch
- while you're making changes, consider rebasing it on the latest typeorm master
- make your changes -- keep everything organised with good, isolated commits in case we want to cherry pick some of these commits to get them upstream later
- install dependencies
- some dependencies will not install when running on ARM, so we run it on a linux/amd64 container instead
- run `docker run -it --platform linux/amd64 -v ${PWD}:/app -w /app node:12 npm install`

Choose a reason for hiding this comment

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

hero

@frangz frangz merged commit afb5e11 into loyaltylion/changes Nov 14, 2022
@frangz frangz deleted the loyaltylion/backport-querybuilder-perf-patches branch November 14, 2022 15:40
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.

2 participants