Commit 2c3dcdc
committed
Don't use length() as it's expensive
Replace expensive length/1 calls with more performant alternatives
to fix Credo warnings flagged in CI[1].
- In scheduler.ex: Use pattern matching [_ | _] instead of
length(events) > 0 in guard clause
- In test files: Replace assert length(stack) > 0 with
refute Enum.empty?(stack) for clearer intent and better performance
In Elixir, length/1 is O(n) because lists are implemented as linked
lists, requiring traversal of the entire list to count elements.
For emptiness checks, pattern matching against [] or [_ | _] is O(1)
and Enum.empty?/1 stops at the first element, making them much more
efficient than counting all elements just to compare with zero.
These changes maintain the same functionality while following Elixir
best practices for checking list emptiness.
[1]: https://github.com/appsignal/appsignal-elixir/actions/runs/19948488321/job/57203328408
[skip changeset]1 parent e9571dc commit 2c3dcdc
File tree
3 files changed
+7
-7
lines changed- lib/appsignal/check_in
- test/appsignal
3 files changed
+7
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
0 commit comments