@@ -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 } ,
@@ -1216,12 +1216,10 @@ mod decl {
12161216 #[ pyclass( name = "product" , traverse) ]
12171217 #[ derive( Debug , PyPayload ) ]
12181218 struct PyItertoolsProduct {
1219- pools : Vec < Vec < PyObjectRef > > ,
1219+ pools : Vec < PyTupleRef > ,
12201220 #[ pytraverse( skip) ]
12211221 idxs : PyRwLock < Vec < usize > > ,
12221222 #[ pytraverse( skip) ]
1223- cur : AtomicCell < usize > ,
1224- #[ pytraverse( skip) ]
12251223 stop : AtomicCell < bool > ,
12261224 }
12271225
@@ -1256,12 +1254,19 @@ mod decl {
12561254 . filter ( |n| * n <= isize:: MAX as usize / size_of :: < usize > ( ) )
12571255 . ok_or_else ( || vm. new_overflow_error ( "repeat argument too large" ) ) ?;
12581256
1259- let mut single: Vec < Vec < PyObjectRef > > = Vec :: new ( ) ;
1260- for arg in iterables. iter ( ) {
1261- single. push ( arg. try_to_value ( vm) ?) ;
1257+ let mut single = Vec :: new ( ) ;
1258+ if repeat != 0 {
1259+ for arg in iterables. iter ( ) {
1260+ let pool = if let Some ( tuple) = arg. downcast_ref_if_exact :: < PyTuple > ( vm) {
1261+ tuple. to_owned ( )
1262+ } else {
1263+ PyTuple :: new_ref ( arg. try_to_value ( vm) ?, & vm. ctx )
1264+ } ;
1265+ single. push ( pool) ;
1266+ }
12621267 }
12631268
1264- let mut pools: Vec < Vec < PyObjectRef > > = Vec :: new ( ) ;
1269+ let mut pools: Vec < PyTupleRef > = Vec :: new ( ) ;
12651270 pools
12661271 . try_reserve_exact ( npools)
12671272 . map_err ( |_| vm. no_memory_error ( ) ) ?;
@@ -1276,60 +1281,41 @@ mod decl {
12761281 . map_err ( |_| vm. no_memory_error ( ) ) ?;
12771282 idxs. resize ( npools, 0 ) ;
12781283
1279- let l = pools. len ( ) ;
1284+ let empty = pools. iter ( ) . any ( |pool| pool . is_empty ( ) ) ;
12801285
12811286 Ok ( Self {
12821287 pools,
12831288 idxs : PyRwLock :: new ( idxs) ,
1284- cur : AtomicCell :: new ( l. wrapping_sub ( 1 ) ) ,
1285- stop : AtomicCell :: new ( false ) ,
1289+ stop : AtomicCell :: new ( empty) ,
12861290 } )
12871291 }
12881292 }
12891293
12901294 #[ pyclass( with( IterNext , Iterable , Constructor ) , flags( BASETYPE ) ) ]
12911295 impl PyItertoolsProduct {
1292- fn update_idxs ( & self , mut idxs : PyRwLockWriteGuard < ' _ , Vec < usize > > ) {
1293- if idxs. is_empty ( ) {
1294- self . stop . store ( true ) ;
1295- return ;
1296- }
1297-
1298- let cur = self . cur . load ( ) ;
1299- let lst_idx = & self . pools [ cur] . len ( ) - 1 ;
1300-
1301- if idxs[ cur] == lst_idx {
1302- if cur == 0 {
1303- self . stop . store ( true ) ;
1296+ fn update_idxs ( & self , idxs : & mut [ usize ] ) {
1297+ for ( index, pool) in idxs. iter_mut ( ) . zip ( & self . pools ) . rev ( ) {
1298+ * index += 1 ;
1299+ if * index < pool. len ( ) {
13041300 return ;
13051301 }
1306- idxs[ cur] = 0 ;
1307- self . cur . fetch_sub ( 1 ) ;
1308- self . update_idxs ( idxs) ;
1309- } else {
1310- idxs[ cur] += 1 ;
1311- self . cur . store ( idxs. len ( ) - 1 ) ;
1302+ * index = 0 ;
13121303 }
1304+ self . stop . store ( true ) ;
13131305 }
13141306 }
13151307
13161308 impl SelfIter for PyItertoolsProduct { }
13171309 impl IterNext for PyItertoolsProduct {
13181310 fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1311+ let mut idxs = zelf. idxs . write ( ) ;
13191312 // stop signal
13201313 if zelf. stop . load ( ) {
13211314 return Ok ( PyIterReturn :: StopIteration ( None ) ) ;
13221315 }
13231316
13241317 let pools = & zelf. pools ;
13251318
1326- for p in pools {
1327- if p. is_empty ( ) {
1328- return Ok ( PyIterReturn :: StopIteration ( None ) ) ;
1329- }
1330- }
1331-
1332- let idxs = zelf. idxs . write ( ) ;
13331319 let res = vm. ctx . new_tuple (
13341320 pools
13351321 . iter ( )
@@ -1338,7 +1324,7 @@ mod decl {
13381324 . collect ( ) ,
13391325 ) ;
13401326
1341- zelf. update_idxs ( idxs) ;
1327+ zelf. update_idxs ( & mut idxs) ;
13421328
13431329 Ok ( PyIterReturn :: Return ( res. into ( ) ) )
13441330 }
0 commit comments