useragent icon indicating copy to clipboard operation
useragent copied to clipboard

Improve parsing performance

Open marselester opened this issue 3 years ago • 7 comments

It takes 8.5% less time to parse according to the benchmark, It uses ~52.7% less memory, and makes 16.8% less allocations.

Note, there are no breaking changes.

$ go test -count=10 -bench=^BenchmarkUserAgent$ -benchmem .
$ benchstat old.txt new.txt

name          old time/op    new time/op    delta
UserAgent-12     237µs ± 2%     217µs ± 2%   -8.52%  (p=0.000 n=10+10)

name          old alloc/op   new alloc/op   delta
UserAgent-12    89.8kB ± 0%    42.5kB ± 0%  -52.75%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
UserAgent-12     1.82k ± 0%     1.52k ± 0%  -16.83%  (p=0.000 n=10+10)
old.txt
goos: darwin
goarch: amd64
pkg: github.com/mileusna/useragent
cpu: Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
BenchmarkUserAgent-12    	    4748	    234376 ns/op	   89910 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    5067	    235759 ns/op	   89902 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    5061	    241559 ns/op	   89815 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4959	    241981 ns/op	   89761 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4848	    238471 ns/op	   89880 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4965	    238081 ns/op	   89819 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4884	    232825 ns/op	   89921 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4891	    235413 ns/op	   89790 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    5052	    237770 ns/op	   89868 B/op	    1824 allocs/op
BenchmarkUserAgent-12    	    4998	    237143 ns/op	   89719 B/op	    1824 allocs/op
PASS
ok  	github.com/mileusna/useragent	12.890s
new.txt
goos: darwin
goarch: amd64
pkg: github.com/mileusna/useragent
cpu: Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
BenchmarkUserAgent-12    	    5184	    214837 ns/op	   42490 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5476	    214017 ns/op	   42445 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5276	    214757 ns/op	   42505 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5454	    218415 ns/op	   42433 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5116	    217853 ns/op	   42408 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5482	    220742 ns/op	   42479 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5217	    218494 ns/op	   42431 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5294	    216651 ns/op	   42413 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5479	    218571 ns/op	   42472 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5474	    216790 ns/op	   42452 B/op	    1517 allocs/op
PASS
ok  	github.com/mileusna/useragent	12.049s

marselester avatar Oct 26 '23 00:10 marselester

Hello, thank your contribution.

As I can see it is not safe to use it concurrently, and in web apps user agents are usually parsed concurrently, for example on each HTTP request? Therefore it does represents breaking changes in existing apps.

mileusna avatar Nov 09 '23 16:11 mileusna

Hi! That's a good point, I have updated the code and benchmarks.

$ benchstat old.txt new.txt 
name          old time/op    new time/op    delta
UserAgent-12     237µs ± 2%     221µs ± 1%   -6.73%  (p=0.000 n=10+10)

name          old alloc/op   new alloc/op   delta
UserAgent-12    89.8kB ± 0%    42.5kB ± 0%  -52.66%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
UserAgent-12     1.82k ± 0%     1.52k ± 0%  -16.83%  (p=0.000 n=10+10)
new.txt
goos: darwin
goarch: amd64
pkg: github.com/mileusna/useragent
cpu: Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
BenchmarkUserAgent-12    	    4928	    220713 ns/op	   42575 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5343	    221977 ns/op	   42474 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5191	    221726 ns/op	   42533 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5101	    223259 ns/op	   42576 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5205	    222943 ns/op	   42518 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5353	    221157 ns/op	   42544 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5388	    221315 ns/op	   42494 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5380	    220784 ns/op	   42487 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5241	    220295 ns/op	   42564 B/op	    1517 allocs/op
BenchmarkUserAgent-12    	    5188	    219393 ns/op	   42513 B/op	    1517 allocs/op
PASS
ok  	github.com/mileusna/useragent	12.211s

marselester avatar Nov 09 '23 19:11 marselester

I will check it.

mileusna avatar Nov 09 '23 19:11 mileusna

Hi @mileusna, have you had a chance to check the changes?

marselester avatar Jan 16 '24 16:01 marselester

Just my 2 cents that it's not really any significant gain worthy of a refactor. The percentage in memory drop might seem good, but really it still didn't use all that much memory to begin with, which makes this 50% memory save insignificant. I think it's more important to simply keep it more readable, which is how I find the original implementation.

J-theGit avatar Feb 26 '24 18:02 J-theGit

I think it's more important to simply keep it more readable, which is how I find the original implementation.

True, that is why I hesitate to review this, specially since the first PR was not safe to use it concurrently,, so it has to be carefully tested and the risk might be higher than the reward and there are a lot of changes and refactoring to review.

I'm more focused on improving performances than memory usage, and although I have tried some things (unrelated to this PR) benchmarks still doesn't justify any change.

mileusna avatar Feb 26 '24 19:02 mileusna

I understand your concerns. Thank you for considering this change.

The current version of the PR is safe to use concurrently. The sync.Pool handles all the synchronizations. Initially I wasn't sure if concurrency safety was part of the library API or left to the caller to handle.

I tested user agent parsing on ~600K headers with the following results: it took ~2s to process, consuming ~500 MB of memory, and allocating ~11.26M objects on the heap.

Extrapolating from the benchmarks, there could be 263.3 MB of memory saved, and 1.89 million less allocations which should reduce GC pressure since there will be less heap objects to check (shorter GC pauses, less CPU consumption, improved tail latency of HTTP requests if user agents are parsed while serving requests).

I have a follow up PR https://github.com/marselester/useragent/pull/1 that could further reduce memory consumption by -36.23% (~95.4 MB) and allocations by -9.43% (~0.88M).

So the same 600K headers could be processed using 141.3 MB of memory (vs 500 MB), and 8.49M of allocs (vs 11.26M). There might be more opportunities to improve performance.

marselester avatar Feb 26 '24 21:02 marselester