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

Optimize map accesses in several places #1644

Merged
merged 2 commits into from
Sep 23, 2024

Conversation

rPraml
Copy link
Contributor

@rPraml rPraml commented Sep 23, 2024

In this PR I will try to optimize map access on several places and pick up @gbrail's comment: #1575 (comment)

// Update the existing value and keep it in the same place in the list
map.get(nv).value = value;
}
map.compute(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we will save one hash computation, but have the price of an additional invocation of a lambda expression:
I did this short test:

	@Test
	public void speed() {
		for (long i = 0L; i < 10_000_000L; i++) {
			ht.put("one", 1);
			ht.put("two", 2);
			ht.put("three", 3);
			ht.put("four", 4);
			ht.put("five", 5);
			ht.put("six", 6);
			ht.put("seven", 7);
			ht.put("eight", 8);
			ht.put("nine", 9);
			ht.put("ten", 10);

		}
	}

it runs with the old code in apporx 8,5sec and with the new code approx 5,5 sec

@@ -595,23 +599,21 @@ private void reflect(
NativeJavaMethod setters = null;
String setterName = "set".concat(nameComponent);

if (ht.containsKey(setterName)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can safely remove containsKey here, as we call get in the next line, which is checked for null by the instanceOf test.

@@ -687,13 +689,11 @@ private Field[] getAccessibleFields(boolean includeProtected, boolean includePri
private static MemberBox findGetter(
boolean isStatic, Map<String, Object> ht, String prefix, String propertyName) {
String getterName = prefix.concat(propertyName);
if (ht.containsKey(getterName)) {
// Check that the getter is a method.
Object member = ht.get(getterName);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here. Do not hit map twice

@gbrail
Copy link
Collaborator

gbrail commented Sep 23, 2024

Yes, this is a good change. Thanks!

@gbrail gbrail merged commit 2eccdd1 into mozilla:master Sep 23, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants