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

Pg Client: improve Interval support #1462

Merged
merged 5 commits into from
Sep 16, 2024

Conversation

tsegismont
Copy link
Contributor

@tsegismont tsegismont commented Sep 6, 2024

Faster and garbage-free codec, compatibility with java.time.Duration

Closes #1281

Commits can be reviewed individually.

This a preliminary commit related to issue eclipse-vertx#1281

Encoding and decoding an Interval implied creating many objects (Duration and Period are immutable).
Besides, the Duration.plusXXX and Period.normalized methods perform several computations.

With the proposed change:

- the codec doesn't create redundant objects
- a decoded interval is always normalized (i.e. the number of months is strictly smaller than 12, the number of days is strictly smaller than 30,... etc).

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont
Copy link
Contributor Author

Not ready yet, do not merge

The test compares the previous and the proposed solutions.

It confirms the assumptions about the proposed solution:

- about twice as fast
- removes pressure on GC

# JMH version: 1.19
# VM version: JDK 1.8.0_422, VM 25.422-b05
# VM options: -Xms8g -Xmx8g -Xmn7g
# Warmup: 20 iterations, 1 s each
# Measurement: 10 iterations, 2 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time

Benchmark                                                                         Mode  Cnt      Score     Error   Units
IntervalBenchmarks.encodeWithDurationAndPeriod                                   thrpt   30  46339.530 ± 389.115  ops/ms
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.alloc.rate                    thrpt   30   3393.324 ±  28.481  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.alloc.rate.norm               thrpt   30     96.000 ±   0.001    B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Eden_Space           thrpt   30   3436.887 ± 778.345  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Eden_Space.norm      thrpt   30     97.302 ±  22.205    B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Survivor_Space       thrpt   30      0.005 ±   0.006  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Survivor_Space.norm  thrpt   30     ≈ 10⁻⁴              B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.count                         thrpt   30     36.000            counts
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.time                          thrpt   30     30.000                ms
IntervalBenchmarks.encodeWithParts                                               thrpt   30  82322.542 ± 109.306  ops/ms
IntervalBenchmarks.encodeWithParts:·gc.alloc.rate                                thrpt   30     ≈ 10⁻⁴            MB/sec
IntervalBenchmarks.encodeWithParts:·gc.alloc.rate.norm                           thrpt   30     ≈ 10⁻⁶              B/op
IntervalBenchmarks.encodeWithParts:·gc.count                                     thrpt   30        ≈ 0            counts

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont tsegismont added this to the 4.5.11 milestone Sep 9, 2024
@tsegismont tsegismont changed the title Pg Client: conversion of Duration to/from Interval Pg Client: improve Interval support Sep 9, 2024
The conversion algorithm assumes a year last 12 months and a month lasts 30 days, as Postgres does and ISO 8601 suggests.

See https://github.com/postgres/postgres/blob/5bbdfa8a18dc56d3e64aa723a68e02e897cb5ec3/src/include/datatype/timestamp.h#L116

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont
Copy link
Contributor Author

This is ready for review, PTAL @vietj

@tsegismont tsegismont merged commit 7d71038 into eclipse-vertx:4.x Sep 16, 2024
10 checks passed
tsegismont added a commit to tsegismont/vertx-sql-client that referenced this pull request Sep 16, 2024
* Refactor Pg Interval codec

This a preliminary commit related to issue eclipse-vertx#1281

Encoding and decoding an Interval implied creating many objects (Duration and Period are immutable).
Besides, the Duration.plusXXX and Period.normalized methods perform several computations.

With the proposed change:

- the codec doesn't create redundant objects
- a decoded interval is always normalized (i.e. the number of months is strictly smaller than 12, the number of days is strictly smaller than 30,... etc).

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>

* Added a JMH test for Interval encoding

The test compares the previous and the proposed solutions.

It confirms the assumptions about the proposed solution:

- about twice as fast
- removes pressure on GC

# JMH version: 1.19
# VM version: JDK 1.8.0_422, VM 25.422-b05
# VM options: -Xms8g -Xmx8g -Xmn7g
# Warmup: 20 iterations, 1 s each
# Measurement: 10 iterations, 2 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time

Benchmark                                                                         Mode  Cnt      Score     Error   Units
IntervalBenchmarks.encodeWithDurationAndPeriod                                   thrpt   30  46339.530 ± 389.115  ops/ms
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.alloc.rate                    thrpt   30   3393.324 ±  28.481  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.alloc.rate.norm               thrpt   30     96.000 ±   0.001    B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Eden_Space           thrpt   30   3436.887 ± 778.345  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Eden_Space.norm      thrpt   30     97.302 ±  22.205    B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Survivor_Space       thrpt   30      0.005 ±   0.006  MB/sec
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.churn.PS_Survivor_Space.norm  thrpt   30     ≈ 10⁻⁴              B/op
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.count                         thrpt   30     36.000            counts
IntervalBenchmarks.encodeWithDurationAndPeriod:·gc.time                          thrpt   30     30.000                ms
IntervalBenchmarks.encodeWithParts                                               thrpt   30  82322.542 ± 109.306  ops/ms
IntervalBenchmarks.encodeWithParts:·gc.alloc.rate                                thrpt   30     ≈ 10⁻⁴            MB/sec
IntervalBenchmarks.encodeWithParts:·gc.alloc.rate.norm                           thrpt   30     ≈ 10⁻⁶              B/op
IntervalBenchmarks.encodeWithParts:·gc.count                                     thrpt   30        ≈ 0            counts

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>

* Interval conversion from/to Duration

The conversion algorithm assumes a year last 12 months and a month lasts 30 days, as Postgres does and ISO 8601 suggests.

See https://github.com/postgres/postgres/blob/5bbdfa8a18dc56d3e64aa723a68e02e897cb5ec3/src/include/datatype/timestamp.h#L116

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>

* Update vertx-pg-client/src/main/java/io/vertx/pgclient/data/Interval.java

* Update vertx-pg-client/src/main/java/io/vertx/pgclient/data/Interval.java

---------

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont tsegismont deleted the issue/1281 branch September 17, 2024 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant