@@ -10,6 +10,7 @@ use std::{
1010 path:: PathBuf ,
1111 process:: { Command , Stdio } ,
1212 str:: FromStr ,
13+ sync:: Mutex ,
1314} ;
1415
1516struct Cache {
@@ -201,16 +202,17 @@ struct SourceLocation {
201202
202203#[ derive( Default ) ]
203204pub struct Compiler {
204- cache : Option < Cache > ,
205+ cache : Option < Mutex < Cache > > ,
205206 compile : bool ,
206207}
207208
208209impl Compiler {
209210 pub fn new ( compile : bool , cache_path : Option < PathBuf > ) -> Result < Self > {
210- let cache = cache_path. map ( Cache :: new) . transpose ( ) ?;
211+ let cache = cache_path. map ( Cache :: new) . transpose ( ) ?. map ( Mutex :: new ) ;
211212 Ok ( Compiler { compile, cache } )
212213 }
213214
215+ /// the concurrency level of the exec is controlled by rayon parallelism
214216 fn exec ( args : & [ & str ] , stdin : & str ) -> Result < String > {
215217 let mut child = Command :: new ( "docker" )
216218 . args ( args)
@@ -242,7 +244,7 @@ impl Compiler {
242244 }
243245
244246 /// compiles ASM code
245- pub fn asm ( & mut self , src : & str ) -> Result < Bytes > {
247+ pub fn asm ( & self , src : & str ) -> Result < Bytes > {
246248 let mut bytecode = Bytecode :: default ( ) ;
247249 for op in src. split ( ';' ) {
248250 let op = match bytecode:: OpcodeWithData :: from_str ( op. trim ( ) ) {
@@ -256,9 +258,13 @@ impl Compiler {
256258 }
257259
258260 /// compiles LLL code
259- pub fn lll ( & mut self , src : & str ) -> Result < Bytes > {
260- if let Some ( bytecode) = self . cache . as_mut ( ) . and_then ( |c| c. get ( src) ) {
261- return Ok ( bytecode. clone ( ) ) ;
261+ pub fn lll ( & self , src : & str ) -> Result < Bytes > {
262+ if let Some ( bytecode) = self
263+ . cache
264+ . as_ref ( )
265+ . and_then ( |c| c. lock ( ) . unwrap ( ) . get ( src) . cloned ( ) )
266+ {
267+ return Ok ( bytecode) ;
262268 }
263269 if !self . compile {
264270 bail ! ( "No way to compile LLLC for '{}'" , src)
@@ -267,26 +273,30 @@ impl Compiler {
267273 let stdout = Self :: exec ( & [ "run" , "-i" , "--rm" , "lllc" ] , src) ?;
268274 let bytecode = Bytes :: from ( hex:: decode ( stdout. trim ( ) ) ?) ;
269275
270- if let Some ( cache) = & mut self . cache {
271- cache. insert ( src, bytecode. clone ( ) ) ?;
276+ if let Some ( ref cache) = self . cache {
277+ cache. lock ( ) . unwrap ( ) . insert ( src, bytecode. clone ( ) ) ?;
272278 }
273279
274280 Ok ( bytecode)
275281 }
276282
277283 /// compiles YUL code
278- pub fn yul ( & mut self , src : & str ) -> Result < Bytes > {
284+ pub fn yul ( & self , src : & str ) -> Result < Bytes > {
279285 self . solc ( Language :: Yul , src)
280286 }
281287
282288 /// compiles Solidity code
283- pub fn solidity ( & mut self , src : & str ) -> Result < Bytes > {
289+ pub fn solidity ( & self , src : & str ) -> Result < Bytes > {
284290 self . solc ( Language :: Solidity , src)
285291 }
286292
287- fn solc ( & mut self , language : Language , src : & str ) -> Result < Bytes > {
288- if let Some ( bytecode) = self . cache . as_mut ( ) . and_then ( |c| c. get ( src) ) {
289- return Ok ( bytecode. clone ( ) ) ;
293+ fn solc ( & self , language : Language , src : & str ) -> Result < Bytes > {
294+ if let Some ( bytecode) = self
295+ . cache
296+ . as_ref ( )
297+ . and_then ( |c| c. lock ( ) . unwrap ( ) . get ( src) . cloned ( ) )
298+ {
299+ return Ok ( bytecode) ;
290300 }
291301 if !self . compile {
292302 bail ! ( "No way to compile {:?} for '{}'" , language, src)
@@ -312,8 +322,8 @@ impl Compiler {
312322
313323 let bytecode = Bytes :: from ( hex:: decode ( bytecode) ?) ;
314324
315- if let Some ( cache) = & mut self . cache {
316- cache. insert ( src, bytecode. clone ( ) ) ?;
325+ if let Some ( ref cache) = self . cache {
326+ cache. lock ( ) . unwrap ( ) . insert ( src, bytecode. clone ( ) ) ?;
317327 }
318328
319329 Ok ( bytecode)
0 commit comments