Skip to content

Commit

Permalink
use indirect and direct (packed) radix sorting, which according to my…
Browse files Browse the repository at this point in the history
… measurements is around 10% faster
  • Loading branch information
douira committed Jan 7, 2025
1 parent 3af8680 commit c9d7191
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@ protected static int[] createIndexBuffer(int length) {

return indices;
}

protected static int[] extractIndices(long[] items) {
int[] indices = new int[items.length];
for (int i = 0; i < items.length; i++) {
indices[i] = (int) items[i];
}
return indices;
}

protected static long[] prepareItems(int[] keys) {
long[] items = new long[keys.length];
for (int i = 0; i < keys.length; i++) {
items[i] = (long) keys[i] << 32 | i;
}
return items;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2002-2017 Sebastiano Vigna
*
* Licensed 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.
*
*
*
* For the sorting and binary search code:
*
* Copyright (C) 1999 CERN - European Organization for Nuclear Research.
*
* Permission to use, copy, modify, distribute and sell this software and
* its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation. CERN makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without expressed or implied warranty.
*/
package net.caffeinemc.mods.sodium.client.util.sorting;


import it.unimi.dsi.fastutil.ints.IntArrays;

import java.util.Arrays;

public class MixedSort extends AbstractSort {
private static final int INDIRECT_SORT_THRESHOLD = 1000;

public static int[] mixedSort(int[] keys) {
// indirect sorting is faster for small arrays, packed direct sorting is faster for large arrays
if (keys.length <= INDIRECT_SORT_THRESHOLD) {
return indirectRadixSort(keys);
} else {
return packedUnstableSort(keys);
}
}

private static int[] indirectRadixSort(int[] keys) {
var indices = createIndexBuffer(keys.length);
IntArrays.radixSortIndirect(indices, keys, false);
return indices;
}

private static int[] packedUnstableSort(int[] keys) {
var items = prepareItems(keys);
Arrays.sort(items);
return extractIndices(items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ protected float getKey(Vector3f position) {
private static abstract class AbstractVertexSorter implements VertexSorting {
@Override
public final int[] sort(Vector3f[] positions) {
return this.mergeSort(positions);
}

private int[] mergeSort(Vector3f[] positions) {
final var keys = new float[positions.length];
final var keys = new int[positions.length];

for (int index = 0; index < positions.length; index++) {
keys[index] = this.getKey(positions[index]);
keys[index] = ~Float.floatToRawIntBits(this.getKey(positions[index]));
}

return MergeSort.mergeSort(keys);
return MixedSort.mixedSort(keys);
}

protected abstract float getKey(Vector3f object);
Expand Down

0 comments on commit c9d7191

Please sign in to comment.