This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use C99 Variable-length array if possible
Type: enhancement Stage: resolved
Components: Versions: Python 3.11
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: corona10, pablogsal, vstinner
Priority: normal Keywords:

Created on 2021-05-05 12:10 by corona10, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg393006 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-05-05 12:10
I had a chance to read Python/suggestion.c and I can notice that we use the static declared size of the array.

Since we live in the C99 era and the CPython codebase already uses C99(struct initialization is a good example), how about using the C99 feature if possible.

We should care about stack overflow from the big input but with the expected very small input size it will be okay to use.

-    static size_t buffer[MAX_STRING_SIZE];
-
     // Both strings are the same (by identity)
     if (a == b) {
         return 0;
@@ -68,6 +66,8 @@ levenshtein_distance(const char *a, size_t a_size,
         size_t t_size = a_size; a_size = b_size; b_size = t_size;
     }

+    size_t buffer[a_size];
+
     // quick fail when a match is impossible.
     if ((b_size - a_size) * MOVE_COST > max_cost) {
         return max_cost + 1
msg393007 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-05 12:59
Thanks for the suggestion! Unfortunately I am not very convinced since we already have the self imposed limitation for the maximum size of the string (this is to limit the execution time and memory) so there is no point of allowing bigger buffers. Also consider that this may happen on threads where the stack is much much smaller, so we cannot allow arbitrary length arrays.

Also, on a minor note, we use a restricted set of c99 supported by all compilers so we would need to check if this is supported in all of them.
msg393008 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-05-05 13:31
> Also, on a minor note, we use a restricted set of c99 supported by all compilers so we would need to check if this is supported in all of them

Ah, I notice that msvc does not support VLA (Sorry, I am not using Windows machine for 5 years +)
Using a C99/C11 features looks like a long future :(
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88215
2021-05-05 13:36:38corona10setstatus: open -> closed
stage: resolved
2021-05-05 13:31:56corona10setresolution: wont fix
2021-05-05 13:31:31corona10setmessages: + msg393008
2021-05-05 12:59:10pablogsalsetmessages: + msg393007
2021-05-05 12:10:58corona10create