magiconair / properties

Java properties scanner for Go
BSD 2-Clause "Simplified" License
323 stars 77 forks source link

⚡ Speedup merge #65

Closed AdityaVallabh closed 1 year ago

AdityaVallabh commented 1 year ago

The Merge function currently checks if a key in other exists in p by iterating over all keys in p which is redundant as we already have a faster way to look that up in p.m.

This reduces the asymptotic complexity of Merge from quadratic O(N2) down to linear O(N) where N is the number of properties.

Also wrote a benchmark to measure the exact speedup and here are the results:

Merging hundred properties - 6x faster Merging thousand properties - 37x faster Merging 10K properties. - 300x faster Merging 100K properties - 2600x faster

Before

$ GOMAXPROCS=1 go test -run=^$ -bench ^BenchmarkMerge$ github.com/magiconair/properties
goos: linux
goarch: amd64
pkg: github.com/magiconair/properties
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkMerge/num_properties_100                  58174             19691 ns/op
BenchmarkMerge/num_properties_1000                   687           1637600 ns/op
BenchmarkMerge/num_properties_10000                    7         156907466 ns/op
BenchmarkMerge/num_properties_100000                   1        16630149940 ns/op
PASS

After

$ GOMAXPROCS=1 go test -run=^$ -bench ^BenchmarkMerge$ github.com/magiconair/properties
goos: linux
goarch: amd64
pkg: github.com/magiconair/properties
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkMerge/num_properties_100                 342036              3254 ns/op
BenchmarkMerge/num_properties_1000                 27264             44239 ns/op
BenchmarkMerge/num_properties_10000                 2245            523613 ns/op
BenchmarkMerge/num_properties_100000                 190           6237122 ns/op
PASS
magiconair commented 1 year ago

Yes, I'll try to review and merge today or tomorrow.

magiconair commented 1 year ago

Done. Merged to v1.8.7