Skip to content

Commit 2c3dcdc

Browse files
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

3 files changed

+7
-7
lines changed

lib/appsignal/check_in/scheduler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule Appsignal.CheckIn.Scheduler do
120120
end
121121

122122
@impl true
123-
def terminate(_reason, %{events: events}) when length(events) > 0 do
123+
def terminate(_reason, %{events: [_ | _] = events}) do
124124
# If any events are stored, attempt to transmit them before the
125125
# process is terminated.
126126
handle_continue({:transmit, events}, initial_state())

test/appsignal/error_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule Appsignal.ErrorTest do
2121
test "format's the error's stack trace", %{metadata: metadata} do
2222
{_name, _message, stack} = metadata
2323
assert is_list(stack)
24-
assert length(stack) > 0
24+
refute Enum.empty?(stack)
2525
assert Enum.all?(stack, &is_binary(&1))
2626
end
2727
end
@@ -46,7 +46,7 @@ defmodule Appsignal.ErrorTest do
4646
test "format's the error's stack trace", %{metadata: metadata} do
4747
{_name, _message, stack} = metadata
4848
assert is_list(stack)
49-
assert length(stack) > 0
49+
refute Enum.empty?(stack)
5050
assert Enum.all?(stack, &is_binary(&1))
5151
end
5252
end
@@ -73,7 +73,7 @@ defmodule Appsignal.ErrorTest do
7373
test "format's the error's stack trace", %{metadata: metadata} do
7474
{_name, _message, stack} = metadata
7575
assert is_list(stack)
76-
assert length(stack) > 0
76+
refute Enum.empty?(stack)
7777
assert Enum.all?(stack, &is_binary(&1))
7878
end
7979
end
@@ -101,7 +101,7 @@ defmodule Appsignal.ErrorTest do
101101
test "format's the error's stack trace", %{metadata: metadata} do
102102
{_name, _message, stack} = metadata
103103
assert is_list(stack)
104-
assert length(stack) > 0
104+
refute Enum.empty?(stack)
105105
assert Enum.all?(stack, &is_binary(&1))
106106
end
107107
end
@@ -126,7 +126,7 @@ defmodule Appsignal.ErrorTest do
126126
test "format's the error's stack trace", %{metadata: metadata} do
127127
{_name, _message, stack} = metadata
128128
assert is_list(stack)
129-
assert length(stack) > 0
129+
refute Enum.empty?(stack)
130130
assert Enum.all?(stack, &is_binary(&1))
131131
end
132132
end

test/appsignal/stacktrace_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule Appsignal.StacktraceTest do
1414
end
1515

1616
test "does not return an empty list", %{stack: stack} do
17-
assert length(stack) > 0
17+
refute Enum.empty?(stack)
1818
end
1919

2020
test "returns a stacktrace containing the error", %{

0 commit comments

Comments
 (0)