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

Wrapping Jsoup on JVM to improve performance #79

Closed
Him188 opened this issue Sep 15, 2024 · 4 comments
Closed

Wrapping Jsoup on JVM to improve performance #79

Him188 opened this issue Sep 15, 2024 · 4 comments

Comments

@Him188
Copy link

Him188 commented Sep 15, 2024

Hi, as a follow up of #39, I'm considering if we can make Ksoup a wrapper of Jsoup on JVM.

Here are some reasons:

My app previously target desktop JVM and Android so it was using Jsoup. Ksoup allows us to support iOS without chaning code which very nice, however we can't accept any performance degredation on the JVM, because the parsing is in a critical user path such that performance directly affect user experience. Therefore, I can't switch the common code to Ksoup unless Ksoup perform the same well as Jsoup on JVM.

I finally ended up adopting some compiler magic that creating expect clas Element, Node, Document, etc. in the commonMain, actualizing it to Jsoup on the JVM, and actualizing to Ksoup on Native. Errors suppressed.

Relevant source code:

So, I'm considering if Ksoup can use Jsoup on the JVM, hence getting the best performance. This would help everyone who needs to concern about performance.

Technically Ksoup can't do this magic - it's too risky to do as a library. I assume it can be achieved by turning Ksoups's Element classes to become a delegate of Jsoup's corresponding part. The only overhead is the wrapper class - which is just one object can is acceptable.

But again, thats for your tarrific work of creating Ksoup. Looking forward to your replies!

@Him188 Him188 changed the title Forwarding requests to Jsoup directly on JVM Wrapping Jsoup on JVM to improve performance Sep 15, 2024
@Him188
Copy link
Author

Him188 commented Sep 15, 2024

There are some impliciations though - by having two different implementations, Ksoup might return different results on different targets.

Consider if Ksoup ports Jsoup 1.0 to Kotlin Native, and depends on Jsoup 1.0 on the JVM. In a future Jsoup 1.1 release it fixes a bug which led to wrong selector results. Now, if a user have Jsoup 1.1 dependency locally, which will override Ksoup's dependency, they will see inconsisitent results across targets.

But I'm personally happy with this inconsistency, because Jsoup is very stable and this is unlikely to happen.

@itboy87
Copy link
Collaborator

itboy87 commented Sep 16, 2024

@Him188 Thank you for your feedback! However, I currently have no plans to wrap Jsoup for the JVM, as it could lead to inconsistencies and increased complexity. At the moment, Jsoup is 2-3 times faster only for parsing, but query performance is the same. I have just completed the ksoup-lite variant, which parses directly from strings without any external dependencies (see #67). Next Once I complete #78, I will start working on performance improvements and aim to match Jsoup’s performance before releasing version 1.0.0.

@itboy87
Copy link
Collaborator

itboy87 commented Sep 16, 2024

Follow up on: #80

@itboy87 itboy87 closed this as completed Sep 16, 2024
@itboy87
Copy link
Collaborator

itboy87 commented Sep 21, 2024

@Him188 after #83 Ksoup have almost same Jsoup performance on Jvm.
Screenshot 2024-09-21 at 12 50 30 AM
Screenshot 2024-09-21 at 12 45 43 AM

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

No branches or pull requests

2 participants