Skip to content

Commit 05e496a

Browse files
authored
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()
1 parent db60f78 commit 05e496a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.nio.ByteBuffer;
5252
import java.nio.FloatBuffer;
5353
import java.nio.IntBuffer;
54+
import java.util.ArrayDeque;
5455
import java.util.ArrayList;
5556
import java.util.logging.Level;
5657
import java.util.logging.Logger;
@@ -81,7 +82,7 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
8182
private int[] channels; // OpenAL source IDs
8283
private AudioSource[] channelSources; // jME source associated with each channel
8384
private int nextChannelIndex = 0; // Next available channel index
84-
private final ArrayList<Integer> freeChannels = new ArrayList<>(); // Pool of freed channels
85+
private final ArrayDeque<Integer> freeChannels = new ArrayDeque<>(); // Pool of freed channels
8586

8687
// Listener and environment
8788
private Listener listener;
@@ -793,7 +794,7 @@ private void applyListenerVolume(Listener listener) {
793794

794795
private int newChannel() {
795796
if (!freeChannels.isEmpty()) {
796-
return freeChannels.remove(0);
797+
return freeChannels.removeFirst();
797798
} else if (nextChannelIndex < channels.length) {
798799
return nextChannelIndex++;
799800
} else {
@@ -906,7 +907,7 @@ private boolean fillStreamingSource(int sourceId, AudioStream stream, boolean lo
906907
for (int i = 0; i < processed; i++) {
907908
int buffer;
908909

909-
ib.position(0).limit(1);
910+
ib.clear().limit(1);
910911
al.alSourceUnqueueBuffers(sourceId, 1, ib);
911912
buffer = ib.get(0);
912913

@@ -931,7 +932,7 @@ private boolean fillStreamingSource(int sourceId, AudioStream stream, boolean lo
931932
}
932933

933934
if (filled) {
934-
ib.position(0).limit(1);
935+
ib.clear().limit(1);
935936
ib.put(0, buffer);
936937
al.alSourceQueueBuffers(sourceId, 1, ib);
937938
// At least one buffer enqueued = success.
@@ -974,7 +975,7 @@ private void attachStreamToSource(int sourceId, AudioStream stream, boolean loop
974975
}
975976

976977
if (filled) {
977-
ib.position(0).limit(1);
978+
ib.clear().limit(1);
978979
ib.put(id).flip();
979980
al.alSourceQueueBuffers(sourceId, 1, ib);
980981
success = true;

0 commit comments

Comments
 (0)