-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Lower graph build memory by processing GTFS shapes more efficiently #6752
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
Merged
leonardehrenfried
merged 19 commits into
opentripplanner:dev-2.x
from
leonardehrenfried:efficient-shapes
Aug 13, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
15c31b2
Make processing of GTFS shapes more efficient
leonardehrenfried e5157e4
Improve iteration of efficient shapes
leonardehrenfried 99b4bd7
Reduce duplicated sorting during geometry processing
leonardehrenfried 35814ad
Add test
leonardehrenfried 30e027f
Update shape points
leonardehrenfried 9253178
Skip intermediate list
leonardehrenfried fe3949f
Clean up
leonardehrenfried e3137eb
Remove shape handling from OtpTransitServiceImpl
leonardehrenfried 0117ae6
Remove duplicate copies of stop times
leonardehrenfried 86e6da8
Rename builder to just 'CompactShape'
leonardehrenfried 7ddd7f2
Remove route short name from StopTime
leonardehrenfried 4397d79
Improve Javadoc
leonardehrenfried f7b2aa7
Improve tests
leonardehrenfried 3583947
Make CompactShape more robust in the face of very large sequence numb…
leonardehrenfried 7ed5b82
Add Javadoc
leonardehrenfried a7cb470
CompactShape that sorts temporary Lists
abyrd 4e57b36
Add test for ShapePoint#equals
leonardehrenfried 9eeb679
Apply review feedback
leonardehrenfried fd05541
Javadoc
leonardehrenfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
application/src/main/java/org/opentripplanner/gtfs/mapping/CompactShape.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.opentripplanner.gtfs.mapping; | ||
|
||
import gnu.trove.list.TDoubleList; | ||
import gnu.trove.list.TIntList; | ||
import gnu.trove.list.array.TDoubleArrayList; | ||
import gnu.trove.list.array.TIntArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import org.opentripplanner.model.ShapePoint; | ||
|
||
/** | ||
* A representation that stores GTFS shape points in a memory-efficient way and allows you to | ||
* iterate over them for further processing. | ||
* <p> | ||
* The fields of ShapePoints are stored densely in automatically expanding Trove primitive lists. | ||
* When later needed, they are reconstituted into objects and sorted on their sequence number. | ||
* Only an iterator over the sorted collection escapes rather than the collection itself, providing | ||
* some assurance that the objects will be quickly garbage collected. | ||
* <p> | ||
* This class is package-private but implements Iterable, so you should use that as the return type | ||
* of the mapping process. | ||
*/ | ||
class CompactShape implements Iterable<ShapePoint> { | ||
|
||
private static final int INITIAL_CAPACITY = 50; | ||
private static final double NO_DISTANCE = -9999; | ||
|
||
private final TIntList seqs = new TIntArrayList(INITIAL_CAPACITY); | ||
private final TDoubleList lats = new TDoubleArrayList(INITIAL_CAPACITY); | ||
private final TDoubleList lons = new TDoubleArrayList(INITIAL_CAPACITY); | ||
private final TDoubleList dists = new TDoubleArrayList(INITIAL_CAPACITY); | ||
|
||
public void addPoint(org.onebusaway.gtfs.model.ShapePoint shapePoint) { | ||
seqs.add(shapePoint.getSequence()); | ||
lats.add(shapePoint.getLat()); | ||
lons.add(shapePoint.getLon()); | ||
dists.add(shapePoint.isDistTraveledSet() ? shapePoint.getDistTraveled() : NO_DISTANCE); | ||
} | ||
|
||
@Override | ||
public Iterator<ShapePoint> iterator() { | ||
return IntStream.range(0, lats.size()) | ||
.mapToObj(i -> { | ||
double dist = dists.get(i); | ||
return new ShapePoint(seqs.get(i), lats.get(i), lons.get(i), dist < 0 ? null : dist); | ||
}) | ||
.sorted() | ||
.iterator(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.