From 5730e57e78c98a7ef613d83746d5637e89fc8ede Mon Sep 17 00:00:00 2001 From: James Peach Date: Sat, 19 Jun 2021 15:03:05 +1000 Subject: [PATCH] Use a local tempfile for preprocessed output. Preprocessed output tends to be quite large, often multiple megabytes. Rather than transferring it over the RPC interface, use a tempfile so that the server doesn't use a lot of memory uneccessarily. This updates #45. --- cmd/llamacc/args.go | 11 +++++++++++ cmd/llamacc/main.go | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/llamacc/args.go b/cmd/llamacc/args.go index 1e7c82f..44106c8 100644 --- a/cmd/llamacc/args.go +++ b/cmd/llamacc/args.go @@ -89,6 +89,17 @@ func (c *Compilation) RemoteCompiler(cfg *Config) string { return "cc" } +// LanguageExt returns the file extension for the current language. +func (c *Compilation) LanguageExt() string { + for k, v := range extLangs { + if v == c.Language { + return k + } + } + + panic("unknown language extension") +} + type Flags struct { MD bool MMD bool diff --git a/cmd/llamacc/main.go b/cmd/llamacc/main.go index 757b49c..736301a 100644 --- a/cmd/llamacc/main.go +++ b/cmd/llamacc/main.go @@ -188,6 +188,16 @@ func buildLocalPreprocess(ctx context.Context, client *daemon.Client, cfg *Confi return fmt.Errorf("find %s: %w", comp.LocalCompiler(cfg), err) } + tmp, err := ioutil.TempFile("", fmt.Sprintf("llamacc-*%s", comp.LanguageExt())) + if err != nil { + return err + } + + defer func() { + os.Remove(tmp.Name()) + tmp.Close() + }() + var preprocessed bytes.Buffer { var preprocessor exec.Cmd @@ -197,8 +207,9 @@ func buildLocalPreprocess(ctx context.Context, client *daemon.Client, cfg *Confi preprocessor.Args = append(preprocessor.Args, comp.LocalArgs...) if !cfg.FullPreprocess { preprocessor.Args = append(preprocessor.Args, "-fdirectives-only") + } - preprocessor.Args = append(preprocessor.Args, "-E", "-o", "-", comp.Input) + preprocessor.Args = append(preprocessor.Args, "-E", "-o", tmp.Name(), comp.Input) preprocessor.Stdout = &preprocessed preprocessor.Stderr = os.Stderr if cfg.Verbose { @@ -212,6 +223,9 @@ func buildLocalPreprocess(ctx context.Context, client *daemon.Client, cfg *Confi args := daemon.InvokeWithFilesArgs{ Function: cfg.Function, + Files: []files.Mapped{ + remap(tmp.Name(), wd), + }, Outputs: []files.Mapped{ { Local: files.LocalFile{Path: path.Join(wd, comp.Output)}, @@ -226,7 +240,7 @@ func buildLocalPreprocess(ctx context.Context, client *daemon.Client, cfg *Confi if !cfg.FullPreprocess { args.Args = append(args.Args, "-fdirectives-only", "-fpreprocessed") } - args.Args = append(args.Args, "-x", comp.PreprocessedLanguage, "-o", comp.Output, "-") + args.Args = append(args.Args, "-x", comp.PreprocessedLanguage, "-o", comp.Output, toRemote(tmp.Name(), wd)) out, err := client.InvokeWithFiles(&args) if err != nil {