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: replace node object for node class #436

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

H4ad
Copy link
Contributor

@H4ad H4ad commented Jul 4, 2023

Calling Object.defineProperty is not expensive but it's not cheap too (reference).

So, to emulate the same behavior and have no penalty for the performance, we can just change it to a class.

@vercel
Copy link

vercel bot commented Jul 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orama-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 17, 2023 0:44am

@micheleriva
Copy link
Member

I fear this wouldn't be easily serializable to JSON to be loaded back in using the data-persistence plugin... have you tested this?

@H4ad
Copy link
Contributor Author

H4ad commented Jul 6, 2023

I didn't test the serialization, but you are right, I will add a specific case to handle the load method for these nodes.

@H4ad
Copy link
Contributor Author

H4ad commented Jul 6, 2023

@micheleriva Actually, this is not a bug? Because when we load it back, and then serialize it again, how do you set serialize for those nodes that were serialized before?

@H4ad
Copy link
Contributor Author

H4ad commented Jul 17, 2023

I fixed the problem with the serialization, I didn't like the recursion, but at least it's not throwing an error with 1M items.

Copy link
Member

@micheleriva micheleriva left a comment

Choose a reason for hiding this comment

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

Two minor things, but LGTM. @allevo would appreciate your feedback before merging

convertedNode.docs = node.docs;
convertedNode.word = node.word;

for (const childrenKey of Object.keys(node.children)) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not const childrenKey in node.children? AFAIK that should be a bit faster

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know exactly if is slower or faster.

For this benchmark, for in is faster.
For this other, it is slower.

The only thing I know is for in usually causes deoptmizations on v8, refs:

Copy link
Member

Choose a reason for hiding this comment

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

In your benchmark:

for (var i=10000; i > 0; i--) {
  Object.keys(obj).forEach(key => console.log(key));
}

there's also an extra function call as a callback for the forEach method. This adds slight overhead, so I don't think the benchmark itself is reliable enough.

https://github.com/zhangchiqing/OptimizationKillers/blob/master/README.md
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#5-for-in

These links are interesting and I'd rather follow them, lgtm

@@ -502,6 +515,20 @@ export async function load<R = unknown>(raw: R): Promise<Index> {
fieldLengths,
} = raw as Index

const indexes: Index['indexes'] = {};

for (const prop of Object.keys(rawIndexes)) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, maybe a for..in loop here would help performances

Copy link
Collaborator

@allevo allevo left a comment

Choose a reason for hiding this comment

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

The performance remains almost the same, but the code will be clearer.
Could you address Michele's suggestions?

LGTM.

@micheleriva micheleriva merged commit da98a30 into oramasearch:main Jul 17, 2023
@H4ad H4ad deleted the perf/create-node-radix branch July 17, 2023 16:01
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