taiyang-li commented on code in PR #2048:
URL: https://github.com/apache/orc/pull/2048#discussion_r1859761024


##########
c++/src/io/Cache.cc:
##########
@@ -0,0 +1,172 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cassert>
+
+#include "Cache.hh"
+
+namespace orc {
+
+  std::vector<ReadRange> ReadRangeCombiner::coalesce(std::vector<ReadRange> 
ranges) const {
+    if (ranges.empty()) {
+      return ranges;
+    }
+
+    // Remove zero-sized ranges
+    auto end = std::remove_if(ranges.begin(), ranges.end(),
+                              [](const ReadRange& range) { return range.length 
== 0; });
+    // Sort in position order
+    std::sort(ranges.begin(), end, [](const ReadRange& a, const ReadRange& b) {
+      return a.offset != b.offset ? a.offset < b.offset : a.length > b.length;
+    });
+
+    // Remove ranges that overlap 100%
+    std::vector<ReadRange> uniqueRanges;
+    uniqueRanges.reserve(ranges.size());
+    for (auto it = ranges.begin(); it != end; ++it) {
+      if (uniqueRanges.empty() || !uniqueRanges.back().contains(*it)) {
+        uniqueRanges.push_back(*it);
+      }
+    }
+    ranges = std::move(uniqueRanges);
+
+    // Skip further processing if ranges is empty after removing zero-sized 
ranges.
+    if (ranges.empty()) {
+      return ranges;
+    }
+
+#ifndef NDEBUG
+    for (size_t i = 0; i < ranges.size() - 1; ++i) {
+      const auto& left = ranges[i];
+      const auto& right = ranges[i + 1];
+      assert(left.offset < right.offset);
+      assert(!left.contains(right));
+    }
+#endif
+
+    std::vector<ReadRange> coalesced;
+
+    auto itr = ranges.begin();
+    // Ensure ranges is not empty.
+    assert(itr <= ranges.end());
+
+    // Start of the current coalesced range and end (exclusive) of previous 
range.
+    // Both are initialized with the start of first range which is a 
placeholder value.
+    uint64_t coalescedStart = itr->offset;
+    uint64_t coalescedEnd = coalescedStart + itr->length;
+
+    for (++itr; itr < ranges.end(); ++itr) {

Review Comment:
   It is difficult keep the same semantics if we merge the 3 loops. Later loops 
rely on previous loops, and sometimes multiple elements are processed at one 
time. Maybe you are right, I just personally think it is not worth putting 
efforts in a performance insensitive place. Let's keep it as it was. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to