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

Fix performance regresssion #9045

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed a performance regression when opening large libraries [#9041](https://github.com/JabRef/jabref/issues/9041)

### Removed


Expand Down
40 changes: 7 additions & 33 deletions src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;

import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.model.database.event.EntriesAddedEvent;
Expand All @@ -30,7 +29,6 @@
import org.jabref.model.entry.Month;
import org.jabref.model.entry.event.EntriesEventSource;
import org.jabref.model.entry.event.EntryChangedEvent;
import org.jabref.model.entry.event.FieldAddedOrRemovedEvent;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
Expand All @@ -54,8 +52,6 @@ public class BibDatabase {
* State attributes
*/
private final ObservableList<BibEntry> entries = FXCollections.synchronizedObservableList(FXCollections.observableArrayList(BibEntry::getObservables));

private final ObservableSet<Field> visibleFields = FXCollections.observableSet();
private Map<String, BibtexString> bibtexStrings = new ConcurrentHashMap<>();

private final EventBus eventBus = new EventBus();
Expand Down Expand Up @@ -140,8 +136,13 @@ public ObservableList<BibEntry> getEntries() {
*
* @return set of fieldnames, that are visible
*/
public ObservableSet<Field> getAllVisibleFields() {
return visibleFields;
public Set<Field> getAllVisibleFields() {
Set<Field> allFields = new TreeSet<>(Comparator.comparing(Field::getName));
for (BibEntry e : getEntries()) {
allFields.addAll(e.getFields());
}
return allFields.stream().filter(field -> !FieldFactory.isInternalField(field))
.collect(Collectors.toSet());
}

/**
Expand Down Expand Up @@ -213,8 +214,6 @@ public synchronized void insertEntries(List<BibEntry> newEntries, EntriesEventSo
eventBus.post(new EntriesAddedEvent(newEntries, newEntries.get(0), eventSource));
}
entries.addAll(newEntries);

updateVisibleFields();
}

public synchronized void removeEntry(BibEntry bibEntry) {
Expand Down Expand Up @@ -252,7 +251,6 @@ public synchronized void removeEntries(List<BibEntry> toBeDeleted, EntriesEventS
boolean anyRemoved = entries.removeIf(entry -> ids.contains(entry.getId()));
if (anyRemoved) {
eventBus.post(new EntriesRemovedEvent(toBeDeleted, eventSource));
updateVisibleFields();
}
}

Expand Down Expand Up @@ -586,30 +584,6 @@ private void relayEntryChangeEvent(FieldChangedEvent event) {
eventBus.post(event);
}

@Subscribe
private void listen(FieldAddedOrRemovedEvent event) {
// When a field is removed from an entry we can't tell if it's
// still present in other entries, and thus we can't remove it
// from the set of visible fields. However, when a new field is added
// to any entry, we can simply add it to the set because we're
// going to add it whether other entries have it or not
boolean isAdded = visibleFields.add(event.getField());
if (!isAdded) {
updateVisibleFields();
}
}

private void updateVisibleFields() {
visibleFields.clear();
Set<Field> allFields = new TreeSet<>(Comparator.comparing(Field::getName));
for (BibEntry e : getEntries()) {
allFields.addAll(e.getFields());
}
visibleFields.addAll(allFields.stream().filter(field -> !FieldFactory.isInternalField(field))
.filter(field -> StringUtil.isNotBlank(field.getName()))
.collect(Collectors.toSet()));
}

public Optional<BibEntry> getReferencedEntry(BibEntry entry) {
return entry.getField(StandardField.CROSSREF).flatMap(this::getEntryByCitationKey);
}
Expand Down