Skip to content

Improve pbf parser performance - #2193

Closed
otbutz wants to merge 7 commits into
graphhopper:masterfrom
otbutz:speedup_pbf
Closed

otbutz wants to merge 7 commits into
graphhopper:masterfrom
otbutz:speedup_pbf

Conversation

@otbutz

@otbutz otbutz commented Nov 26, 2020

Copy link
Copy Markdown
Contributor

This should speedup pbf parsing by about 10%

@otbutz

otbutz commented Nov 30, 2020

Copy link
Copy Markdown
Contributor Author

#2191 reduced parsing duration for my test dataset from 74s to 50s. With the changes of this PR we're down to about 38s.

@otbutz

otbutz commented Dec 1, 2020

Copy link
Copy Markdown
Contributor Author

Fixes #2197

@karussell

Copy link
Copy Markdown
Member

Do you know the underlying reason of the speed up?

@otbutz

otbutz commented Dec 8, 2020

Copy link
Copy Markdown
Contributor Author

Most of the commits aim to reduce object creation and collection resizes which are both costly operations if they're often repeated.

0f376c6 tries to avoid additional lookups in the tree by passing through internal IDs from previous lookups.

16eb716 follows the OSM pbf spec as we only need to process one typ of object within a PrimitiveGroup.

@otbutz

otbutz commented Dec 8, 2020

Copy link
Copy Markdown
Contributor Author

Is there any reason we keep ReaderNode.getEle() ? It's an ugly hack within setTag and isn't really used for routing.

@otbutz

otbutz commented Dec 8, 2020

Copy link
Copy Markdown
Contributor Author

@karussell i removed the node skipping logic as it was quite an invasive change with little benefit. Also removed the elevation handling from the reader.

@otbutz

otbutz commented Dec 9, 2020

Copy link
Copy Markdown
Contributor Author

@karussell any remarks? I'd like to get this merged as my other parsing related PRs are based on this.

@easbar

easbar commented Dec 11, 2020

Copy link
Copy Markdown
Member

Could you please update this to latest master?

@otbutz

otbutz commented Dec 11, 2020

Copy link
Copy Markdown
Contributor Author

@easbar done :)

@easbar

easbar commented Dec 11, 2020

Copy link
Copy Markdown
Member

I did not look at the code yet, but I can confirm that this branch is faster and the numbers of nodes and edges stays the same. For germany-190101.osm.pbf I got:

#master:
com.graphhopper.reader.osm.OSMReader - time pass1:114s, pass2:198s, total:312s
#this branch:
com.graphhopper.reader.osm.OSMReader - time pass1:109s, pass2:178s, total:288s

@otbutz

otbutz commented Dec 11, 2020 •

Copy link
Copy Markdown
Contributor Author

Thanks for testing 😃

#2205 should yield even better results while improving the readability of the code

@karussell

karussell commented Dec 11, 2020 •

Copy link
Copy Markdown
Member

In general it is not clear which change is only for refactoring (or clean up) and which is to improve performance - can you move the unrelated changes into a separate PR?

0f376c6 tries to avoid additional lookups in the tree by passing through internal IDs from previous lookups.

The OSMReader is already a complicated mess ;) so I would try to avoid making it worse. And the "graphIndex" would add to this. Is this required for the speed up? I would really like to clean up and/or refactor OSMReader to make it faster, but readability and maintainability should be a higher priority now that reading PBFs is acceptable fast (and usually only a small part when CH or LM is involved).

16eb716 follows the OSM pbf spec as we only need to process one typ of object within a PrimitiveGroup.

Is this required for the speed up?

#2205 should yield even better results while improving the readability of the code

What is the reason you drop support for XML in #2205? And if this is done it should be done in a separate PR. In general smaller PRs can be merged faster :)

@otbutz

otbutz commented Dec 11, 2020 •

Copy link
Copy Markdown
Contributor Author

In general it is not clear which change is only for refactoring (or clean up) and which is to improve performance - can you move the unrelated changes into a separate PR?

Sure

The OSMReader is already a complicated mess ;)

