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

Low-hanging perf improvements in highlighting code #196

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/purple-ghosts-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'contexture-elasticsearch': patch
---

Low-hanging perf improvements in elasticsearch highlighting
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { groupStats } from './groupStatUtils.js'
// [1, 2, 3] -> [{to: 1}, {from: 1, to: 2}, {from: 2, to: 3}, {from: 3}]
let boundariesToRanges = _.flow(
F.mapIndexed((to, i, list) => F.compactObject({ from: list[i - 1], to })),
(arr) => F.push({ from: _.last(arr).to }, arr)
(arr) => F.pushOn(arr, { from: _.last(arr).to })
Copy link
Member

Choose a reason for hiding this comment

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

I think I actually prefer F.push here. Mixing non-mutating with mutating in the same flow is kind of awkward for me. If you want to mutate an array with push you should just loop over the array and do that.

)

let drilldownToRange = (drilldown) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,19 @@ let getFieldSubFields = (field) =>
/**
* Returns object of all subfields in a schema.
*/
let getSchemaSubFields = (schema) =>
F.reduceIndexed(
(acc, field, path) =>
F.mergeOn(
acc,
_.mapKeys((k) => `${path}.${k}`, getFieldSubFields(field))
),
{},
schema.fields
)
let getSchemaSubFields = _.memoize(
(schema) =>
F.reduceIndexed(
(acc, field, path) => {
let subFields = getFieldSubFields(field)
for (let k in subFields) acc[`${path}.${k}`] = subFields[k]
return acc
Copy link
Member

Choose a reason for hiding this comment

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

Mutating the accumulator and then returning it feels kind of silly to me. Why not just use a for loop. It's easy to read and clearly indicates the imperative approach you're taking here.

},
{},
schema.fields
),
_.get('elasticsearch.index')
)

/**
* Returns object of all group fields and their subfields in a schema.
Expand Down Expand Up @@ -148,7 +151,7 @@ export let getAllHighlightFields = _.memoize((schema) => {
let collectKeysAndValues = (f, coll) =>
F.reduceTree()(
(acc, val, key) =>
f(val) ? F.push(val, acc) : f(key) ? F.push(key, acc) : acc,
f(val) ? F.pushOn(acc, val) : f(key) ? F.pushOn(acc, key) : acc,
Copy link
Member

Choose a reason for hiding this comment

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

Same kind of thing here. Using reduce with a mutating call in the reducing function feels a little awkward to me. Why not just walk the tree and and push onto an array?

[],
coll
)
Expand Down
Loading