JDK-8189934 : [macos] Component.getMousePosition() is slow
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8u131,9,10
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: os_x
  • CPU: x86_64
  • Submitted: 2017-10-24
  • Updated: 2021-08-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
macOS 10.13 (17A405)

EXTRA RELEVANT SYSTEM CONFIGURATION :
MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)
2,9 GHz Intel Core i5
16 GB 2133 MHz LPDDR3
Intel Iris Graphics 550 1536 MB

A DESCRIPTION OF THE PROBLEM :
Calling of Component.getMousePosition() became about 100 times slower compared to the previous versions of java.

The first version in which I observe this behaviour is:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

This is possibly related to the fix of the bug JDK-8169589 that was fixed in this version.

Works fine with:

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

REGRESSION.  Last worked in version 7u80

ADDITIONAL REGRESSION INFORMATION: 
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run this code:



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The time for 10 calls of getMousePosition() is calculated and printed every second.
On the previous versions of java this was about 1-2 milliseconds.
ACTUAL -
For the latest version of java 1.8.0_152 I see time about 70 milliseconds when I do not move the mouse within the window and about 200 milliseconds if I move the mouse within the window



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------


import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class MousePositionTest extends JPanel {

	JTextArea textArea;
	Timer timer;

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame frame = new JFrame("MousePositionTest");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setContentPane(new MousePositionTest());
				frame.pack();
				frame.setVisible(true);
			}
		});
	}

	public MousePositionTest() {
		super(new GridLayout(0, 1));
		textArea = new JTextArea();
		textArea.setEditable(false);
		add(textArea);
		setPreferredSize(new Dimension(400, 400));
		setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		timer = new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				long time = System.currentTimeMillis();
				Point pos = null;
				for (int i = 1; i < 10; i++) {
					pos = getMousePosition();
				}
				time -= System.currentTimeMillis();
				textArea.setText("pos = " + pos + " [" + -time + "ms]");
			}
		});
		timer.start();
	}

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use Java 1.8.0_121 or earlier


Comments
the fix should be back ported to 8u
14-12-2017

As discussed in the weekly meeting this bug will worked in jdk 11 and later will be back ported to jdk 10.
14-12-2017

Yes Victor. Working on this but it seems mac api CGWindowListCopyWindowInfo() is causing a delay of around 1 or 2 ms for each call in the latest JDK. This is a costly operation on mac per their swift documentation. But why this call is having a variable delay across the different JDK versions is a problem as we don't have the internal source for this implementation.
29-11-2017

Issue reproduced on macOS Sierra 10.12.6. Component.getMousePosition() is taking more time. Time taken in milliseconds by different JDK versions in my testing are as follows: JDK 9b181: 43 to 60 without mouse move 120 to 140 with mouse move JDK 9b121: 16 to 23 without mouse move 20 to 30 with mouse move JDK 8b121: 1 to 2 without mouse move 1 to 5 with mouse move
31-10-2017

Reported with JDK 8u152 MAC OS X 10.13 (High Sierra) The calling of Component.getMousePosition() is comparitively 100 times slower when compared with older JDK versions 8u121 to upwards. This seems a regression introduced in JDK 8u131 possible from fix added with JDK-8169589. Verfied this on MAC OS X 10.12.6 (Sierra) with attached test case and respective JDK versions (8u121, 8u131, 8u152, 8u162, 9, 9.0.1, and 10) and could confirm the issue. Results: ========= 8u121: OK 8u131: FAIL 8u152: FAIL 8u162 ea b01: FAIL 9: FAIL 9.0.01 - FAIL 10 ea b28 - FAIL See attached screenshots for reference.
30-10-2017