Agreed 😅

so I would try to avoid making it worse. And the "graphIndex" would add to this. Is this required for the speed up?

See #2194 for more details. It was an attempt to reduce the number of lookups.

I would really like to clean up and/or refactor OSMReader to make it faster, but readability and maintainability should be a higher priority now that reading PBFs is acceptable fast (and usually only a small part when CH or LM is involved).

Fair point. I'm going to remove it.

16eb716 follows the OSM pbf spec as we only need to process one typ of object within a PrimitiveGroup.

Is this required for the speed up?

Just a minor bugfix. I will move it to a separate PR.

What is the reason you drop support for XML in #2205?

The current implementation has to deal with both formats which leaves little room for optimizations and makes the code less readable. e.g osm2pgsql requires all imported pbfs to be sorted by type and id now. If we follow their requirement it could help us to improve #2194

See also our discussion in #2130

Anybody who wants to import XML files can still convert them with tools like osmconvert or osmium.

And if this is done it should be done in a separate PR. In general smaller PRs can be merged faster :)

It's in fact it's already a separate branch but i haven't created PR yet.

@easbar

easbar commented Dec 13, 2020 •

Copy link
Copy Markdown
Member

I ran master vs this branch a few more times:

# master
2020-12-13 15:48:08.522 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:180s, pass2:280s, total:460s
2020-12-13 15:57:45.614 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:178s, pass2:283s, total:462s
2020-12-13 16:07:44.938 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:187s, pass2:296s, total:483s
2020-12-13 16:17:48.119 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:191s, pass2:292s, total:483s
2020-12-13 16:27:18.800 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:175s, pass2:279s, total:455s
2020-12-13 16:37:04.125 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:181s, pass2:287s, total:469s
2020-12-13 16:46:39.075 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:280s, total:459s
2020-12-13 16:56:11.371 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:276s, total:455s
2020-12-13 17:05:46.935 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:281s, total:461s
2020-12-13 17:15:35.966 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:189s, pass2:282s, total:471s
2020-12-13 17:25:14.669 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:281s, total:460s
2020-12-13 17:34:50.932 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:278s, total:458s
# speedup_pbf      
2020-12-13 09:48:14.702 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:170s, pass2:264s, total:434s
2020-12-13 09:57:27.834 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:170s, pass2:268s, total:438s
2020-12-13 10:06:51.245 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:179s, pass2:268s, total:447s
2020-12-13 10:16:08.205 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:173s, pass2:268s, total:441s
2020-12-13 10:25:36.795 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:177s, pass2:273s, total:451s
2020-12-13 10:34:49.561 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:170s, pass2:267s, total:437s
2020-12-13 10:44:07.813 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:174s, pass2:268s, total:442s
2020-12-13 10:53:29.824 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:172s, pass2:272s, total:445s
2020-12-13 11:02:47.947 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:172s, pass2:269s, total:441s
2020-12-13 11:12:28.436 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:182s, pass2:281s, total:463s
2020-12-13 11:21:45.686 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:171s, pass2:270s, total:441s
2020-12-13 11:31:05.948 [main] INFO  com.graphhopper.reader.osm.OSMReader - time pass1:174s, pass2:270s, total:444s

The speedup seems like less than 10%? I was trying to check whether all the commits yield an improvement or if some are more crucial than others, but this wasn't very clear, because the differences were rather small. Did you run a similar experiment? This was germany-190101.osm.pbf (on a rather weak server) btw.

@easbar

easbar commented Dec 14, 2020

Copy link
Copy Markdown
Member

The current implementation has to deal with both formats which leaves little room for optimizations and makes the code less readable. e.g osm2pgsql requires all imported pbfs to be sorted by type and id now. If we follow their requirement it could help us to improve #2194

I agree that having less different input (same for output) formats potentially leads to better maintainability and does not have many drawbacks especially when there are good tools to do the conversion. However, regarding pbf/xml OSM I wish we can keep xml support at least for tests, because binary pbf files are hard to track in git and harder to work with. Can we e.g. keep osmosis (its Java) as test dependency such that we can keep some small OSM xml files and convert them on the fly for tests (assuming our OSMReader only supports pbf in the future)? Or use osmium or some other tool and do the conversion as a pre-build step when running the tests.

