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

Some implementation details are dictated by Kotlin/Native shortcomings #5

Closed
2 tasks done
dkhalanskyjb opened this issue Apr 28, 2020 · 0 comments
Closed
2 tasks done

Comments

@dkhalanskyjb
Copy link
Collaborator

dkhalanskyjb commented Apr 28, 2020

  • In several places, x in A..B is replaced with the non-idiomatic x >= A && x <= B. This is due to the fact that Kotlin/Native does not yet optimize such constructs outside of for-loops. In particular, in LocalTime#ofSecondOfDay about half the time of the function execution was spent in argument validity checks, allocating several objects in the meantime. https://youtrack.jetbrains.com/issue/KT-38787
  • Parsers are not singleton objects but are instead functions that return newly-constructed parsers. This is done to sidestep a bug that led to an unchecked null value dereferencing as a result of wrong initialization order of values: parser A, referencing parser B, was being initialized before B, which led to it dereferencing a null value.
dkhalanskyjb added a commit that referenced this issue Apr 30, 2020
Testing revealed that accessing even immutable global variables
concurrently is not supported out-of-the-box and instead crashes
the Kotlin Native runtime. Thus, `SharedImmutable` annotation was
added to every global variable.

Performance fixes include:
* The system timezone is only queried once, after that, it's cached
  for the whole life of the thread; Java does something similar,
  as, according to
  https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#setDefault(java.util.TimeZone),
  the system timezone is only queried at the start of the VM, if
  ever.
* Instead of passing strings between Kotlin and native parts,
  numeric ids are used. To have a sensible interpretation of such
  ids that prohibited their invalidation, Apple and Windows use
  additional memory to store the mapping. Moreover, it is assumed
  for Apple that the set of timezones and their rules never change,
  and for *nix, it is assumed that the timezone objects are always
  at the same memory location, which is true for now but will
  change if the implementation switches to use C++20 capabilities.
* Most common `ZoneOffset` instances are now cached, which saves
  a lot of time due to not having to recompute their string
  representation, which is costly in aggregate due to how common
  getting anonymous offsets is for operations on datetimes.
* Comparators have been rewritten not to use `compareBy`, which is
  comparatively slow.
* `LocalDate` no longer uses `Month` as its main representation of
  months, as it is an enum, which are surprisingly slow.
* `LocalDate` used to calculate many additional fields which may
  never be required but were nonetheless computed for each instance
  of the class. Now, instantiating `LocalDate` is much cheaper at
  the cost of several additional fields being computed on each call.
* Some Kotlin-style range checks were replaced with the
  corresponding simple comparisons due to a missing optimization
  in Kotlin/Native. See
  #5

After these fixes, on my machine tests run in 22 seconds instead of
56.
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
@dkhalanskyjb and others