@@ -14,23 +14,20 @@ pub(crate) struct Alloc {
14
14
}
15
15
16
16
#[ derive( Debug ) ]
17
- struct AllocInner {
18
- pool : RefCell < Vec < Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > > > ,
19
- zeroes : Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > ,
17
+ struct AllocInner { }
18
+
19
+ thread_local ! {
20
+ static POOL : RefCell <Vec <Rc <[ f32 ; RENDER_QUANTUM_SIZE ] >>> = RefCell :: new( Vec :: with_capacity( 32 ) ) ;
21
+ static ZEROES : Rc <[ f32 ; RENDER_QUANTUM_SIZE ] > = Rc :: new( [ 0. ; RENDER_QUANTUM_SIZE ] ) ;
20
22
}
21
23
22
24
impl Alloc {
23
25
pub fn with_capacity ( n : usize ) -> Self {
24
26
let pool: Vec < _ > = ( 0 ..n) . map ( |_| Rc :: new ( [ 0. ; RENDER_QUANTUM_SIZE ] ) ) . collect ( ) ;
25
- let zeroes = Rc :: new ( [ 0. ; RENDER_QUANTUM_SIZE ] ) ;
26
-
27
- let inner = AllocInner {
28
- pool : RefCell :: new ( pool) ,
29
- zeroes,
30
- } ;
27
+ POOL . set ( pool) ;
31
28
32
29
Self {
33
- inner : Rc :: new ( inner ) ,
30
+ inner : Rc :: new ( AllocInner { } ) ,
34
31
}
35
32
}
36
33
@@ -44,20 +41,20 @@ impl Alloc {
44
41
45
42
pub fn silence ( & self ) -> AudioRenderQuantumChannel {
46
43
AudioRenderQuantumChannel {
47
- data : Rc :: clone ( & self . inner . zeroes ) ,
44
+ data : ZEROES . with ( Rc :: clone) ,
48
45
alloc : Rc :: clone ( & self . inner ) ,
49
46
}
50
47
}
51
48
52
49
#[ cfg( test) ]
53
50
pub fn pool_size ( & self ) -> usize {
54
- self . inner . pool . borrow ( ) . len ( )
51
+ POOL . with_borrow ( |p| p . len ( ) )
55
52
}
56
53
}
57
54
58
55
impl AllocInner {
59
56
fn allocate ( & self ) -> Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > {
60
- if let Some ( rc) = self . pool . borrow_mut ( ) . pop ( ) {
57
+ if let Some ( rc) = POOL . with_borrow_mut ( |p| p . pop ( ) ) {
61
58
// reuse from pool
62
59
rc
63
60
} else {
@@ -67,9 +64,7 @@ impl AllocInner {
67
64
}
68
65
69
66
fn push ( & self , data : Rc < [ f32 ; RENDER_QUANTUM_SIZE ] > ) {
70
- self . pool
71
- . borrow_mut ( ) // infallible when single threaded
72
- . push ( data) ;
67
+ POOL . with_borrow_mut ( |p| p. push ( data) ) ;
73
68
}
74
69
}
75
70
@@ -107,7 +102,7 @@ impl AudioRenderQuantumChannel {
107
102
///
108
103
/// If this function returns false, it is still possible for all samples to be zero.
109
104
pub ( crate ) fn is_silent ( & self ) -> bool {
110
- Rc :: ptr_eq ( & self . data , & self . alloc . zeroes )
105
+ ZEROES . with ( |z| Rc :: ptr_eq ( & self . data , z ) )
111
106
}
112
107
113
108
/// Sum two channels
@@ -121,7 +116,7 @@ impl AudioRenderQuantumChannel {
121
116
122
117
pub ( crate ) fn silence ( & self ) -> Self {
123
118
Self {
124
- data : Rc :: clone ( & self . alloc . zeroes ) ,
119
+ data : ZEROES . with ( Rc :: clone) ,
125
120
alloc : Rc :: clone ( & self . alloc ) ,
126
121
}
127
122
}
@@ -152,7 +147,7 @@ impl AsRef<[f32]> for AudioRenderQuantumChannel {
152
147
impl std:: ops:: Drop for AudioRenderQuantumChannel {
153
148
fn drop ( & mut self ) {
154
149
if Rc :: strong_count ( & self . data ) == 1 {
155
- let zeroes = Rc :: clone ( & self . alloc . zeroes ) ;
150
+ let zeroes = ZEROES . with ( Rc :: clone) ;
156
151
let rc = std:: mem:: replace ( & mut self . data , zeroes) ;
157
152
self . alloc . push ( rc) ;
158
153
}
0 commit comments