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

8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX #8376

Conversation

turbanoff
Copy link
Member

@turbanoff turbanoff commented Apr 23, 2022

TimeUnit.toMillis/toNanos/toMicros/toSeconds is shorter and a bit faster.
Compared via JMH benchmark: 150ns -> 125ns/op

Benchamark adapted from http://cr.openjdk.java.net/~shade/8152083/TimeUnitBench.java
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class TimeUnitBench {

    static final int SIZE = TimeUnit.values().length * 10;

    @Param({"0", "1", "2", "3", "4", "5", "6", "-1"})
    int bias;

    TimeUnit[] mod;

    @Setup
    public void setup() {
        mod = new TimeUnit[SIZE];

        TimeUnit[] vals = TimeUnit.values();
        for (int c = 0; c < SIZE; c++) {
            if (bias == -1) {
                // megamorphic
                mod[c] = vals[c % vals.length];
            } else {
                mod[c] = vals[bias];
            }
        }

        Random r = new Random();
        for (int c = 0; c < 10000; c++) {
            int i = r.nextInt();
            for (int o = 0; o < vals.length; o++) {
                if (vals[o].toNanos(i) != TimeUnit.NANOSECONDS.convert(i, vals[o]))
                    throw new IllegalStateException("switch " + o);
            }
        }
    }

    @Benchmark
    public void const_convert(Blackhole bh) {
        for (TimeUnit tu : mod) {
            bh.consume(do_convert(tu));
        }
    }

    @Benchmark
    public void value_convert(Blackhole bh) {
        for (TimeUnit tu : mod) {
            bh.consume(do_convert(tu, 1L));
        }
    }

    @Benchmark
    public void const_toNanos(Blackhole bh) {
        for (TimeUnit tu : mod) {
            bh.consume(do_toNanos(tu));
        }
    }

    @Benchmark
    public void value_toNanos(Blackhole bh) {
        for (TimeUnit tu : mod) {
            bh.consume(do_toNanos(tu, 1L));
        }
    }

    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    private long do_toNanos(TimeUnit tu) {
        return tu.toNanos(1L);
    }

    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    private long do_toNanos(TimeUnit tu, long value) {
        return tu.toNanos(value);
    }

    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    private long do_convert(TimeUnit tu) {
        return TimeUnit.NANOSECONDS.convert(1L, tu);
    }

    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    private long do_convert(TimeUnit tu, long value) {
        return TimeUnit.NANOSECONDS.convert(value, tu);
    }
}

Results:

Benchmark                    (bias)  Mode  Cnt    Score    Error  Units
TimeUnitBench.const_convert       0  avgt    5  151,856 ± 28,595  ns/op
TimeUnitBench.const_convert       1  avgt    5  150,720 ± 23,863  ns/op
TimeUnitBench.const_convert       2  avgt    5  151,432 ± 23,184  ns/op
TimeUnitBench.const_convert       3  avgt    5  150,959 ± 24,726  ns/op
TimeUnitBench.const_convert       4  avgt    5  150,966 ± 25,280  ns/op
TimeUnitBench.const_convert       5  avgt    5  150,976 ± 25,542  ns/op
TimeUnitBench.const_convert       6  avgt    5  151,118 ± 25,254  ns/op
TimeUnitBench.const_convert      -1  avgt    5  152,673 ± 29,861  ns/op
TimeUnitBench.const_toNanos       0  avgt    5  121,296 ± 25,236  ns/op
TimeUnitBench.const_toNanos       1  avgt    5  121,707 ± 25,364  ns/op
TimeUnitBench.const_toNanos       2  avgt    5  121,736 ± 25,726  ns/op
TimeUnitBench.const_toNanos       3  avgt    5  121,822 ± 25,491  ns/op
TimeUnitBench.const_toNanos       4  avgt    5  121,867 ± 26,090  ns/op
TimeUnitBench.const_toNanos       5  avgt    5  121,927 ± 25,611  ns/op
TimeUnitBench.const_toNanos       6  avgt    5  121,821 ± 25,843  ns/op
TimeUnitBench.const_toNanos      -1  avgt    5  121,923 ± 25,206  ns/op
TimeUnitBench.value_convert       0  avgt    5  150,525 ± 25,439  ns/op
TimeUnitBench.value_convert       1  avgt    5  151,098 ± 24,024  ns/op
TimeUnitBench.value_convert       2  avgt    5  151,259 ± 25,381  ns/op
TimeUnitBench.value_convert       3  avgt    5  151,030 ± 25,548  ns/op
TimeUnitBench.value_convert       4  avgt    5  151,290 ± 25,998  ns/op
TimeUnitBench.value_convert       5  avgt    5  151,006 ± 25,448  ns/op
TimeUnitBench.value_convert       6  avgt    5  150,945 ± 25,314  ns/op
TimeUnitBench.value_convert      -1  avgt    5  151,186 ± 25,814  ns/op
TimeUnitBench.value_toNanos       0  avgt    5  121,556 ± 25,745  ns/op
TimeUnitBench.value_toNanos       1  avgt    5  123,410 ± 22,323  ns/op
TimeUnitBench.value_toNanos       2  avgt    5  125,452 ± 26,575  ns/op
TimeUnitBench.value_toNanos       3  avgt    5  125,297 ± 26,204  ns/op
TimeUnitBench.value_toNanos       4  avgt    5  125,357 ± 26,085  ns/op
TimeUnitBench.value_toNanos       5  avgt    5  125,165 ± 26,185  ns/op
TimeUnitBench.value_toNanos       6  avgt    5  125,536 ± 25,487  ns/op
TimeUnitBench.value_toNanos      -1  avgt    5  125,460 ± 25,063  ns/op

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/8376/head:pull/8376
$ git checkout pull/8376

