About Golang Compilation Time

Asked 2 years ago, Updated 2 years ago, 64 views

I'm a beginner who started writing code in go language about a week ago.

As for the question, I asked you how to shorten the compilation time (mannered) such as "go run" and "go buyold/install".

I will write a little bit about the current situation.
I am currently writing a program using go.
Since the explanation is confusing, let's assume that you are creating two files: main.go and mypackage.go.main.go has func main() and mypackage.go has other func.

I can't use go's standard library for what I want to do, so I create a lot of func myself and create a file (mypackage.go) that summarizes it.I'm calling it in main.go with import to see how the program works ("gorun main.go").

In this way, we are currently creating, compiling, and executing code. I'm having a hard time compiling mypackage.go for a very long time (more than 4-6 minutes) since mypackage.go was over a few hundred lines, and the result was the same when I ported all mypackage.go func to main.go.I am still planning to add func to mypackage.go, so I expect it to take more time to compile.

When I looked into the reduction of compilation time on the web, I found a description that calling mypackage.go every time is taking more time.So, after the go installation, I took the method of go build/run.This method itself does not take long to compile after installation, but after modifying mypackage.go (of course), the program results will not reflect the modifications unless you do the same again.

Is it possible to compile quickly while modifying mypackage.go and see the results?
I started using go because the compilation time was short, but I'm embarrassed that I'm failing at the beginning.How do you compile mypackage.go without taking time?

May I speak to you?

go

2022-09-30 20:57

2 Answers

@argus
As I am a beginner, I apologize for any deficiencies in the etiquette (how to answer) on the site.

I have found something that seems to be causing the compilation time to be unusually long.
Save it for recording with the sample code.

sample.go:

package main 
import "fmt" 
typeImgInfo structure { 
             MaxColSize int
             MaxRowSize int
             GREEN [1040][1388] int//← This was the cause. 
} 

funcShow(ImgImgInfo){ 
     fmt.Println (Img.MaxColSize, Img.MaxRowSize) 
     Img.MaxColSize++
     /* GREEN Calculation (Omitted)*/
     fmt.Println (Img.MaxColSize, Img.MaxRowSize) 
} 

funcmain(){
     Img: = ImgInfo{} 
     Show (Img) 
} 

Apparently, creating a 2DMatrix with a matrix would take a long time to compile.

Up until now, I have used it like a C++ dynamic array.
In C++, setting a fixed-length array does not have much impact on the compilation time (I don't think so), but
Apparently, in the go language, a fixed length increases the compilation time.

For example, the above code is

time run sample.go

I don't know why the compilation time is so different because of lack of study. In fact, there were many lines like this, each of which affected the compilation time and took a total of six minutes to compile.If you rewrite all of these parts, the total compilation time is about 0.6 seconds.I learned a lot.Thank you for your continued support.


2022-09-30 20:57

As far as I've looked, passing a huge object to the argument increases the compilation time.

go embeds the information needed for the garbage collection of stack frames in the executable, and this data grows as the argument size grows.

Note: https://golang.org/doc/asm#runtime

When you run go tool 6g-Stest.go to examine the assembly, you can see that there is a large amount of data embedded, such as:The output of this data is likely to affect the compilation time.

gclocals·c0c23a3d90b7d84b1fe0b61ccfa2921dt=7dupok size=1082660value=0
        0x00000000840d 2c 00 55 55 55 55 55 55 55 55 55 55 55 55 55 55 ...., .UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0010 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0020 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0030 5555555555555555555555UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0040 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0050 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0060 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0070 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0080 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        0x0090 55 55 55 55 55 55 55 55 55 55 55 55 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
        ...

This problem can be resolved by passing a pointer instead of a value.

funcShow(Img*ImgInfo){ 

} 

funcmain(){
     Img: = ImgInfo{} 
     Show (&Img) 
}


2022-09-30 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.