Skip to content

Commit 2fc330f

Browse files
TD5meta-codesync[bot]
authored andcommitted
Improve test_runner performance: O(n²) → O(n) test grouping and fix ct_hooks collection
Summary: Replace the O(n²) add_or_append/2 function in get_requested_tests/1 with a map-based accumulator that preserves insertion order in O(n) time. Also fix getCtHook/3 to use lists:partition/2 instead of lists:splitwith/2, which would miss non-contiguous ct_hooks entries in the options list. Reviewed By: jcpetruzza Differential Revision: D95046286 fbshipit-source-id: c50aba5cf10a2271fd301ac5be12754086253a9e
1 parent 2a2f265 commit 2fc330f

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

prelude/erlang/common_test/test_binary/src/test_runner.erl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,28 +370,21 @@ g1.t1, g2.t2, g1.t2, g1.t3, g2.t2, we produce:
370370
""".
371371
-spec get_requested_tests([#ct_test{}]) -> [{[atom()], [atom()]}].
372372
get_requested_tests(Tests) ->
373-
lists:foldl(
374-
fun(Test, List) ->
373+
{TestMap, RevOrderedKeys} = lists:foldl(
374+
fun(Test, {Map, Keys}) ->
375375
Groups = Test#ct_test.groups,
376-
add_or_append(List, {Groups, Test#ct_test.test_name})
376+
TestName = Test#ct_test.test_name,
377+
case Map of
378+
#{Groups := Existing} ->
379+
{Map#{Groups => [TestName | Existing]}, Keys};
380+
_ ->
381+
{Map#{Groups => [TestName]}, [Groups | Keys]}
382+
end
377383
end,
378-
[],
384+
{#{}, []},
379385
Tests
380-
).
381-
382-
-spec add_or_append(list({K, list(V)}), {K, V}) -> list({K, list(V)}).
383-
add_or_append(List, {Key, Value}) ->
384-
List0 = lists:map(
385-
fun
386-
({Key0, Value0}) when Key0 =:= Key -> {Key0, lists:append(Value0, [Value])};
387-
(Other) -> Other
388-
end,
389-
List
390386
),
391-
case List0 =:= List of
392-
true -> lists:append(List0, [{Key, [Value]}]);
393-
false -> List0
394-
end.
387+
[{Key, lists:reverse(maps:get(Key, TestMap))} || Key <- lists:reverse(RevOrderedKeys)].
395388

396389
-doc """
397390
Built the test_spec selecting the requested tests and
@@ -435,7 +428,7 @@ Collect all the ct_hooks entries provided by the user, and add the cth_tpx hook
435428
CtHooks :: {ct_hooks, [term()]},
436429
CtOpts1 :: [term()].
437430
getCtHook(CtOpts0, ResultOutput, ProgressLineFingerprint) ->
438-
{CtHooksOpts, CtOpts1} = lists:splitwith(
431+
{CtHooksOpts, CtOpts1} = lists:partition(
439432
fun
440433
({ct_hooks, _}) -> true;
441434
(_) -> false

0 commit comments

Comments
 (0)