Comments (4)
Since Java 7, you can create your own inlining cache in Java code (like the bimorphic inlining cache of hotspot) using the java.lang.invoke API. This API also comes with a way to trigger code de-optimization (j.l.i.SwitchPoint) so instead of having a volatile read, you can trigger the de-optimization of the logging code when the configuration change making a call to a logger that doesn't log really free.
You may not believe me so there is a simple test:
get jar of the logger from https://github.com/forax/beautiful_logger (the jar is on JitPack) and write a simple JMH test that compare the speed of flogger with that logger, you will see that when there is nothing to log, this logger is faster that flogger, in fact it's as fast as having an empty method.
Note: the code is a little complex because it is Java 8 to 11 compatible and the way to get the caller class in 8 and 9 has changed.
from flogger.
As with all benchmarks, it depends greatly on what you are measuring and how.
Flogger's claims around "cheap disabled log statements" relate to logger usage where you pass multiple parameters. Consider:
jdkLogger.fine(String.format("Hello %s", world))
jdkLogger.fine("Hello " + world)
Which both do work at the call site to construct and copy a string that's never used.
logger.atFine().log("Hello %s", world)
allocates no objects and does only a minimal amount of work to check the log level.
Obviously it's hard to beat:
jdkLogger.fine("constant string");
since that should be a volatile read, level check and branch, but that's not what the average log statement looks like (at least not in Google).
Avoiding allocation is particularly important, since fine logging is typically hit orders of magnitude more often than the normally enabled levels. Even if it's fast at the log site, if it allocates memory it increases garbage collector pressure (more frequent GC events, more object churn etc...).
If you have your benchmarks available, I'd be happy to take a look.
from flogger.
I've only tested
logger.fine("constant string");
what if there was a way to really pay no cost (no volatile read, no branch) ?
from flogger.
We have worked hard for the (still internal) Android version of Flogger to make sure that tools like Proguard can identify disabled log statements and completely remove them (including loggers and variable setup). However this requires bytecode rewriting and would probably not really help much on servers with hotspot compilation anyway (and it obviously doesn't let you re-enable logging at run time either). Unfortunately this is still not quite ready to be open-sourced for frustrating technical reasons I can't really go into.
As for the server side code, the log context is deliberately bi-morphic (only 2 specific subtypes per logger; the real context and the no-op one). This is helpful because virtual method dispatch to bi-morphic types is special cased by most hotspot VMs. Thus if a log statement is seen to be "always disabled" then the fast-path generated by the hot-spot compiler will be very fast (it's likely to rewrite the call to hoist the logging check into the calling code). Essentially it's as fast as well can make it with our benchmarks (and we've tried all sorts of tricks).
Obviously if you can see any way to improve performance for disabled log statement I'd be very interested, but most of the win already comes from the API design in avoiding varags and auto-boxing (rather than super optimized code).
I'll close this for now since I don't plan on changing anything at the moment, but feel free to re-open if you have ideas.
from flogger.
Related Issues (20)
- How do you import the project into intellij? HOT 4
- Guice/ErrorInjectingConstructor from unit test HOT 7
- Benchmarks and String Creation HOT 19
- Architecture diagram for flogger
- Extensibility How to HOT 9
- Better document Flogger's status (active, maintained, in use at Google) HOT 5
- Support for java.util.ResourceBundle HOT 9
- Add tags from thread context to log HOT 17
- Use java.time.Duration for atMostEvery in LoggingApi. HOT 2
- Flag --incompatible_disable_starlark_host_transitions will break Flogger in Bazel 7.0 HOT 6
- Release schedule?
- Default JavaDocs generated for Flogger confusingly suggest it's deprecated (it isn't). HOT 2
- How to handle ScopedLoggingContext for new Threads / ThreadPools? HOT 1
- Provide a log writer to produce JSON log items according to Elastic Common Schema HOT 8
- I don't believe ScopedLoggingContexts should obviously accept null "no-op" arguments. HOT 5
- Incorrect caller details logged with Flogger in spring boot HOT 2
- Error: 'JavaInfo' value has no field or method 'transitive_deps' HOT 5
- Error: 'JavaInfo' value has no field or method 'transitive_deps' HOT 5
- Next release HOT 2
- ThreadlocalRandom instead of ThreadLocal HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flogger.