kip
I’m after ideas and suggestions on how to emit a message when a certain condition is met at runtime - but only once, the first time the condition is met.
My use case is this: in ex_cldr_numbers a number format like #,###.## is compiled into meta data at compile time for all known formats (there are a lot of them in CLDR).
Since formats can be arbitrary they can also be compiled at runtime and then processed. That’s expensive (doubles the execution time for formatting a number in typical cases). And since number formatting is often in a tight loop its something to be avoided.
Therefore I want to emit a message (probably via Logger.debug) when a format is going to be compiled. But only the first time that format is being compiled. Otherwise the risk is that the log output becomes far too noisy to be useful. Additional formats can be defined in configuration and hence pre-compiled, therefore knowing which ones aren’t pre-compiled helps decide if they should be configured.
I can imagine some scaffolding involving an ets table and a counter based upon the format (which is a binary). And in development mode that’s probably ok, the overhead won’t be much. But I’m not sure that’s a good production solution.
This warn_once idea has cropped up as an idea for me in a few other cases so I figure someone may already have a solid approach.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
As a preliminary question, have you considered generating modules and injecting the
warn_oncefunctionality in theirbefore_compilecallback? That should piggyback on the compiler’s ability to only compile those modules once (at runtime when you choose).kip
@dimitarvp thanks for chiming in. I try hard to do as much work at compile time. Here is the module in question (edited for brevity). Its perfectly acceptable to provide an arbitrary format string at runtime, the only issue being the potential of this being a performance bottleneck. It might not be in some use cases, but it will in others.
I could:
dimitarvp
Yes. I don’t think it’s a big deal though, they are not going to have source files and won’t get in anybody’s face. Don’t overthink it, I’d encourage you to try just this approach — although I’m not aware of all your requirements so it might turn out to be a suboptimal solution.
A combination of
:countersand:persistent_termcan work but it could be tedious to code around.Finally, telemetry actually sounds pretty appropriate for your use case but I’m not sure how could that help with only printing a warning once.
kip
In case anyone stumbles across this question in their own question, the following is suitable for me needs:
I wrap this at the calling site with a macro to make the inclusion of this generated code dependent on configuration, and whether
:persistent_termexists.dimitarvp
Good solution.
Changing
persistent_termvalue is, I hear, expensive, but since it’s done very rarely in this scenario, the added peace of mind is very worth it.