Skip to content

Commit a2b4610

Browse files
committed
write ast file concurrently
1 parent 587d10a commit a2b4610

File tree

7 files changed

+80
-64
lines changed

7 files changed

+80
-64
lines changed

c/llcppg.pub

Lines changed: 0 additions & 19 deletions
This file was deleted.

c/net/llcppg.pub

Lines changed: 0 additions & 3 deletions
This file was deleted.

c/os/llcppg.pub

Lines changed: 0 additions & 7 deletions
This file was deleted.

c/pthread/llcppg.pub

Lines changed: 0 additions & 3 deletions
This file was deleted.

c/pthread/sync/llcppg.pub

Lines changed: 0 additions & 7 deletions
This file was deleted.

c/time/llcppg.pub

Lines changed: 0 additions & 5 deletions
This file was deleted.

chore/genpub/pub/handle.go

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func WritePubfile(pubFile string) {
110110
}
111111
fmt.Println("handle finished =>", pubFile)
112112
}()
113-
doWritePubfile(w, pubFile)
113+
DoWritePubFile(w, pubFile)
114114
}
115115

116116
func PubFilenameForDir(dir string, pubFile string) string {
@@ -229,27 +229,87 @@ func MergePubfiles(llcppgPubFileName string, dir string) {
229229
wg.Wait()
230230
}
231231

232-
func doWritePubfile(w io.Writer, pubfile string) {
233-
fset := token.NewFileSet()
234-
dir := filepath.Dir(pubfile)
235-
pkgMap, err := parser.ParseDir(fset, dir, func(fi fs.FileInfo) bool {
236-
return !strings.HasPrefix(fi.Name(), "_") &&
237-
!strings.HasPrefix(fi.Name(), ".") &&
238-
strings.HasSuffix(fi.Name(), ".go")
239-
}, parser.ParseComments)
240-
if err != nil {
241-
panic(err)
242-
}
243-
pubWriter := NewPubWriter(w, fset)
244-
for _, v := range pkgMap {
245-
for _, f := range v.Files {
246-
if ast.IsGenerated(f) {
247-
continue
232+
func GenAstFiles(quit chan int, dir string, fset *token.FileSet) <-chan *ast.File {
233+
output := make(chan *ast.File)
234+
go func() {
235+
defer close(output)
236+
pkgMap, err := parser.ParseDir(fset, dir, func(fi fs.FileInfo) bool {
237+
return !strings.HasPrefix(fi.Name(), "_") &&
238+
!strings.HasPrefix(fi.Name(), ".") &&
239+
strings.HasSuffix(fi.Name(), ".go")
240+
}, parser.ParseComments)
241+
if err != nil {
242+
panic(err)
243+
}
244+
for _, v := range pkgMap {
245+
for _, f := range v.Files {
246+
if ast.IsGenerated(f) {
247+
continue
248+
}
249+
if !ast.FileExports(f) {
250+
continue
251+
}
252+
select {
253+
case output <- f:
254+
case <-quit:
255+
return
256+
}
248257
}
249-
if !ast.FileExports(f) {
250-
continue
258+
}
259+
}()
260+
return output
261+
}
262+
263+
func writeAstFiles(quit chan int, fset *token.FileSet, files <-chan *ast.File) <-chan string {
264+
output := make(chan string)
265+
go func() {
266+
defer close(output)
267+
for file := range files {
268+
temp, err := os.CreateTemp(os.TempDir(), "file*.pub")
269+
if err != nil {
270+
fmt.Println(err)
271+
return
251272
}
252-
pubWriter.WriteFile(f)
273+
defer temp.Close()
274+
w := bufio.NewWriter(temp)
275+
defer func() {
276+
if w.Buffered() > 0 {
277+
w.Flush()
278+
output <- temp.Name()
279+
} else {
280+
os.Remove(temp.Name())
281+
}
282+
}()
283+
pubWriter := NewPubWriter(w, fset)
284+
pubWriter.WriteFile(file)
253285
}
286+
}()
287+
return output
288+
}
289+
290+
func DoWritePubFile(w io.Writer, pubFile string) {
291+
quit := make(chan int)
292+
defer func() {
293+
close(quit)
294+
}()
295+
fset := token.NewFileSet()
296+
dir := filepath.Dir(pubFile)
297+
astFiles := GenAstFiles(quit, dir, fset)
298+
files := writeAstFiles(quit, fset, astFiles)
299+
wg := sync.WaitGroup{}
300+
for file := range files {
301+
wg.Add(1)
302+
go func(fileName string) {
303+
defer func() {
304+
os.Remove(fileName)
305+
wg.Done()
306+
}()
307+
b, err := os.ReadFile(fileName)
308+
if err != nil {
309+
panic(err)
310+
}
311+
w.Write(b)
312+
}(file)
254313
}
314+
wg.Wait()
255315
}

0 commit comments

Comments
 (0)