pariz / gountries

Gountries provides: Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data.
MIT License
408 stars 67 forks source link

Packed data and query speedups #6

Closed BTBurke closed 7 years ago

BTBurke commented 7 years ago

This pull request packs the YAML data into bindata.go and relies on that first before falling back to the data directory. It also implements a couple of simple indexes to speed up the queries.

I only implemented two of the things I outlined in ref #5. go-bindata is used to pack the YAML files. This makes it so the library can be embedded in any binary without needing access to the data/ directory. I updated the readme with instructions for how to repack the data if there are changes to the YAML files.

The countries are now represented as a map[string]Country by Alpha2 key. Then a couple of simple indexes from common name -> alpha2 and alpha2 -> alpha3 are generated at the same time the query is initialized. This provides a pretty significant speed up:

No query optimizations
===
BenchmarkCountryLookupByName-4         10000        136806 ns/op
PASS

Indexed query
===
BenchmarkCountryLookupByName-4       2000000           882 ns/op
PASS

All the tests pass. I added one simple query benchmark for looking up a country by name. I also added a sort to a couple of the tests to ensure that the test output comes out in a predictable order.

pariz commented 7 years ago

Thanks, i'll take a look as soon as I get off work.

pariz commented 7 years ago

Tested. Works beautifully. Thanks for your contribution.