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

Embed the public suffix database list directly inside a class #8589

Merged
merged 12 commits into from
Nov 27, 2024

Conversation

yschimke
Copy link
Collaborator

@yschimke yschimke commented Nov 24, 2024

Attempt a fix for #8580

Removes the need for proguard for the database. At the cost of more costly code generation at build time.

Point at which we parse the file

java.lang.Exception
	at okhttp3.internal.publicsuffix.EmbeddedPublicSuffixList.<clinit>(EmbeddedPublicSuffixList.kt:35)
	at okhttp3.internal.publicsuffix.PublicSuffixDatabase.<clinit>(PublicSuffixDatabase.kt:157)
	at okhttp3.Cookie$Companion.parse$okhttp(Cookie.kt:561)
	at okhttp3.Cookie$Companion.parse(Cookie.kt:450)
	at okhttp3.CookiesTest.acceptOriginalServerMatchesSubdomain(CookiesTest.kt:306)

Generated source looks like, and both the source and class file are 57KB.

  override val bytes: ByteString = "Y2l0eS5rYXdhc2FraS5qcApjaXR5LmtpdGFreXVzaHUuanAKY2l0eS5rb2JlLmpwCmNpdHkubmFnb3lhLmpwCmNpdHkuc2FwcG9yby5qcApjaXR5LnNlbmRhaS5qcApjaXR5Lnlva29oYW1hLmpwCnd3dy5jawo=".decodeBase64()!!

@yschimke yschimke requested a review from JakeWharton November 24, 2024 11:26
okhttp/build.gradle.kts Outdated Show resolved Hide resolved
@yschimke yschimke marked this pull request as ready for review November 24, 2024 11:31
@yschimke
Copy link
Collaborator Author

@yschimke
Copy link
Collaborator Author

I shouldn't have uncompressed it. Will compare the compressed version

Copy link
Collaborator

@JakeWharton JakeWharton left a comment

Choose a reason for hiding this comment

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

I'll review the rest on desktop. Too dense for mobile.

okhttp/build.gradle.kts Show resolved Hide resolved
@yschimke
Copy link
Collaborator Author

One alternative is to leave the existing implementation in src/main as the default, until we get a version that we are happy with and then switch.

@@ -338,7 +239,7 @@ class PublicSuffixDatabase internal constructor(
low = mid + end + 1
} else {
// Found a match.
match = String(this, mid, publicSuffixLength)
match = this.substring(mid, mid + publicSuffixLength).string(Charsets.UTF_8)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like string should have an overload which takes start/end indices to avoid this double copy. Or we should have added ByteString.slice!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will raise a PR.

override fun ensureLoaded() {
}

val compressedBytes: ByteString = $publicSuffixListBytes
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about a nullable var and setting to null once read?


override val exceptionBytes: ByteString

init {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not done in ensureLoaded?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I figured the JVM classloader would be a nice simple mutex. Ther s so much locking code in the other impl

@yschimke yschimke merged commit 54b64fe into square:master Nov 27, 2024
20 checks passed
Copy link
Collaborator

@swankjesse swankjesse left a comment

Choose a reason for hiding this comment

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

I particularly don’t like this. We shouldn’t need to degrade good user’s runtime performance and artifact sizes because bad users are clumsy and trip on every rake.

@swankjesse
Copy link
Collaborator

Is there a way to load resources on Android that isn’t horrible for performance?!

@yschimke
Copy link
Collaborator Author

I can benchmark

  • resources
  • assets
  • embedded

Although my money is on this being better for performance than resources, just maybe at a small runtime memory cost.

@swankjesse
Copy link
Collaborator

Can we do assets on Android and resources on the JVM? OkHttp 5 can be multiplatform with just Android and JVM platforms, right?

@yschimke
Copy link
Collaborator Author

This went a bit pear shaped...

#8599

But exploring the space :)

@yschimke
Copy link
Collaborator Author

I'll check the multiplatform build option next.

@yschimke
Copy link
Collaborator Author

yschimke commented Dec 1, 2024

@yschimke
Copy link
Collaborator Author

yschimke commented Dec 1, 2024

Made an attempt #8600

We would need that other PR above in order to get a Context to load from assets. So that other change is worth benchmarking first.

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