@otbutz

otbutz commented Dec 14, 2020

Copy link
Copy Markdown
Contributor Author

binary pbf files are hard to track in git

Git and binary files doesn't mix well but at least our test cases are tiny and usually added once without any further updates to their content.

and harder to work with.

If someone really wants to track down a bug and needs to examine the pbf:

osmium cat -f osm map-bug432.osm.pbf

I'm not sure if it's worth the hassle to do the conversion step for each unittest run if these files are rarely modified or even looked at.

@easbar

easbar commented Dec 14, 2020

Copy link
Copy Markdown
Member

I'm not sure if it's worth the hassle to do the conversion step for each unittest run if these files are rarely modified or even looked at.

Why is it much hassle when the files are small? It should be quite fast? And like you say binary files do not work well with git, so in case we make an adjustment to one of these files it would be a hassle to convert them to xml and compare the different versions.

@otbutz

otbutz commented Dec 14, 2020

Copy link
Copy Markdown
Contributor Author

If we go for this it would be easier to convert the pbfs of my other branch back to XML as i've already fixed a few issues like sorting etc. We also need to make sure that timestamps of the generated pbfs are correct as some unittests check them but i don't really know the reason for that. Maybe we can remove those checks.

@otbutz

otbutz commented Dec 14, 2020

Copy link
Copy Markdown
Contributor Author

@karussell what's your take on this?

@karussell

karussell commented Dec 14, 2020 •

Copy link
Copy Markdown
Member

I also prefer keeping XML for tests as a PBF file would require yet another step to decipher the graph (and changes are harder to track). And/or we create a possibility with ascii charts like suggested in #143 :)

@otbutz

otbutz commented Dec 14, 2020

Copy link
Copy Markdown
Contributor Author

Can we merge this PR? I'm going to refactor my other branch and switch to on-the-fly conversion for the test files.

@easbar

easbar commented Dec 21, 2020 •

Copy link
Copy Markdown
Member

Can we merge this PR? I'm going to refactor my other branch and switch to on-the-fly conversion for the test files.

I agree with @karussell here that it is still hard to see what changes are meant to be speed ups and which ones are mostly there to clean up the code. Also I fully agree that cleaning up should be the priority. I 'accidentally' got familiar with the OSM reading code because of another bug I am investigating (#1984 ) so I had a look at this PR again. I see the following issues:

  • Its still not clear if the changes to the pbf parser are meant to speed up the import or if this is a 'minor bugfix' as you said that you will move to another PR?

  • I was not able to measure a significant speed up (it was <10%) so I think we should only apply changes that make the code easier to understand.

Regarding the changes in this PR I see three main things that were changed:

  1. lazy initialization of the properties HashMap in ReaderElement -> let's leave a comment that this might be a potential speed up, but not do it now.

  2. elevation tag in ReaderNode: afaik this feature is not used, so yes I think we should remove it @karussell? If so this should go into a separate PR. -> Remove handling of node tag "ele" #2230

  3. The changes in PbfBlobDecoder seem to be a mix of clean ups (like removing commented out code) and other improvements. Can we maybe just do the clean ups first/here so it is easier to see what really changed and how the decoder got improved?

Btw another thing that looks like it can be removed is the inBounds feature. That should also allow cleaning up the code a bit more. @karussell? --> #2220

@easbar

easbar commented Dec 21, 2020

Copy link
Copy Markdown
Member

Once we come to an agreement about what should be done we can probably also just cherry pick some of this PR's commits as they already nicely separate the different changes.

Comment on lines -101 to -116

// Build a new bound object which corresponds to the header.
/*
Bound bound;
if (header.hasBbox()) {
HeaderBBox bbox = header.getBbox();
bound = new Bound(bbox.getRight() * COORDINATE_SCALING_FACTOR, bbox.getLeft() * COORDINATE_SCALING_FACTOR,
bbox.getTop() * COORDINATE_SCALING_FACTOR, bbox.getBottom() * COORDINATE_SCALING_FACTOR,
header.getSource());
} else {
bound = new Bound(header.getSource());
}

// Add the bound object to the results.
decodedEntities.add(new BoundContainer(bound));
*/

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.

Related: #2133

@easbar

easbar commented Dec 21, 2020 •

Copy link
Copy Markdown
Member

And/or we create a possibility with ascii charts like suggested in #143 :)

I think we should be able to easily use VCS friendly .osm.xml files for testing. On the other hand I do not see many benefits in testing with OSM-like data except for testing the code that does the OSM import and some end-to-end tests. For everything else testing via the Java/GraphHopper API seems a lot more useful. Therefore I do not really see a use-case for these ascii charts ;)

