bonfire-networks / bonfire-app

Bonfire - tend to your digital life in community. Customise and host your own online space and control your experience at the most granular level.
https://bonfirenetworks.org
GNU Affero General Public License v3.0
585 stars 43 forks source link

optimise widely used functions such as `e`, config, settings, Extend, logging... #977

Open mayel opened 3 months ago

mayel commented 3 months ago
Zurga commented 2 months ago

Based some early profiling done on the render_feed function in Bonfire.UI.Social.Benchmark it seems that Bonfire.Common.Extend.application_loaded? could also use optimization. Please note that this is in the dev environment.

This is the top 100 ish most called and their times:

Profile results of #PID<0.1758.0>
#                                                                                              CALLS    %    TIME µS/CALL
String.Tokenizer.validate/2                                                                    18498 0.21   53248    2.88
:unicode.characters_to_binary/2                                                                25127 0.22   54219    2.16
Inspect.Algebra.color/3                                                                        19106 0.22   54888    2.87
IO.ANSI.do_format/5                                                                            34032 0.22   54911    1.61
:elixir_config.identifier_tokenizer/0                                                          19074 0.22   56042    2.94
:persistent_term.get/2                                                                         33032 0.22   56267    1.70
:erlang.atom_to_list/1                                                                         33076 0.22   56402    1.71
String.Tokenizer.tokenize/1                                                                    19074 0.23   57681    3.02
Untangle.format_location/2                                                                      9729 0.23   57721    5.93
:erlang.universaltime_to_localtime/1                                                            2874 0.23   57838   20.12
Path.split/1                                                                                   20194 0.23   58105    2.88
:filename.unix_splitb/1                                                                        20194 0.24   59728    2.96
:erlang.get_module_info/2                                                                      10805 0.24   59852    5.54
:filename.split/1                                                                              20850 0.24   60368    2.90
Path.pathtype/2                                                                                42680 0.24   60713    1.42
Path.unix_pathtype/1                                                                           42680 0.25   63007    1.48
:application.get_env/3                                                                         22132 0.25   63604    2.87
Application.get_env/3                                                                          22132 0.26   66008    2.98
Macro.classify_atom/1                                                                          23347 0.26   66158    2.83
:application_controller.get_env/2                                                              22132 0.26   66355    3.00
:application_controller.get_application_module/1                                                2704 0.27   67090   24.81
List.to_string/1                                                                               23690 0.27   67616    2.85
String.decrement/1                                                                             48124 0.27   67994    1.41
anonymous fn/2 in Path.Wildcard.list_dir/1                                                     51672 0.28   69481    1.34
:unicode.characters_to_binary/1                                                                25127 0.29   71755    2.86
:binary.split/3                                                                                27979 0.29   73057    2.61
:ets.lookup/2                                                                                  29464 0.29   73637    2.50
Untangle.format_application_location/3                                                          9729 0.29   73753    7.58
:filelib.wrap_escapes/1                                                                        30616 0.30   76002    2.48
:logger.compare_levels/2                                                                       17920 0.30   76094    4.25
Exception.format_mfa/3                                                                          9729 0.30   76477    7.86
:application_controller.loaded_applications/0                                                   1395 0.30   76510   54.85
anonymous fn/2 in ExULID.Crockford.decoding/0                                                  49920 0.31   77903    1.56
Logger.compare_levels/2                                                                        17920 0.31   78202    4.36
:maps.next/1                                                                                   47503 0.31   79005    1.66
Untangle.log_level?/1                                                                          17920 0.32   79735    4.45
Access.get/2                                                                                   57572 0.32   81314    1.41
:filelib.convert_escapes/1                                                                     30956 0.32   81589    2.64
:filename.do_flatten/2                                                                         30956 0.33   81659    2.64
anonymous fn/2 in Bonfire.Common.Enums.filter_empty/2                                          30790 0.34   86071    2.80
Phoenix.HTML.Engine.html_escape/5                                                              63979 0.36   91068    1.42
:erlang.atom_to_binary/2                                                                       58951 0.39   97739    1.66
Path.relative_to_unforced/3                                                                    67771 0.40   99994    1.48
anonymous fn/2 in Bonfire.Common.Enums.filter_empty_enum/2                                     71367 0.40  100748    1.41
Code.Identifier.escape_map/1                                                                   75931 0.43  106790    1.41
Path.do_absname_join/4                                                                         75744 0.43  107513    1.42
Code.Identifier.escape_char/1                                                                  75613 0.44  109431    1.45
:file.call/2                                                                                   15585 0.44  111344    7.14
:gen.do_call/4                                                                                 16173 0.45  112341    6.95
:filelib."-do_wildcard_4/3-lc$^0/1-0-"/3                                                       41828 0.46  114701    2.74
:lists.reverse/2                                                                               77462 0.46  115672    1.49
:binary.last/1                                                                                 80864 0.46  116413    1.44
Path.relative/2                                                                                41296 0.46  116584    2.82
Inspect.Algebra.fits?/4                                                                        83984 0.50  126778    1.51
Path.join/1                                                                                    50453 0.51  127858    2.53
Path.relative_to/3                                                                             10097 0.52  129821   12.86
anonymous fn/3 in String.starts_with?/2                                                        95900 0.53  132765    1.38
:erlang.system_info/1                                                                          90774 0.54  136467    1.50
String.recur_printable?/2                                                                      48599 0.55  138814    2.86
Enum.with_index_list/2                                                                         51539 0.57  143436    2.78
Macro.valid_alias_piece?/1                                                                     58642 0.58  145846    2.49
Path.major_os_type/0                                                                           54161 0.61  152977    2.82
Access.get/3                                                                                   57572 0.62  154842    2.69
Macro.inspect_atom/2                                                                           35425 0.64  161222    4.55
:erlang.atom_to_binary/1                                                                       58951 0.66  165871    2.81
:lists.keyfind/3                                                                              118986 0.69  172193    1.45
:maps.filter_1/2                                                                               36603 0.72  180621    4.93
Path.do_join/3                                                                                 40720 0.73  182960    4.49
Keyword.get/3                                                                                  65113 0.73  183921    2.82
IO.chardata_to_string/1                                                                       132024 0.74  186094    1.41
Macro.operator?/2                                                                              66152 0.75  187353    2.83
Inspect.Algebra.format/3                                                                       85738 0.78  195693    2.28
:lists.reverse/1                                                                               77666 0.83  207534    2.67
:filename.join1/4                                                                             163772 0.88  220446    1.35
:file.file_name_1/2                                                                            85228 0.88  221886    2.60
Path.join/2                                                                                    40720 0.91  228668    5.62
anonymous fn/1 in Bonfire.Common.Enums.filter_empty_enum/2                                     71367 0.92  230987    3.24
Path.expand_split/2                                                                           167277 0.97  244845    1.46
Bonfire.Common.Enums.filter_empty/2                                                           152957 0.98  246750    1.61
Path.remove_dir_sep/2                                                                          80864 0.98  247315    3.06
:filelib.match_part/2                                                                         186120 1.00  251629    1.35
String.Tokenizer.continue/6                                                                   176855 1.00  252346    1.43
:os.type/0                                                                                     90559 1.01  252574    2.79
:filelib.do_star/2                                                                             99716 1.01  253225    2.54
Enum.predicate_list/3                                                                         100140 1.05  262916    2.63
Code.Identifier.escape/5                                                                       77987 1.28  322827    4.14
Enum.filter_list/2                                                                             91571 1.34  336849    3.68
Macro.inner_classify/1                                                                         33076 1.36  340886   10.31
Enum."-reduce/3-lists^foldl/2-0-"/3                                                           145533 1.53  384174    2.64
Macro.trim_leading_while_valid_identifier/1                                                   304097 1.71  429696    1.41
Enum."-map/2-lists^map/1-1-"/2                                                                123557 1.83  459937    3.72
anonymous fn/3 in Bonfire.Common.Extend.application_loaded?/1                                 344633 1.94  487859    1.42
:filename."-unix_splitb/1-lc$^0/1-0-"/1                                                       187507 1.99  499377    2.66
:ets.select/2                                                                                   1395 2.08  522875  374.82
:ets.match/2                                                                                    3072 2.77  695497  226.40
Enumerable.List.reduce/3                                                                      346728 3.86  970856    2.80
:lists.member/2                                                                               754005 4.41 1109064    1.47
:application_controller.get_application_module/2                                              715723 8.03 2016893    2.82
mayel commented 2 months ago

Ah specifically Application.loaded_applications() and Application.get_application(module) seem slow, so we can start by simply caching the outputs (we have Bonfire.Common.Cache.maybe_apply_cached/3 as a wrapper to cachex

Zurga commented 2 months ago

Yeah that would be a great improvement

mayel commented 2 months ago

@Zurga done! curious to see the difference...

mayel commented 2 months ago

After a chat with @Zurga I've also benchmarked (in my local dev environment) how different Logger configs affect performance (either changing the level or setting config :logger, backends: [] to disable printing logs in console):

1- compiled with error level, but not printing:

1- compiled with error level, but not printing

2- compiled with error level, and printing:

2- compiled with error level, and printing

3- compiled with info level, but not printing:

3- compiled with info level, but not printing

4- compiled with info level, but only printing error:

4- compiled with info level, but only printing error

5- compiled with info level, and printing:

5- compiled with info level, and printing

6- compiled with debug level, but only printing info:

6- compiled with debug level, but only printing info

6b- compiled with debug level, but not printing:

6b- compiled with debug level, but not printing

7- compiled with debug level, and printing:

7- compiled with debug level, and printing