From be460660b0a05fa0b13b21daa70f9ad934c14ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 21 Apr 2022 19:23:26 +0200 Subject: [PATCH] Add explanatory comments & simplity interval comparison operator --- include/tiny-cuda-nn/gpu_memory.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/tiny-cuda-nn/gpu_memory.h b/include/tiny-cuda-nn/gpu_memory.h index dcfbf14b..e204e05e 100644 --- a/include/tiny-cuda-nn/gpu_memory.h +++ b/include/tiny-cuda-nn/gpu_memory.h @@ -387,13 +387,10 @@ struct Interval { size_t start, end; bool operator<(const Interval& other) const { - if (end < other.end) { - return true; - } else if (end == other.end) { - return start < other.start; - } else { - return false; - } + // This operator is used to sort non-overlapping intervals. Since intervals + // may be empty, the second half of the following expression is required to + // resolve ambiguity when `end` of adjacent empty intervals is equal. + return end < other.end || (end == other.end && start < other.start); } bool overlaps(const Interval& other) const { @@ -503,6 +500,9 @@ class GPUMemoryArena { } size_t start = best_candidate->start; + + // Note: the += operator can turn `best_candidate` into an empty interval, which is fine because it will + // be absorbed into adjacent free intervals in later calls to `merge_adjacent_intervals`. m_allocated_intervals[start] = best_candidate->start += n_bytes; enlarge(size());