Skip to content
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

Provide a IntCounter and IntGauge #149

Closed
siddontang opened this issue Jan 31, 2018 · 7 comments
Closed

Provide a IntCounter and IntGauge #149

siddontang opened this issue Jan 31, 2018 · 7 comments

Comments

@siddontang
Copy link
Contributor

Mostly, we use the integer for counter and gauge, so it is more efficient to use atomic int64 directly than float64.

@breezewish
Copy link
Member

prometheus-go implements similar optimizations, but leaving a nicer interface (transparent to users): prometheus/client_golang@ae69392

@siddontang
Copy link
Contributor Author

cool, but we should support math.Trunc, and do a benchmark to compare Trunc + Int Atomic and Float Atomic.

@breezewish
Copy link
Member

breezewish commented Mar 1, 2018

yes. I will do a benchmark. If the performance impact of math.trunc is minimal, I prefer this way.

@breezewish
Copy link
Member

breezewish commented Mar 1, 2018

According to the benchmark, adding a trunc check (and uses float as interface) will slow down about 20%~30% compared to AtomicInt:

Mac:

test counter::bench_atomic_f64                          ... bench:          11 ns/iter (+/- 2)
test counter::bench_atomic_u64                          ... bench:           4 ns/iter (+/- 0)
test counter::bench_atomic_u64_trunc                    ... bench:           6 ns/iter (+/- 2)

Linux:

test counter::bench_atomic_f64                          ... bench:          11 ns/iter (+/- 1)
test counter::bench_atomic_u64                          ... bench:           6 ns/iter (+/- 0)
test counter::bench_atomic_u64_trunc                    ... bench:           8 ns/iter (+/- 0)

However it is still nearly 30%~50% faster than the AtomicFloat. In addition, the f64 is as slow as the RwLock implementation (with is used as a replacement without nightly).

@siddontang
Copy link
Contributor Author

@breezewish
Copy link
Member

According to the rust book,

Casting from a float to an integer will round the float towards zero.

I guess it is equal to if (v.round() as u64) as f64 == v. Will update the benchmark for this later.

@breezewish
Copy link
Member

Time consumption of these two are exactly the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants