Github user HuangWHWHW commented on a diff in the pull request: https://github.com/apache/flink/pull/1118#discussion_r39232369 --- Diff: flink-runtime/src/main/java/org/apache/flink/runtime/operators/sort/UnilateralSortMerger.java --- @@ -1525,34 +1525,41 @@ protected final void disposeSortBuffers(boolean releaseMemory) { final List<MemorySegment> allReadBuffers, final List<MemorySegment> writeBuffers) throws IOException { - final double numMerges = Math.ceil(channelIDs.size() / ((double) this.maxFanIn)); - final int channelsToMergePerStep = (int) Math.ceil(channelIDs.size() / numMerges); - + // A channel list with length maxFanIn<sup>i</sup> can be merged to maxFanIn files in i-1 rounds where every merge + // is a full merge with maxFanIn input channels. A partial round includes merges with fewer than maxFanIn + // inputs. It is most efficient to perform the partial round first. + final double scale = Math.ceil(Math.log(channelIDs.size()) / Math.log(this.maxFanIn)) - 1; + + final int numStart = channelIDs.size(); + final int numEnd = (int) Math.pow(this.maxFanIn, scale); + + final int numMerges = (int) Math.ceil((numStart - numEnd) / (double) (this.maxFanIn - 1)); --- End diff -- Are you sure the `this.maxFanIn` will never equal 1(avoid dividing 0)?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---