From 727ef0e543b6fb6f33269712bf851d9ee516cff4 Mon Sep 17 00:00:00 2001 From: Mandeep Singh Grang Date: Tue, 24 Apr 2018 21:25:57 +0000 Subject: [PATCH] [docs] Add a note on non-deterministic sorting order of equal elements Reviewers: RKSimon, t.p.northover, dexonsmith Reviewed By: RKSimon Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D45831 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330773 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/CodingStandards.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/CodingStandards.rst b/docs/CodingStandards.rst index 1d631ccd92f..1010a4bf4af 100644 --- a/docs/CodingStandards.rst +++ b/docs/CodingStandards.rst @@ -826,6 +826,17 @@ As a rule of thumb, in case an ordered result is expected, remember to sort an unordered container before iteration. Or use ordered containers like vector/MapVector/SetVector if you want to iterate pointer keys. +Beware of non-deterministic sorting order of equal elements +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +std::sort uses a non-stable sorting algorithm in which the order of equal +elements is not guaranteed to be preserved. Thus using std::sort for a +container having equal elements may result in non-determinstic behavior. +To uncover such instances of non-determinism, LLVM has introduced a new +llvm::sort wrapper function. For an EXPENSIVE_CHECKS build this will randomly +shuffle the container before sorting. As a rule of thumb, always make sure to +use llvm::sort instead of std::sort. + Style Issues ============