-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When the length of the array exceeds 1073741824, it will cause the current thread to be in a blocked state #21918
Comments
The main difference is that Java has a runtime, so extra checks like this can be done at runtime. If you want this with V, you will need to do the checks yourself, since V doesn't have a runtime. If you really need to work with huge arrays like this, it would be best to declare a struct, with the Otherwise, you can try increasing values with the V command line option |
A brief description of my usage scenario at that time may help to understand. I tried to write a file transfer tool, using the API about network IO in the v standard library. I have implemented the file transfer function, and tested the transfer of files from several Mb to hundreds of Mb in size. Everything is normal. But when I tested a movie file transfer of about 2Gb in size, I waited about 30 minutes and still did not complete the transfer. I realized that there may be a problem, because using the operating system's Ctrl + V only takes about 30 seconds. At first I thought my code was not efficient, but after several hours of investigation, I found that the cause was the thread blocking caused by the addition of element code to the v array. ...
file_path := 'C:\\Users\\TZ\\Desktop\\星际穿越.rmvb'
mut file := os.open( file_path ) or { eprintln( err.str() ) return }
defer { file.close() }
bytes := file.read_bytes( int( os.file_size( file_path ) ) ) //Module: os method signature:`pub fn (f &File) read_bytes(size int) []u8`
println('file bytes len=${bytes.len}') //file bytes len=1911250979
... The array length of the final printout is 1911250979, which obviously exceeds 1073741824. |
It seems that the array length can't exceed 1073741824, #17958 |
Yes, it is a known problem, caused by V using 32 bit integers for the .len and .cap fields of its dynamic arrays, and the default strategy, for growing a dynamic array (doubling the allocated memory, when the capacity is reached). |
In the read_bytes's case, the resulting array's capacity is known beforehand (by the file size), thus it is allocated right away, without needing to reallocate anything after that, and so the array can reach up to |
…1, instead of overflowing a.cap (partial fix for vlang#21918)
I'm making |
Okay, I see. |
With latest V, it will print a backtrace on reaching the limit, and that will be at 2^31, not 2^30 (as a temporary fix, until the |
Yes, I have tried it and it works fine. But there are still some things I can't figure out, what happens when the int type comes to a 64-bit system? Will the number of bytes occupied change? I remember the documentation saying "Unlike C and Go, int is always a 32-bit integer". And when it comes, will your changes be reverted? Do programmers still need to check for out-of-bounds by themselves? |
array.len will be i64, so the size of the array structure will grow |
I consider this specific issue solved (the blocking behavior due to the infinite loop is fixed, the memory limit is increased, and there is better diagnostic now, when the limit is reached). @medvednikov, @tzSharing do you agree? |
Yes, I agree. @spytheman |
Describe the bug
adding an element to an array, when its length exceeds 1073741824, causes the current thread to block without any exception information
Reproduction Steps
V code:
output:
The main thread is blocked and can only be forcibly interrupted.
I tried running the same code with Kotlin to see the results, and it can be seen that Kotlin did not encounter any thread blocking issues and provided exception information. It may be that the memory requested by the Java virtual machine is too small. Most importantly, it provided location information, which is crucial for locating errors, especially when troubleshooting in complex projects.
kotlin code:
output:
Expected Behavior
Since the length of the array is of type int, the length should be<=max_int. Even if there is a failure during the addition process due to various reasons, there should be an exception message instead of blocking the thread.
Current Behavior
The running thread is blocked
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.6 fd7986c
Environment details (OS name and version, etc.)
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: