Skip to content

Commit 90270d9

Browse files
authored
Merge pull request #31 from c9s/store-regexp-cache
IMPROVE: store compiled regexp in cache
2 parents e010b2f + e664920 commit 90270d9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/.idea
2+
.DS_Store

cmd/requestgen/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ func (g *Generator) generate(typeName string) {
599599
g.importPackage("encoding/json")
600600
g.importPackage("regexp")
601601
g.importPackage("reflect")
602+
g.importPackage("sync")
602603

603604
if g.apiClientField != nil && (*apiUrlStr != "" || *useDynamicPath) {
604605
g.importPackage("net/url")
@@ -1220,9 +1221,21 @@ func ({{- $recv }} * {{- typeString .StructType -}} ) GetSlugParameters() (map[s
12201221
return params, nil
12211222
}
12221223
1224+
var {{ typeString .StructType }}SlugReCache sync.Map
1225+
1226+
{{- $slugReCache := print (typeString .StructType) "SlugReCache" }}
1227+
12231228
func ({{- $recv }} * {{- typeString .StructType -}} ) applySlugsToUrl(url string, slugs map[string]string) string {
12241229
for _k, _v := range slugs {
1225-
needleRE := regexp.MustCompile(":" + _k + "\\b")
1230+
var needleRE *regexp.Regexp
1231+
1232+
if cached, ok := {{ $slugReCache }}.Load(_k) ; ok {
1233+
needleRE = cached.(*regexp.Regexp)
1234+
} else {
1235+
needleRE = regexp.MustCompile(":" + _k + "\\b")
1236+
{{ $slugReCache }}.Store(_k, needleRE)
1237+
}
1238+
12261239
url = needleRE.ReplaceAllString(url, _v)
12271240
}
12281241
@@ -1562,6 +1575,10 @@ func locateObject(ts *requestgen.TypeSelector, selectedPkgs []*packages.Package)
15621575
log.Debugf("found response type def: %+v -> %+v type:%+v import:%s", ident.Name, obj, obj.Type(), obj.Pkg().Path())
15631576
return obj, nil
15641577

1578+
case *types.Alias:
1579+
log.Debugf("found alias type: %+v", t)
1580+
return obj, nil
1581+
15651582
case *types.Struct:
15661583
log.Infof("found struct type: %+v", t)
15671584
log.Debugf("found response type def: %+v -> %+v type:%+v import:%s", ident.Name, obj, obj.Type(), obj.Pkg().Path())

0 commit comments

Comments
 (0)