Skip to content

Speedup ProtoWriteType.from #2879

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

Merged
merged 3 commits into from
Dec 13, 2024
Merged

Speedup ProtoWriteType.from #2879

merged 3 commits into from
Dec 13, 2024

Conversation

fzhinkin
Copy link
Contributor

@fzhinkin fzhinkin commented Dec 11, 2024

While poking into #2332 I spotted that ProtoWriteType.from implementation is pretty inefficient.

There are a few ways to transform integer into a corresponding ProtoWriteType.
Corresponding benchmarks are here: https://github.com/fzhinkin/kx-serialization-parse-proto-type/blob/main/src/commonMain/kotlin/ParseProtoTypeBenchmark.kt

On JVM (see https://jmh.morethan.io/?source=https://gist.githubusercontent.com/fzhinkin/c8b3335e6ba79ec06e9acffc85230e28/raw/e420db8bbb80cf03f772862eb6a58cd7509cf617/parsing-jvm.json), both 8-aligned array-based approaches works fine.
On Native (see https://jmh.morethan.io/?source=https://gist.githubusercontent.com/fzhinkin/c8b3335e6ba79ec06e9acffc85230e28/raw/e420db8bbb80cf03f772862eb6a58cd7509cf617/parsing-macos-arm64.json), moving masking into a function works a bit faster.

Here's comparison of results proto-related benchmarks collected with existing implementation (baseline) and the implementation from this PR (optimized): https://jmh.morethan.io/?sources=https://gist.githubusercontent.com/fzhinkin/c8b3335e6ba79ec06e9acffc85230e28/raw/e420db8bbb80cf03f772862eb6a58cd7509cf617/baseline.json,https://gist.githubusercontent.com/fzhinkin/c8b3335e6ba79ec06e9acffc85230e28/raw/e420db8bbb80cf03f772862eb6a58cd7509cf617/optimized.json

Suspiciously looking ProtoBaseline.toBytesExplicit shows bimodal results, thus the results.
For parsing related benchmarks, the proposed change slightly improves performance.

@fzhinkin
Copy link
Contributor Author

Additionally, checked how replacing the enum with raw int values will affect the performance - the difference is negligibly small and does not justify such a change.

@fzhinkin fzhinkin marked this pull request as ready for review December 11, 2024 02:45
Comment on lines 30 to 32
if (typeId < 0 || typeId >= entryArray.size) {
return INVALID
}
Copy link
Contributor

Choose a reason for hiding this comment

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

From an optimization perspective it may be valid to not check for typeId<0 (and maybe have a larger array of INVALIDs) to avoid the if check (instead relying on the array check that does this too).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The array bounds check will "give" us ArrayIndexOutOfBounds, not the ProtoWireType.INVALID :)

Anyway, the implementation was implicitly based on the idea that the only call site extracts 3 least-significant bits from an int before passing it down this methods, so when this method is inlined the compiler can get rid of the if.

Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

Great find!

@sandwwraith
Copy link
Member

Although this change was added in #2768 in 2024, before that, there were direct Int constants. So it is not the possible cause of slowness reported in #2332

@fzhinkin
Copy link
Contributor Author

So it is not the possible cause of slowness reported in #2332

It's definitely not, but it improves a performance a bit without large-scale updates. You know, ten bucks is ten bucks. ;)

@fzhinkin fzhinkin merged commit d62f3b8 into dev Dec 13, 2024
4 checks passed
@fzhinkin fzhinkin deleted the speedup-proto-wire-type-parsing branch December 13, 2024 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants