Skip to content

Optimisations and cleanups#173

Merged
NelsonVides merged 3 commits into
masterfrom
optimisations_and_cleanups
Mar 11, 2025
Merged

Optimisations and cleanups#173
NelsonVides merged 3 commits into
masterfrom
optimisations_and_cleanups

Conversation

@NelsonVides

@NelsonVides NelsonVides commented Mar 3, 2025

Copy link
Copy Markdown
Member

This PR proposes a miscellaneous of updates and improvements to the repo. Commits are well organised by topics and with good descriptions, so that'd be the best way to review the whole thing, as some changes are not related to each other – but are some times too small to be moved to their own PR, which I could easily do if any of you would prefer me to do anyways.


EDIT: #173 (comment)

@NelsonVides NelsonVides force-pushed the optimisations_and_cleanups branch from b29b665 to 4041d2b Compare March 3, 2025 12:00
@codecov

codecov Bot commented Mar 3, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
src/model/prometheus_model_helpers.erl 94.25% <100.00%> (-0.07%) ⬇️
src/prometheus_buckets.erl 97.29% <100.00%> (-0.08%) ⬇️
src/prometheus_sup.erl 77.41% <100.00%> (-0.71%) ⬇️
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@NelsonVides NelsonVides force-pushed the optimisations_and_cleanups branch 2 times, most recently from 90a3ec7 to 32492a9 Compare March 3, 2025 12:10
@NelsonVides NelsonVides marked this pull request as ready for review March 3, 2025 12:16

@onno-vos-dev onno-vos-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't had the time to look through all commits yet but sharing these two review comments as a start

Comment thread src/metrics/prometheus_counter.erl Outdated
-define(ISUM_POS, 2).
-define(FSUM_POS, 3).
-define(WIDTH, 16).
-define(WIDTH, erlang:system_info(schedulers_online)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of fixing this to be schedulers_online we can set this up as an application:get_env(<application>, <some_name>, 16) so the original remains preserved but we open up the loophole for setting a higher value.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue is that schedulers_online can change value dynamically at runtime.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Should use just schedulers, as that can't change at runtime.

On the other hand I'm not sure it'd help to make it configurable, currently the algorithm that chooses the key would do X band (?WIDTH - 1), so most values would mean skewing the distribution of schedulers to each key and some keys would see more contention than others. Sensibly, if we're keying on the scheduler id, the only values that make sense are multiples (or halves) of the number of schedulers.

Comment thread src/metrics/prometheus_histogram.erl Outdated
-define(FSUM_POS, 4).
-define(BUCKETS_START, 5).
-define(WIDTH, 16).
-define(WIDTH, erlang:system_info(schedulers_online)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of fixing this to be schedulers_online we can set this up as an application:get_env(<application>, <some_name>, 16) so the original remains preserved but we open up the loophole for setting a higher value.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution has the same issue as @mikpe mentioned above, we don't want to let users change that value dynamically. another issue with defaults - they are rarely changed :)

@NelsonVides

Copy link
Copy Markdown
Member Author

I guess this PR is too big to review, so I've split it into #175 for the uncontroversial, #176 for the most convoluted, and left here maybe the most interesting changes.

@NelsonVides NelsonVides force-pushed the optimisations_and_cleanups branch from eb19b61 to e2c2b50 Compare March 9, 2025 09:33
@NelsonVides NelsonVides requested a review from onno-vos-dev March 9, 2025 09:35
@lhoguin

lhoguin commented Mar 10, 2025

Copy link
Copy Markdown

About the change to use number of schedulers for the width, what if I have a very large number of schedulers, will it still work? Or is there a limit in ets?

Comment thread src/metrics/prometheus_counter.erl Outdated
-define(ISUM_POS, 2).
-define(FSUM_POS, 3).
-define(WIDTH, 16).
-define(WIDTH, erlang:system_info(schedulers)).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X band (?WIDTH - 1) - it seems that WIDTH is expected to be a power of 2

Comment thread src/metrics/prometheus_histogram.erl Outdated
-define(FSUM_POS, 4).
-define(BUCKETS_START, 5).
-define(WIDTH, 16).
-define(WIDTH, erlang:system_info(schedulers)).

@DenysGonchar DenysGonchar Mar 10, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X band (?WIDTH - 1) - it seems that WIDTH is expected to be a power of 2

Note that the histogram table actually requires read concurrency,
as on every insert first parameters need to be looked up.

Otherwise this makes it more flexible to configure any table
independently and precisely, may the need arise.
Examine the condition inlined in the helper instead of passing a
function object. Also ensure that the entry point has checked the types,
when looping on the inner function, the JIT compiler will know already
that the inputs are lists and numbers, skipping dynamic type checking on
every iteration. The `lists` module implements such optimisation
extensively.
@NelsonVides

Copy link
Copy Markdown
Member Author

On @lhoguin comment, I'd say bigger width should just make reads slower as there'd be more rows to read, but inserts should be faster as there'd be less contention. On the other hand, these are educated guesses, not measured points.

On @DenysGonchar comment, actually that's a very good point. In general, what we want is to ensure the distribution of values is as uniform as possible across schedulers, I just ran some checks and it seems like indeed my proposal wouldn't be good enough either.

For that matter, I'm just taking this change out of this PR, will later on when I find time for it run some actual statistics about distributions, and benchmark different solutions. The rest of the changes still apply.

Thank you for your comments 🙇🏽

@NelsonVides NelsonVides force-pushed the optimisations_and_cleanups branch from e2c2b50 to 72ed85c Compare March 10, 2025 10:49

@DenysGonchar DenysGonchar left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me.

@NelsonVides NelsonVides merged commit fb5e95a into master Mar 11, 2025
@NelsonVides NelsonVides deleted the optimisations_and_cleanups branch March 11, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants