Browse Source

Prefer ArrayDeque over ArrayList

You should consider using ArrayDeque when:

You need a collection that efficiently supports adding and removing elements from both ends. This makes it ideal for implementing data structures like queues and stacks.
You frequently perform removals from the beginning of the collection.
see ALAudioRenderer.newChannel() and ALAudioRenderer.freeChannel()
Wyatt Gillette 4 months ago
parent
commit
05e496a87f

+ 6 - 5
jme3-core/src/main/java/com/jme3/audio/openal/ALAudioRenderer.java

@@ -51,6 +51,7 @@ import com.jme3.util.NativeObjectManager;
 import java.nio.ByteBuffer;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 import java.nio.IntBuffer;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.logging.Level;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.Logger;
@@ -81,7 +82,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
     private int[] channels; // OpenAL source IDs
     private int[] channels; // OpenAL source IDs
     private AudioSource[] channelSources; // jME source associated with each channel
     private AudioSource[] channelSources; // jME source associated with each channel
     private int nextChannelIndex = 0; // Next available channel index
     private int nextChannelIndex = 0; // Next available channel index
-    private final ArrayList<Integer> freeChannels = new ArrayList<>(); // Pool of freed channels
+    private final ArrayDeque<Integer> freeChannels = new ArrayDeque<>(); // Pool of freed channels
 
 
     // Listener and environment
     // Listener and environment
     private Listener listener;
     private Listener listener;
@@ -793,7 +794,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
 
 
     private int newChannel() {
     private int newChannel() {
         if (!freeChannels.isEmpty()) {
         if (!freeChannels.isEmpty()) {
-            return freeChannels.remove(0);
+            return freeChannels.removeFirst();
         } else if (nextChannelIndex < channels.length) {
         } else if (nextChannelIndex < channels.length) {
             return nextChannelIndex++;
             return nextChannelIndex++;
         } else {
         } else {
@@ -906,7 +907,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
         for (int i = 0; i < processed; i++) {
         for (int i = 0; i < processed; i++) {
             int buffer;
             int buffer;
 
 
-            ib.position(0).limit(1);
+            ib.clear().limit(1);
             al.alSourceUnqueueBuffers(sourceId, 1, ib);
             al.alSourceUnqueueBuffers(sourceId, 1, ib);
             buffer = ib.get(0);
             buffer = ib.get(0);
 
 
@@ -931,7 +932,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
             }
             }
 
 
             if (filled) {
             if (filled) {
-                ib.position(0).limit(1);
+                ib.clear().limit(1);
                 ib.put(0, buffer);
                 ib.put(0, buffer);
                 al.alSourceQueueBuffers(sourceId, 1, ib);
                 al.alSourceQueueBuffers(sourceId, 1, ib);
                 // At least one buffer enqueued = success.
                 // At least one buffer enqueued = success.
@@ -974,7 +975,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
             }
             }
 
 
             if (filled) {
             if (filled) {
-                ib.position(0).limit(1);
+                ib.clear().limit(1);
                 ib.put(id).flip();
                 ib.put(id).flip();
                 al.alSourceQueueBuffers(sourceId, 1, ib);
                 al.alSourceQueueBuffers(sourceId, 1, ib);
                 success = true;
                 success = true;