@@ -7,7 +7,7 @@ mod decl {
77 VirtualMachine ,
88 builtins:: { PyGenericAlias , PyInt , PyList , PyTuple , PyTupleRef , PyType , PyTypeRef , int} ,
99 class:: PyClassDef ,
10- common:: lock:: { PyMutex , PyRwLock , PyRwLockWriteGuard } ,
10+ common:: lock:: { PyMutex , PyRwLock } ,
1111 convert:: ToPyObject ,
1212 function:: { ArgIndex , FuncArgs , NameIterables , PosArgs } ,
1313 protocol:: { PyIter , PyIterReturn , PyNumber } ,
@@ -1180,12 +1180,10 @@ mod decl {
11801180 #[ pyclass( name = "product" , traverse) ]
11811181 #[ derive( Debug , PyPayload ) ]
11821182 struct PyItertoolsProduct {
1183- pools : Vec < Vec < PyObjectRef > > ,
1183+ pools : Vec < PyTupleRef > ,
11841184 #[ pytraverse( skip) ]
11851185 idxs : PyRwLock < Vec < usize > > ,
11861186 #[ pytraverse( skip) ]
1187- cur : AtomicCell < usize > ,
1188- #[ pytraverse( skip) ]
11891187 stop : AtomicCell < bool > ,
11901188 }
11911189
@@ -1220,12 +1218,19 @@ mod decl {
12201218 . filter ( |n| * n <= isize:: MAX as usize / size_of :: < usize > ( ) )
12211219 . ok_or_else ( || vm. new_overflow_error ( "repeat argument too large" ) ) ?;
12221220
1223- let mut single: Vec < Vec < PyObjectRef > > = Vec :: new ( ) ;
1224- for arg in iterables. iter ( ) {
1225- single. push ( arg. try_to_value ( vm) ?) ;
1221+ let mut single = Vec :: new ( ) ;
1222+ if repeat != 0 {
1223+ for arg in iterables. iter ( ) {
1224+ let pool = if let Some ( tuple) = arg. downcast_ref_if_exact :: < PyTuple > ( vm) {
1225+ tuple. to_owned ( )
1226+ } else {
1227+ PyTuple :: new_ref ( arg. try_to_value ( vm) ?, & vm. ctx )
1228+ } ;
1229+ single. push ( pool) ;
1230+ }
12261231 }
12271232
1228- let mut pools: Vec < Vec < PyObjectRef > > = Vec :: new ( ) ;
1233+ let mut pools: Vec < PyTupleRef > = Vec :: new ( ) ;
12291234 pools
12301235 . try_reserve_exact ( npools)
12311236 . map_err ( |_| vm. no_memory_error ( ) ) ?;
@@ -1240,60 +1245,41 @@ mod decl {
12401245 . map_err ( |_| vm. no_memory_error ( ) ) ?;
12411246 idxs. resize ( npools, 0 ) ;
12421247
1243- let l = pools. len ( ) ;
1248+ let empty = pools. iter ( ) . any ( |pool| pool . is_empty ( ) ) ;
12441249
12451250 Ok ( Self {
12461251 pools,
12471252 idxs : PyRwLock :: new ( idxs) ,
1248- cur : AtomicCell :: new ( l. wrapping_sub ( 1 ) ) ,
1249- stop : AtomicCell :: new ( false ) ,
1253+ stop : AtomicCell :: new ( empty) ,
12501254 } )
12511255 }
12521256 }
12531257
12541258 #[ pyclass( with( IterNext , Iterable , Constructor ) , flags( BASETYPE ) ) ]
12551259 impl PyItertoolsProduct {
1256- fn update_idxs ( & self , mut idxs : PyRwLockWriteGuard < ' _ , Vec < usize > > ) {
1257- if idxs. is_empty ( ) {
1258- self . stop . store ( true ) ;
1259- return ;
1260- }
1261-
1262- let cur = self . cur . load ( ) ;
1263- let lst_idx = & self . pools [ cur] . len ( ) - 1 ;
1264-
1265- if idxs[ cur] == lst_idx {
1266- if cur == 0 {
1267- self . stop . store ( true ) ;
1260+ fn update_idxs ( & self , idxs : & mut [ usize ] ) {
1261+ for ( index, pool) in idxs. iter_mut ( ) . zip ( & self . pools ) . rev ( ) {
1262+ * index += 1 ;
1263+ if * index < pool. len ( ) {
12681264 return ;
12691265 }
1270- idxs[ cur] = 0 ;
1271- self . cur . fetch_sub ( 1 ) ;
1272- self . update_idxs ( idxs) ;
1273- } else {
1274- idxs[ cur] += 1 ;
1275- self . cur . store ( idxs. len ( ) - 1 ) ;
1266+ * index = 0 ;
12761267 }
1268+ self . stop . store ( true ) ;
12771269 }
12781270 }
12791271
12801272 impl SelfIter for PyItertoolsProduct { }
12811273 impl IterNext for PyItertoolsProduct {
12821274 fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1275+ let mut idxs = zelf. idxs . write ( ) ;
12831276 // stop signal
12841277 if zelf. stop . load ( ) {
12851278 return Ok ( PyIterReturn :: StopIteration ( None ) ) ;
12861279 }
12871280
12881281 let pools = & zelf. pools ;
12891282
1290- for p in pools {
1291- if p. is_empty ( ) {
1292- return Ok ( PyIterReturn :: StopIteration ( None ) ) ;
1293- }
1294- }
1295-
1296- let idxs = zelf. idxs . write ( ) ;
12971283 let res = vm. ctx . new_tuple (
12981284 pools
12991285 . iter ( )
@@ -1302,7 +1288,7 @@ mod decl {
13021288 . collect ( ) ,
13031289 ) ;
13041290
1305- zelf. update_idxs ( idxs) ;
1291+ zelf. update_idxs ( & mut idxs) ;
13061292
13071293 Ok ( PyIterReturn :: Return ( res. into ( ) ) )
13081294 }
0 commit comments