Skip to content

Contact calculation performance tweaks - #1147

Merged
josemduarte merged 4 commits into
masterfrom
jd/contact-perf
Sep 4, 2026
Merged

josemduarte merged 4 commits into
masterfrom
jd/contact-perf

Conversation

@josemduarte

Copy link
Copy Markdown
Contributor

A few performance tweaks that increase contact calculation performance ~ 1.5x

  • Use sqrt only when needed, use distance squared otherwise
  • Primitive arrays instead of lists in GridCell
  • Presize HashMap

@aalhossary aalhossary left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I recommend 2 small updates and raised another concern that may need a substantial change.
This is Amr (Not the AI 😊).

}

public boolean hasContact(AtomIdentifier atomId1, AtomIdentifier atomId2) {
return contacts.containsKey(new Pair<AtomIdentifier>(atomId1,atomId2));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't the atom order be enforced here?
e.g.
if atom1 serial < atom2 serial then return hasContact(atomId2,atomId1)

@aalhossary aalhossary Sep 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@josemduarte I checked Pair class hashCode() and confirmed that Pair(atom1, atom2).hashCode() != Pair(atom2, atom1).hashCode(), which is definitely correct.
However, for contact it should not be the case. We should update hasContact(), getContact() and getAtomIdPairFromContact()
Do you agree?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you are right. But the issue is that changing this implementation can be a disruptive change for anywhere that assumes the current behaviour. I think what we can do right now is document it properly so no one trips over this. I've now added docs in e73ce56

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK I'll create a new issue for this point, leave this thread unresolved, while approving the PR.
Hopefully I can find time (I doubt) to study the effect of the proposed update on other BioJava code and outer code.
Do you expect such behaviour change to qualify for BioJava 7.3.0 or 8.0.0?

aalhossary added a commit to aalhossary/biojava that referenced this pull request Aug 30, 2026
Covers what is merged since 7.2.6 and what is open and expected to land:
the download and checksum work, the CATH and ECOD fixes, the contact and
ASA performance tweaks, electron density, and the JUnit 5 migration.

Five entries are for pull requests that are still open - biojava#1134, biojava#1147,
biojava#1148, biojava#1150 and biojava#1151 - and should be checked against what actually
merged before the release is tagged.

@aalhossary aalhossary left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PR provides useful updates.
However, reviewing it raised an issue (a potential bug), to be addressed later.

}

public boolean hasContact(AtomIdentifier atomId1, AtomIdentifier atomId2) {
return contacts.containsKey(new Pair<AtomIdentifier>(atomId1,atomId2));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK I'll create a new issue for this point, leave this thread unresolved, while approving the PR.
Hopefully I can find time (I doubt) to study the effect of the proposed update on other BioJava code and outer code.
Do you expect such behaviour change to qualify for BioJava 7.3.0 or 8.0.0?

@josemduarte
josemduarte merged commit e84025e into master Sep 4, 2026
0 of 5 checks 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