Programs written in JavaScript (TypeScript) were rewritten in the Go language.
I will write about the motivation and the results of the rewrite.
Also, since this was my first time using the Go language, I will also write about what the Go language is and my impressions of the Go language.
motive
Hard to maintain
The ported program was a data processing-related program written in JavaScript (TypeScript) and running on a server.
JavaScript is a populer language, but people who don't do front-end work are not very familiar with it, and I was worried that only a limited number of people would be able to handle it and deal with any problems.
All programming languages have similar grammars, and it may seem like you can get by with a quick glance through a grammar book if you just want to make a small modification, but JavaScript asynchronous programming grammars such as Promise are JavaScript-specific grammars and must be learned separately However, JavaScript asynchronous programming grammars such as Promise are specific to JavaScript and must be learned separately!
Moreover, since JavaScript is based on asynchronous programming, they are essential, but even if you learn how to use them, there are no other situations where you can make use of them.
For those reasons, I decided to rewrite it in a language that seemed a little easier to handle, because I felt that it would be a waste of time to have people learn JavaScript just for that purpose when they have no other plans to use JavaScript.
And I thought that if I was going to rewrite the language, if I was going to learn it anew, I wanted it to have some kind of motivational benefit.
I want to reduce processing time.
As is often the case, the processing time of a program was short in the beginning, but as the amount of data to be handled increased, it began to take quite a long time.
It was the kind of thing that could be scaled out to reduce processing time, but I was too lazy to divide the processing into tasks and create a mechanism to integrate them....
For the time being, I thought that I would like to shorten the processing time by changing the language, while raising the PC specs and keeping the program content as it is now.
Language Selection
First, we thought of "something that is easy to maintain and use," in other words, "something that is easy to set up and learn, and that seems to be trendy and worth learning," and decided to go with Python or Go language.
In terms of speed, I can't speak for Python, but the Go language is a compiled language, so I could certainly expect it to be faster than it is now.
Also, I am ashamed to admit that I make a lot of typos, and I had a lot of run-time errors with dynamically typed languages, I was fully aware of the advantages of statically typed languages, so I also wanted to use a statically typed language if I was going to write a larger program.
For those reasons, we decided to rewrite it in Go language.
transplanting
I started learning the Go language from scratch and ended up porting it with considerable difficulty.
The Go language also has a syntax for asynchronous processing (concurrent processing), so there was no problem in porting the asynchronous program.
result
Faster!
Although it varies depending on conditions and it is difficult to generalize, processing time has generally been reduced by 30-60%.
Particularly interesting was that the higher the PC specs, the more visible the difference became.
This is because when parallel processing is done in the Go language, when the number of CPU cores is increased, processing is allocated to each of them and the full benefit is received, whereas JavaScript runs single-threaded, so it does not benefit as much when the number of CPU cores is increased, This seems to be due to the fact that JavaScript runs single-threaded, so it does not benefit much from an increase in the number of CPU cores.
On the other hand, when compared with a simple program with low CPU operations without parallel processing on a PC with low specifications, the reduction in processing time was about 10-20%, and I personally felt that it was not as fast as I expected.
Unlike benchmark programs, I/O and communication time are more important for practical programs than arithmetic processing, so no matter how fast the Go language is, it is inevitable that this will be the case if there are few areas where it can be used to its full potential.
How to maximize the benefits of the Go language?
However, I still think the appeal of the Go language is its speed.
The Go language is fast mainly due to the following
- native program
- concurrent processing
Therefore, the more intensive the operations, and the more intensive the concurrency, the better the Go language is suited.
And the higher the machine power of the PC on which such programs are run, the faster the Go language will run.
On the other hand, in applications such as microservices, where "simple but high-volume processing can be done with low-spec instances and using a large number of instances, Personally, I think that the advantage of using the Go language for its high processing speed is not as great as I expected.
Of course, it is definitely faster than a script program, so if there are no language restrictions, you can't go wrong with the Go language from the start, speed-wise.
These are the motivations and results of switching from JavaScript to the Go language.
Since this was my first time using the Go language, I would like to write about my impressions of the Go language in a rambling manner.
What kind of language is the Go language?
In a word
The Modern C Language.
It was.
Google's motivation for developing the Go language is described below, and it is exactly what it sounds like.
- I want to write fast programs
- I want to write with ease.
- I want to process in parallel.
To write fast programs, you can write native programs, but the C language is cumbersome to set up and handle, and the functions are too primitive to code.
So, the Go language was developed to hide the troublesome things and to improve the C syntax in the style of today's languages so that programmers can concentrate on logic and write native programs.
learning cost
Contrary to my initial expectations, I honestly felt it was expensive.
Since it is a C-type language, it is essentially one of the more difficult languages to learn because it requires an understanding of memory, pointers, and data structures, and programming with constant awareness of them.
However, since there is no pointer arithmetic, there is no pointer-intensive programming like in C. It is sufficient to know the difference between actuality and pointer (reference), and the degree of difficulty is only compared to languages without pointers.
In addition, there are several writing styles unique to the Go language to make it simple to write, but I got quite stuck here.
And apart from learning the grammar, it is also necessary to learn the design patterns of concurrency, which is something to be learned to some extent.
ease of writing
As difficult and contradictory as it may seem, the code is designed to be easy to write, and intentionally so.
It is designed so that you can write just what you want to do programmatically, without cumbersome environment construction or boilerplate.
In addition, for processes that are often written, there is a writing system that can be used to write them in a concise manner.
The need to deal with inherently difficult things such as memory and concurrency is designed so that programmers do not have to devote effort to anything else and can concentrate on the program content.
concurrent processing
What I personally consider to be the greatest feature of the Go language is the concurrency capability at the language level.
Nowadays, multi-core CPUs are commonplace, and to take advantage of this, it is necessary to write programs that handle hardware, such as dividing processing into threads and executing them.
However, the Go language abstracts concurrency, so all you have to do is specify the processes you want to run concurrently, and the Go language will automatically schedule them into threads behind the scenes, without the programmer having to be aware of the hardware, Programmers can write parallel processing programs using multi-cores without being aware of the hardware.
Uses
The Go language can be used to write native programs, but it does not seem to be a language for truly native processing.
What I mean is that the Go language is not intended to be used to write system programs or GUI programs. (Although it is not impossible to write them.)
The Go language comes with a set of commonly used packages that can be used to do most things, but the packages are mainly focused on data processing and communication, However, the packages are mainly for data processing and communication, and I feel that the language is mainly targeted at data processing on the server side. (I feel that it is a language mainly targeted at server-side data processing.)
For data processing, there is not much need to bother with classes, which explains why the Go language is based on the C language rather than C++.
I can also create native programs, which makes it suitable for creating command line tools.
productivity
The Go language is
- Create new programs quickly.
- Write only the logic you want to do in your program.
- Processes that are often written can be written in a concise manner.
- Frequently used processes are available as a package.
So if you are writing a general data processing program, you will be very productive.
In other words, you can write compiled programs with the ease of a scripting language.
What I liked about the Go language
Long cord
It's very subjective, but the code is long and therefore difficult to read.
In the Go language, it is customary to always check for errors in the return value after calling a function.
Each function call is always followed by three lines of error-checking if statements, which tends to lengthen the overall code as a result.
Concurrency tuning is troublesome
Parallel processing is done automatically, but you need to adjust the number of parallel processes and the buffer size when waiting for processing.
With PC specs, data volume, and data frequency varying widely, finding good adjustment values can be quite troublesome, and there may be no optimal solution to begin with....
Well, in other languages, it's a challenge just to parallelize the process, so the difficulties beyond that can be said to be a luxury that only the Go language can provide.
Impressions, etc.
In this case, the application was a server-side program that processes a large number of files in parallel, so it matched the strengths of the Go language and was as fast as we expected it to be, a great success!
I will continue to write data processing programs in the Go language!
However, I would think twice if I were asked to write even a small program in the Go language.
This is because although the Go language can be written with the ease of a scripting language, it is still a solid language and cannot be written in an atmosphere.
I will try to write some tool programs to see how this area actually works.
In learning the Go language, there are some things that are not described in detail in Go books, such as the Go language's unique writing style, which is not explained in detail so as not to make it difficult, and this is where I got really hooked.
Since we all seem to get stuck in the same place, I have left an article about it for your reference.