{"id":5435,"date":"2024-08-27T13:24:11","date_gmt":"2024-08-27T12:24:11","guid":{"rendered":"https:\/\/gdksoftware.com\/knowledgebase\/const-or-not-compiler-optimization-in-coding"},"modified":"2024-08-27T13:24:11","modified_gmt":"2024-08-27T12:24:11","slug":"const-or-not-compiler-optimization-in-coding","status":"publish","type":"knowledge","link":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding","title":{"rendered":"Const or not? Compiler optimization in coding"},"content":{"rendered":"<p><span data-slate-node=\"text\">Is <\/span><span data-slate-node=\"text\"><span spellcheck=\"false\" data-slate-leaf=\"true\"><code class=\"\" data-line=\"\">const<\/code><\/span><\/span><span data-slate-node=\"text\" data-slate-fragment=\"JTVCJTdCJTIydHlwZSUyMiUzQSUyMnAlMjIlMkMlMjJjaGlsZHJlbiUyMiUzQSU1QiU3QiUyMm9yZGVyZWRNYXJrcyUyMiUzQSU1QiUyMmNvbG9yJTIyJTVEJTJDJTIydGV4dCUyMiUzQSUyMklzJTIwJTIyJTdEJTJDJTdCJTIyb3JkZXJlZE1hcmtzJTIyJTNBJTVCJTIyY29sb3IlMjIlNUQlMkMlMjJjb2RlJTIyJTNBdHJ1ZSUyQyUyMnRleHQlMjIlM0ElMjJjb25zdCUyMiU3RCUyQyU3QiUyMm9yZGVyZWRNYXJrcyUyMiUzQSU1QiUyMmNvbG9yJTIyJTVEJTJDJTIydGV4dCUyMiUzQSUyMiUyMGZvciUyMiU3RCU1RCU3RCU1RA==\"> for<\/span>\u00a0a parameter useful or only for nerds and nitpickers?<\/p>\n<p>Should it be: <span data-slate-node=\"text\"><span spellcheck=\"false\" data-slate-leaf=\"true\"><code class=\"\" data-line=\"\">procedure Check(const Text: string);<\/code><\/span><\/span><span data-slate-node=\"text\">? Or is <\/span><span data-slate-node=\"text\" data-slate-fragment=\"JTVCJTdCJTIydHlwZSUyMiUzQSUyMnAlMjIlMkMlMjJjaGlsZHJlbiUyMiUzQSU1QiU3QiUyMm9yZGVyZWRNYXJrcyUyMiUzQSU1QiUyMmNvbG9yJTIyJTVEJTJDJTIyY29kZSUyMiUzQXRydWUlMkMlMjJ0ZXh0JTIyJTNBJTIycHJvY2VkdXJlJTIwQ2hlY2soY29uc3QlMjBUZXh0JTNBJTIwc3RyaW5nKSUzQiUyMiU3RCUyQyU3QiUyMm9yZGVyZWRNYXJrcyUyMiUzQSU1QiUyMmNvbG9yJTIyJTVEJTJDJTIydGV4dCUyMiUzQSUyMiUzRiUyME9yJTIwaXMlMjAlMjIlN0QlMkMlN0IlMjJvcmRlcmVkTWFya3MlMjIlM0ElNUIlMjJjb2xvciUyMiU1RCUyQyUyMmNvZGUlMjIlM0F0cnVlJTJDJTIydGV4dCUyMiUzQSUyMnByb2NlZHVyZSUyMENoZWNrKFRleHQlM0ElMjBzdHJpbmcpJTNCJTIyJTdEJTVEJTdEJTVE\"><span spellcheck=\"false\" data-slate-leaf=\"true\"><code class=\"\" data-line=\"\">procedure Check(Text: string);<\/code><\/span><\/span> basically the same?<\/p>\n<p>Maybe you never ask yourself how the compiler handles your code. And what effects that has on your application. But if you know a bit about how this works in the background, it&#8217;s pretty smart to take it seriously. It really matters for the performance and size of your application.<\/p>\n<p>A constant parameter allows the compiler to associate the value directly with the variable, without having to re-evaluate it. This is called <em>constant propagation<\/em>. This is (usually) combined with a technique called <em>constant folding<\/em>. In very simply terms, the compiler ensures that variables are replaced (during compilation) by constant expressions, so they no longer need to be evaluated runtime. That means the compiler can already simplify your code and reduce the amount of runtime evaluations. And as a result, it simply becomes faster.<\/p>\n<p>There is one exception and that is parameters that already work by reference, as the case for objects. That is already optimised. Small exception to that again is when you work with interfaces. There is still a small difference if you work with or without const.<\/p>\n<p>Once you know this, you can also act intentionally with this. And if you do, don&#8217;t just think about adding a const, but also think (for example) about code in a loop that uses variables. What happens if you convert the contents of a loop to a procedure or function with constant parameters? As you can imagine, this has positive effects on performance. And that is just an example.<\/p>\n<p>Personally, I knew that const was a compiler optimization, but not exactly how it worked. The trigger to take a closer look at this came from another example. I came across this on the MVP forum. It is a great example which shows that you are usually unaware of the effect on the compiler. You can also take advantage of this as it is not an unusual construction.<\/p>\n<p>The example is as follows:<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GetUserCollection<\/span><span style=\"color: #333333;\">:<\/span> TUserCollection<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">begin<\/span>   \r\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span> FUserCollection <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">nil<\/span> <span style=\"color: #008800; font-weight: bold;\">then<\/span>\r\n    FUserCollection <span style=\"color: #333333;\">:=<\/span> TUserCollection<span style=\"color: #333333;\">.<\/span>Create<span style=\"color: #333333;\">;<\/span>   \r\n\r\n  <span style=\"color: #007020;\">Result<\/span> <span style=\"color: #333333;\">:=<\/span> FUserCollection<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span> \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This code can be improved for the compiler by moving the Create to another method, like this:<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GetUserCollection<\/span><span style=\"color: #333333;\">:<\/span> TUserCollection<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">begin<\/span>   \r\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span> FUserCollection <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">nil<\/span> <span style=\"color: #008800; font-weight: bold;\">then<\/span>\r\n    InitUserCollection<span style=\"color: #333333;\">;<\/span>   \r\n\r\n  <span style=\"color: #007020;\">Result<\/span> <span style=\"color: #333333;\">:=<\/span> FUserCollection<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span> \r\n\r\n<span style=\"color: #008800; font-weight: bold;\">procedure<\/span> <span style=\"color: #0066bb; font-weight: bold;\">InitUserCollection<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">begin<\/span>\r\n  FUserCollection <span style=\"color: #333333;\">:=<\/span> TUserCollection<span style=\"color: #333333;\">.<\/span>Create<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the first situation, the compiler takes into account that, in every call to GetUserCollection, it is possible that an object needs to be created. So-called activation records are prepared for this purpose. This is quite some overhead, especially if you call such a function in a loop. Unlike the optimised code, where the compiler only has to take into account the call to a procedure.<\/p>\n<p>In the MVP forum, this was measured and every call in the optimised code was executed just as fast. The non-optimised code, got slower and slower. Already at 20 calls, it took four times as long to call the procedure. The delay increased linearly, becoming more significant with each additional call.<br \/>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-5398\" src=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"\" width=\"944\" height=\"397\" srcset=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png 944w, https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image-300x126.png 300w, https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image-768x323.png 768w\" sizes=\"(max-width: 944px) 100vw, 944px\" \/><\/p>\n<p>This example and the const variable are two examples of how your way of coding affects what the compiler can or cannot optimize. With worse or better performance in the end.<\/p>\n","protected":false},"featured_media":0,"parent":0,"template":"","class_list":["post-5435","knowledge","type-knowledge","status-publish","hentry","knowledge-category-delphi-es"],"acf":{"author":1384,"type_hero":"compact","hero_image":5354,"hero_image_position":"","hero_title":"Const or not? Compiler optimization in coding","hero_content":"","hero_link":"","hero_show_h1":true},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Const or not? Compiler optimization in coding - GDK Software<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Const or not? Compiler optimization in coding\" \/>\n<meta property=\"og:description\" content=\"Is const for\u00a0a parameter useful or only for nerds and nitpickers? Should it be: procedure Check(const Text: string);? Or is procedure Check(Text: string); basically the same? Maybe you never ask yourself how the compiler handles your code. And what effects that has on your application. But if you know a bit about how this works [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding\" \/>\n<meta property=\"og:site_name\" content=\"GDK Software\" \/>\n<meta property=\"og:image\" content=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding\",\"name\":\"Const or not? Compiler optimization in coding - GDK Software\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/image.png\",\"datePublished\":\"2024-08-27T12:24:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding#primaryimage\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/image.png\",\"contentUrl\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2024\\\/08\\\/image.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\\\/const-or-not-compiler-optimization-in-coding#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/es\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledgebase\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/es\\\/knowledgebase\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Const or not? Compiler optimization in coding\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/es#website\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/es\",\"name\":\"GDK Software\",\"description\":\"Zet de stip op je horizon\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gdksoftware.com\\\/es?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Const or not? Compiler optimization in coding - GDK Software","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding","og_locale":"es_ES","og_type":"article","og_title":"Const or not? Compiler optimization in coding","og_description":"Is const for\u00a0a parameter useful or only for nerds and nitpickers? Should it be: procedure Check(const Text: string);? Or is procedure Check(Text: string); basically the same? Maybe you never ask yourself how the compiler handles your code. And what effects that has on your application. But if you know a bit about how this works [&hellip;]","og_url":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding","og_site_name":"GDK Software","og_image":[{"url":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Tiempo de lectura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding","url":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding","name":"Const or not? Compiler optimization in coding - GDK Software","isPartOf":{"@id":"https:\/\/gdksoftware.com\/es#website"},"primaryImageOfPage":{"@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding#primaryimage"},"image":{"@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding#primaryimage"},"thumbnailUrl":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png","datePublished":"2024-08-27T12:24:11+00:00","breadcrumb":{"@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding#primaryimage","url":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png","contentUrl":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2024\/08\/image.png"},{"@type":"BreadcrumbList","@id":"https:\/\/gdksoftware.com\/es\/knowledgebase\/const-or-not-compiler-optimization-in-coding#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gdksoftware.com\/es"},{"@type":"ListItem","position":2,"name":"Knowledgebase","item":"https:\/\/gdksoftware.com\/es\/knowledgebase"},{"@type":"ListItem","position":3,"name":"Const or not? Compiler optimization in coding"}]},{"@type":"WebSite","@id":"https:\/\/gdksoftware.com\/es#website","url":"https:\/\/gdksoftware.com\/es","name":"GDK Software","description":"Zet de stip op je horizon","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gdksoftware.com\/es?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"}]}},"_links":{"self":[{"href":"https:\/\/gdksoftware.com\/es\/wp-json\/wp\/v2\/knowledge\/5435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gdksoftware.com\/es\/wp-json\/wp\/v2\/knowledge"}],"about":[{"href":"https:\/\/gdksoftware.com\/es\/wp-json\/wp\/v2\/types\/knowledge"}],"acf:post":[{"embeddable":true,"href":"https:\/\/gdksoftware.com\/es\/wp-json\/wp\/v2\/team\/1384"}],"wp:attachment":[{"href":"https:\/\/gdksoftware.com\/es\/wp-json\/wp\/v2\/media?parent=5435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}