@otbutz

otbutz commented Dec 22, 2020

Copy link
Copy Markdown
Contributor Author

Focusing on better readability first seems like a good idea. My biggest gripe with the existing XML files is that most of them violate the specification one way or another. Fixing all these issues in order to able to convert them on-the-fly is going to take a while.

@easbar

easbar commented Dec 22, 2020

Copy link
Copy Markdown
Member

My biggest gripe with the existing XML files is that most of them violate the specification one way or another. Fixing all these issues in order to able to convert them on-the-fly is going to take a while.

What kind of specifications are violated? Can we address these issues first without removing XML support yet?

@easbar

easbar commented Dec 22, 2020 •

Copy link
Copy Markdown
Member

Btw I see you put in a lot of work into this OSM parsing code. What is the overall goal of this? Is this mostly about speeding things up or removing the osmosis dependency? Do you also have functional improvements or extensions n mind?

@otbutz

otbutz commented Dec 22, 2020

Copy link
Copy Markdown
Contributor Author

What kind of specifications are violated?

I tried to do the conversion using Osmosis v0.6 XML Reader and OsmosisSerializer. It got various errors about missing timestamp and version tags.

Is this mostly about speeding things up or removing the osmosis dependency?

Speed and readability.

@easbar

easbar commented Dec 22, 2020 •

Copy link
Copy Markdown
Member

Osmosis 0.6? Isn't 0.48 the latest release: https://github.com/openstreetmap/osmosis/releases/latest? You probably mean OSM API version 0.6 and this is in fact not supported by e.g. latest versions of Osmosis and JOSM. I had a similar problem when I tried opening one of the test xml files in JOSM (it required the version tag).

The last osmosis version that supported OSM v0.5 was 0.35 and it has a migrate command to convert from v0.5 to v0.6: https://wiki.openstreetmap.org/wiki/Osmosis/Detailed_Usage_0.35#--migrate
As a first step can we convert the current test OSM files into a format that is compatible with OSM v0.6?

--> #2223

@otbutz

otbutz commented Dec 22, 2020

Copy link
Copy Markdown
Contributor Author

As a first step can we convert the current test OSM files into a format that is compatible with OSM v0.6?

--> #2223

That's exactly the problem i'm facing.

@karussell

Copy link
Copy Markdown
Member

I'll close this issue here.

@otbutz let me explain :) Our first step should be to make OSMReader simpler - it is a mess :) !

See https://github.com/graphhopper/graphhopper/commits/osm_reader_rewrite for an experimental branch that also includes some minor ideas from here I think.

But even this rewrite is not yet possible as we first need to get the correct bigger picture. And so we e.g. need to think about how to handle the node tags (see my branch node_parser_extraction and also related road_access). btw: this should make node tags available in a custom model too.

And after this is done we can then try to make the (new) OSMReader faster again. Especially due to your contribution from #2191 I think performance is now less of a concern and we really need to focus on maintainability.

@otbutz

otbutz commented Feb 11, 2021

Copy link
Copy Markdown
Contributor Author

Our first step should be to make OSMReader simpler - it is a mess :) !

I wholeheartedly agree 😉

This was referenced Jul 22, 2021
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.

3 participants