Repository navigation
perf(core): avoid Calendar allocations in the device date provider - #895
Merged
Merged
Conversation
Contributor
|
Contributor
posthog-android Compliance ReportDate: 2026-10-27 07:59:32 UTC ✅ All Tests Passed!46/46 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
marandaneto
marked this pull request as ready for review
October 27, 2026 08:14
dustinbyrne
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
PostHogDeviceDateProvidercreates aCalendarjust to return an epoch-millisecondDateor add a number of seconds. Constructing the calendar allocates timezone and calendar state that these operations do not need.Use
Date()for the current timestamp andDate(System.currentTimeMillis() + seconds * 1000L)for an offset. TheLongmultiplication preserves the fullIntrange. Both methods keep the same device-wall-clock behavior, including across timezone and daylight-saving changes. There is no public API change.This benefits the core JVM SDK and the default Android date provider through Android 12L / API 32. Android 13+ already uses direct
Dateconstruction in its network-clock provider, which is unchanged.💚 How did you test it?
Benchmarks
Measured with a temporary non-debuggable release harness on an Android 15 ARM64 emulator, with ART compiled in speed mode. Each comparison used 40 measurements per variant in eight interleaved blocks, with two excluded warmups per block. Measurements were taken before the latest
mainmerge, against base129a83a1, with only the date-provider implementation changed.The timestamp benchmark warmed up 10,000 pairs of calls, then measured 100,000 calls each to
currentDate()andaddSecondsToCurrentDate(30). Returned dates escape through a volatile sink.A separate SDK-setup comparison explicitly selected this device-clock provider through a wrapper, preventing Android 13+ from replacing it with the network-clock provider. Its median was 19.67 ms before and 17.74 ms after. This result is directional only: the 95% block-bootstrap time-difference interval was -6.03 to +0.57 ms, which includes zero. This was not a physical Android 12L device test, and no statistically proven overall startup improvement or default Android 13+ startup improvement is claimed.
The retained change is supported by the clear timestamp-operation improvement, not by the noisy setup result. Other timer experiments were discarded because they did not show a reliable overall benefit.
Correctness and validation
make testJavaon JDK 17: 1,073 tests passed after merging the latestmain.PostHogAndroidTest,PostHogAndroidDateProviderTest,PostHogAppInstallIntegrationTest, andPostHogLifecycleObserverIntegrationTest: 57 tests passed.make checkFormatandgit diff --checkpassed.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file.🤖 Agent context
Autonomy: Human-driven (agent-assisted).
Implemented with Pi using
gpt-6.1-sol, Git, Gradle, Python, the changesets CLI, and GitHub CLI. Local session ID:01a17727-1f78-754a-81a5-2b193f277c40; no public session link is available.Kept the change limited to the date provider, its compatibility tests, and patch changesets for the affected packages. The temporary benchmark harness and investigation notes are not included. The test-authoring audit verified the wall-clock and overflow assertions with controlled mutations. An isolated autoreview of commit
f841da0a5a376d3a397b786af70a54f017983a1aagainstorigin/mainreported no actionable findings. Human review is required before merging.