-
Notifications
You must be signed in to change notification settings - Fork 642
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
Conversation
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. |
formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/Helpers.kt
Show resolved
Hide resolved
if (typeId < 0 || typeId >= entryArray.size) { | ||
return INVALID | ||
} |
There was a problem hiding this comment.
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 INVALID
s) to avoid the if check (instead relying on the array check that does this too).
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great find!
It's definitely not, but it improves a performance a bit without large-scale updates. You know, ten bucks is ten bucks. ;) |
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.