@@ -23,6 +23,7 @@ import (
2323	"regexp" 
2424	"strconv" 
2525	"strings" 
26+ 	"sync" 
2627)
2728
2829// Config holds configuration extracted from a googleapis BUILD.bazel file. 
@@ -59,6 +60,7 @@ func (c *Config) Validate() error {
5960	return  nil 
6061}
6162
63+ var  javaGapicLibraryRE  =  regexp .MustCompile (`java_gapic_library\((?s:.)*?\)` )
6264// Parse reads a BUILD.bazel file from the given directory and extracts the 
6365// relevant configuration from the java_gapic_library rule. 
6466func  Parse (dir  string ) (* Config , error ) {
@@ -70,8 +72,7 @@ func Parse(dir string) (*Config, error) {
7072	}
7173	content  :=  string (data )
7274
73- 	re  :=  regexp .MustCompile (`java_gapic_library\((?s:.)*?\)` )
74- 	gapicLibraryBlock  :=  re .FindString (content )
75+ 	gapicLibraryBlock  :=  javaGapicLibraryRE .FindString (content )
7576	if  gapicLibraryBlock  !=  ""  {
7677		c .hasGAPIC  =  true 
7778		c .grpcServiceConfig  =  findString (gapicLibraryBlock , "grpc_service_config" )
@@ -84,12 +85,23 @@ func Parse(dir string) (*Config, error) {
8485	if  err  :=  c .Validate (); err  !=  nil  {
8586		return  nil , fmt .Errorf ("librariangen: invalid bazel config in %s: %w" , dir , err )
8687	}
87- 	slog .Debug ("librariangen: bazel config loaded" , "conf" , fmt . Sprintf ( "%+v" ,  c ) )
88+ 	slog .Debug ("librariangen: bazel config loaded" , "conf" , c )
8889	return  c , nil 
8990}
9091
92+ var  reCache  =  & sync.Map {}
93+ 
94+ func  getRegexp (key , pattern  string ) * regexp.Regexp  {
95+ 	val , ok  :=  reCache .Load (key )
96+ 	if  ! ok  {
97+ 		val  =  regexp .MustCompile (pattern )
98+ 		reCache .Store (key , val )
99+ 	}
100+ 	return  val .(* regexp.Regexp )
101+ }
102+ 
91103func  findString (content , name  string ) string  {
92- 	re  :=  regexp . MustCompile ( fmt .Sprintf (`%s\s*=\s*"([^"]+)"` , name ))
104+ 	re  :=  getRegexp ( "findString_" + name ,  fmt .Sprintf (`%s\s*=\s*"([^"]+)"` , name ))
93105	if  match  :=  re .FindStringSubmatch (content ); len (match ) >  1  {
94106		return  match [1 ]
95107	}
@@ -98,7 +110,7 @@ func findString(content, name string) string {
98110}
99111
100112func  findBool (content , name  string ) (bool , error ) {
101- 	re  :=  regexp . MustCompile ( fmt .Sprintf (`%s\s*=\s*(\w+)` , name ))
113+ 	re  :=  getRegexp ( "findBool_" + name ,  fmt .Sprintf (`%s\s*=\s*(\w+)` , name ))
102114	if  match  :=  re .FindStringSubmatch (content ); len (match ) >  1  {
103115		if  b , err  :=  strconv .ParseBool (match [1 ]); err  ==  nil  {
104116			return  b , nil 
0 commit comments