Skip to content
العربية

Allocation and performance of builtins

This page comes from the «allocation without naming an allocator» table in the direction 0.9.1 report in the compiler repository, which we measured again on 0.10.0 in the same way: each operation in its own program, a hundred times, under --debug-mem. The rule in 0.10.0: nothing is allocated except what you see in the code (a builtin that builds something, +, or a literal), and the only exception is growing with +=; it still holds in 0.17.1.

Operation Blocks Who frees
س = "ا" + نص(ع) 1 You (حرر(س) or an arena). Forbidden in a بلا_افتراضي module
Joining two strings passed straight to اطبع or a comparison 0 Freed on its own
نص(ع), دمج_نصوص, قص, استبدال, حروف_كبيرة, رياضيات.نسق, اقرأ(), اقرأ_ملف 1 You
A literal list [ع, ع], and نسخ(ق) 2 (header and data) You
A literal dictionary {"ك": ع} 4 You
A list of lists [[ع], [ع]] 6, and حرر on the outer list leaves 4 You, deeply, by hand
تقسيم("ا,ب,ج", ",") 5 (the list and 3 strings) You, and حرر(the list) does not free its strings
مفاتيح(د) a list and a copy of each key You
قيم(د), نظام.وسائط() 2 You
Inserting a new string key into a dictionary a copy of the key The dictionary, on احذف or حرر
Repeated ن += "ب" grows by doubling Nobody: the old buffer stays (a hundred appends left 4 buffers)
د["ا"] = [ع] over an earlier list 2 You, before the assignment (otherwise the earlier one leaks)
A view passed to طول, ==, <, indexing, لكل, a dictionary key, يوجد, عدد(), كسر(), يحوي, بحث, يبدأ_بـ 0 —

A «view» is the result of جزء, قص_الفراغات, مسارات.الامتداد, مسارات.المجلد and مسارات.اسم_الملف: a string on the bytes of another string, with no copying, living as long as its original string. In 0.9.1, passing a view to the operations in the last row allocated a copy that was never given back, every time (one line in tic-tac-toe kept 1.1 million blocks until exit). That was removed in 0.10.0: the same measurement gives 0 blocks.

This measurement has not been redone on 0.17.1. What changed after 0.10.0, in the words of the release notes: since 0.13.0 the compiler frees temporaries (anything without a name) wherever it is safe, and احذف (delete) on a dictionary returns the removed value; since 0.14.0 a local allocation in a function that is neither freed nor passed out of it is compile error E0472. عدد() and كسر() in the table became حلل(ن, عدد64) and حلل(ن, كسر64) in 0.12.0, and were not measured on a view.

How to tell: --debug-mem names the line and the number of blocks, not the operation.

  • Indexing a string by character, ن[ف], goes through the string from its start on every call: 20 thousand indexings took 2.2 s on one string in 0.9.1 (and 1.74 s instead of 2.56 in the 0.11.0 measurement). لكل ح في ن goes through it once.
  • The default static build (musl) is slower than --dynamic (glibc) by 1.2 to 1.6 times in allocation-heavy programs.
  • The detector, which has been on by default in a direct run since 0.10.0, is 0% to 32% slower than --release on the benchmark programs in the compiler repository, and memory freed in its mode is not returned to the system. So a long-running program that frees a lot is run with --release or built with meem build.

احذف

On a dictionary, it frees the copy of the string key that the dictionary owns.

استبدال

Allocates a new string; you free it.

اضف

Growing the list frees its old buffer: the growth itself leaks nothing.

اطبع

The result of joining two strings with + passed straight to it is freed on its own after printing, and so is the result of a builtin such as نص(ع) inside it; but the result of a function you write yourself inside that join is not freed.

اقرأ

Allocates a copy of the line read; you free it.

بحث

Allocates nothing, even on a view.

تقسيم

Allocates the list and a string for each piece (copies, not views). حرر(the list) does not free its strings, so use an arena when there are many.

جزء

Returns a view onto the original string with no copying, and using it afterwards (length, comparison, indexing, لكل, a dictionary key) allocates nothing since 0.10.0. A view lives as long as its original string: using it after the original is freed is caught by the detector (E0430), and in a file built without the detector it is undefined behaviour.

حاول_احجز

A valid size that does not fit in memory returns خطأ.لا_ذاكرة. But a wildly «invalid» size (such as 10^14) is runtime error E0366, which cannot be caught.

حرر

Does not follow what is inside a container: freeing a list of lists leaves the inner lists, and freeing a list from تقسيم leaves its strings. It makes the variable لاشيء, so using the list or dictionary through the same name afterwards is error E0428 in every mode. Use or double free through another name is caught by the detector (E0429, E0430, E0431, E0350), and in a file built without the detector it is undefined behaviour.

حروف_كبيرة

Allocates a new string; you free it.

دمج_نصوص

Allocates a new string; you free it.

رتب_ب

The temporary merge buffer is freed on its own.

طول

On a string, it counts the characters by going through it, and it allocates nothing, even on a view.

قص

Allocates a new string; you free it.

قص_الفراغات

Like جزء: a view with no copying, and using it allocates nothing. It lives as long as its original string.

قيم

Allocates a new list (a header and data).

مفاتيح

Allocates a list and a copy of each string key: «لكل ك في مفاتيح(د)» copies all the keys. Going through the dictionary itself, «لكل ك في د», does not copy.

نسخ

Allocates a new copy (a list has a header and data).

نص

Allocates a new string from the last allocator or the default one; you free it (حرر or an arena).

يبدأ_بـ

Allocates nothing, even on a view.

يحوي

Allocates nothing, even on a view.

Memory in Meem · Builtins · Reference index