Update a local copy of the PR:
$ git checkout pull/8376
$ git pull https://git.openjdk.org/jdk pull/8376/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 8376

View PR using the GUI difftool:
$ git pr show -t 8376

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/8376.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Apr 23, 2022

👋 Welcome back aturbanov! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

@openjdk openjdk bot commented Apr 23, 2022

@turbanoff The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-jfr
  • nio

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added nio core-libs hotspot-jfr labels Apr 23, 2022
@turbanoff turbanoff changed the title [PATCH] Simplify TimeUnit.convert calls to TimeUnit.toSomething instead 8285519: Simplify TimeUnit.convert calls Apr 24, 2022
@openjdk openjdk bot added the rfr label Apr 24, 2022
@mlbridge
Copy link

@mlbridge mlbridge bot commented Apr 24, 2022

Webrevs

@turbanoff turbanoff changed the title 8285519: Simplify TimeUnit.convert calls 8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX Apr 24, 2022
@AlanBateman
Copy link
Contributor

@AlanBateman AlanBateman commented Apr 30, 2022

The changes to the Selector implementations to use toXXX look okay. Note that it's not performance critical as it is only executed when a thread blocked in epoll_wait/equivalent is interrupted by a signal, it's not a normal code path.

….toSome_instead_of_TimeUnit.convert

# Conflicts:
#	src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java
#	src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java
@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Jun 12, 2022

@turbanoff This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@turbanoff
Copy link
Member Author

@turbanoff turbanoff commented Jun 13, 2022

Still hope to merge it :)

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

LGTM

@openjdk
Copy link

@openjdk openjdk bot commented Jun 13, 2022

@turbanoff This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX

Reviewed-by: rriggs

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 446 new commits pushed to the master branch:

  • e90b579: 8288332: Tier1 validate-source fails after 8279614
  • b42c1ad: 8279614: The left line of the TitledBorder is not painted on 150 scale factor
  • 9b6d0a7: 8285838: DST not applying properly with zone id offset set with TZ env variable
  • 53a0ace: 8286101: Support formatting in @value tag
  • 8f400b9: 8286779: javax.crypto.CryptoPolicyParser#isConsistent always returns 'true'
  • e0baf01: 8287007: [cgroups] Consistently use stringStream throughout parsing code
  • 1769596: 8285263: Minor cleanup could be done in java.security
  • b97a4f6: 8288114: Update JIRA link in vcs.xml
  • 2adef6a: 8267860: Off-by-one bug when searching arrays in AlpnGreaseTest
  • 4aede33: 8288282: Zero-release build is broken after JDK-8279047 due to UseHeavyMonitors is read-only
  • ... and 436 more: https://git.openjdk.org/jdk/compare/0e4bece5b5143b8505496ea7430bbfa11e151aff...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready label Jun 13, 2022
}
if (s.endsWith("h")) {
return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
return HOURS.toNanos(Long.parseLong(s.substring(0, s.length() - 1).trim()));
Copy link
Member Author

@turbanoff turbanoff Jun 14, 2022

Choose a reason for hiding this comment

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

May be some JFR experts know, why there was manual multiplication before?

@turbanoff
Copy link
Member Author

@turbanoff turbanoff commented Jun 16, 2022

/integrate

@openjdk
Copy link

@openjdk openjdk bot commented Jun 16, 2022

Going to push as commit e833c1d.
Since your change was applied there have been 496 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated label Jun 16, 2022
@openjdk openjdk bot closed this Jun 16, 2022
@openjdk openjdk bot removed ready rfr labels Jun 16, 2022
@openjdk
Copy link

@openjdk openjdk bot commented Jun 16, 2022

@turbanoff Pushed as commit e833c1d.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs hotspot-jfr integrated nio
3 participants