aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorSimon Tatham <simon.tatham@arm.com>2024-10-08 08:42:03 +0100
committerGitHub <noreply@github.com>2024-10-08 08:42:03 +0100
commit58ef1eb06143f48629e8b904971ab014fc4bad39 (patch)
tree83d69cd4957f27a68fa9b6aff0d0dd84e4806b33 /clang/lib/CodeGen/CodeGenModule.cpp
parent975da028f78546729f0e78711a4b92107c92ec8e (diff)
downloadllvm-58ef1eb06143f48629e8b904971ab014fc4bad39.zip
llvm-58ef1eb06143f48629e8b904971ab014fc4bad39.tar.gz
llvm-58ef1eb06143f48629e8b904971ab014fc4bad39.tar.bz2
[libc] Bound the worst-case stack usage in qsort(). (#110849)
Previously, the Quicksort implementation was written in the obvious way: after each partitioning step, it explicitly recursed twice to sort the two sublists. Now it compares the two sublists' sizes, and recurses only to sort the smaller one. To handle the larger list it loops back round to the top of the function, so as to handle it within the existing stack frame. This means that every recursive call is handling a list at most half that of its caller. So the maximum recursive call depth is O(lg N). Otherwise, in Quicksort's bad cases where each partition step peels off a small constant number of array elements, the stack usage could grow linearly with the array being sorted, i.e. it might be Θ(N). I tested this code by manually constructing a List Of Doom that causes this particular quicksort implementation to hit its worst case, and confirming that it recursed very deeply in the old code and doesn't in the new code. But I haven't added that list to the test suite, because the List Of Doom has to be constructed in a way based on every detail of the quicksort algorithm (pivot choice and partitioning strategy), so it would silently stop being a useful regression test as soon as any detail changed.
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
0 files changed, 0 insertions, 0 deletions