<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Imad Zerrouq</title>
      <link>https://imzerro.org</link>
      <description></description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://imzerro.org/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 20 Sep 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Generating GUI keys from return addresses</title>
          <pubDate>Sun, 20 Sep 2026 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://imzerro.org/posts/generating-gui-keys-from-return-addresses/</link>
          <guid>https://imzerro.org/posts/generating-gui-keys-from-return-addresses/</guid>
          <description xml:base="https://imzerro.org/posts/generating-gui-keys-from-return-addresses/">&lt;p&gt;Lately I have gone down the rabbit hole of immediate-mode GUI, and decided to write my own imgui code for my own projects that does layout computations and issues draw commands to the renderer.
In the process I figured out a very nice way to generate keys for persistent state that I have not seen used anywhere and wanted to share it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;TL;DR: if your widgets are functions and you manage to make your compiler never inline them, you can use the return address as a key for storing state across frames.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;what-is-immediate-mode-gui&quot;&gt;What is immediate-mode GUI?&lt;/h2&gt;
&lt;p&gt;Immediate-mode GUI, or imgui for short, is a style of writing GUI code where the user specifies each GUI element to draw directly inside the render loop, every frame, using an immediate-mode API.
Put simply, the UI view/layout is recomputed every frame, dynamically, based on the user&#39;s widget specifications.
Imgui code tends to look something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{ // Inside the render loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  bool pressed = button(&amp;quot;my button label&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  if pressed {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    do_something_cool(); &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a simplified version, because you still need to pass layout information (height, width, color...) to your button draw function somehow, but I won&#39;t be talking about that in this article.&lt;/p&gt;
&lt;p&gt;This style of writing GUI code was originally demonstrated by &lt;a rel=&quot;external&quot; href=&quot;https://caseymuratori.com/blog_0001&quot;&gt;Casey Muratori&lt;/a&gt; and later got popularized by open source libraries such as &lt;a rel=&quot;external&quot; href=&quot;https://github.com/ocornut/imgui/tree/master&quot;&gt;Dear ImGui&lt;/a&gt;.
The benefit of this style of coding, as opposed to traditional &lt;em&gt;retained mode&lt;/em&gt; (where the UI layout is mostly static/pre-allocated and mutated upon user interaction), is that your UI looks and feels reactive and dynamic, it also tends to render correctly with no visual bugs.&lt;/p&gt;
&lt;p&gt;The drawback is that you are recomputing your UI layout every frame, but this isn&#39;t an issue for modern processors, they are so fast that even a layout of more than 1000 widgets can be computed in under a milliseconds, that is if you code it correctly and don&#39;t make your processor chase data pointers spread all over memory.&lt;/p&gt;
&lt;h2 id=&quot;why-widgets-need-keys&quot;&gt;Why widgets need keys&lt;/h2&gt;
&lt;p&gt;Some widgets data can&#39;t just be recomputed it must be stored across frames for the widget to behave correctly. A checkbox, for example, needs to keep track of whether it&#39;s checked or not, a scroll region needs to save the scroll progress, and so on.
The responsibility of allocating and preserving the state for some of these widgets can be placed on the user. The checkbox, only needs a boolean variable that outlives the frame lifetime, and the user can allocate it outside that scope and pass it as pointer to the checkbox function.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;// global namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bool is_checkbox_checked = false;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{ // Inside the render loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  checkbox(&amp;quot;check me&amp;quot;, &amp;amp;is_checkbox_checked);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  if is_checkbox_checked  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    make_user_happy();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, in some cases it&#39;s too cumbersome for the user to keep track of the widget state, especially when it serves them no purpose. In the case of a scroll region the user has no use for the scroll progress or the size of the content that has been clipped from view.
It&#39;s also more convenient for the GUI code to manage this state internally and have access to it whenever.
To do this we need a way to correlate every widget call site with its state across frames.
The most obvious solution is a key-value lookup table or a hashmap that stores the widget state and allows for quick access to it through unique keys.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;struct UiState {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  float2 scroll_view;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  float2 scroll_offset;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  float animation_rate;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // you can aggregate more properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // that need to be saved inside this struct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Hashmap&amp;lt;UiState&amp;gt; state_map;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;function widget(...){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  WidgetKey key = ?? ; // what do i use for this ?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  widget_state = state_map.get(key);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // remaining widget logic code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;NOTE: I use &lt;code&gt;...&lt;/code&gt; to omit function parameters and unnecessary details.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;OK, the state map is in place. Now, how do I get unique keys? Well, let&#39;s look at the existing solutions.&lt;/p&gt;
&lt;h2 id=&quot;common-ways-to-generate-keys&quot;&gt;Common ways to generate keys&lt;/h2&gt;
&lt;h3 id=&quot;1-let-the-user-handle-and-supply-keys&quot;&gt;1. Let the user handle and supply keys&lt;/h3&gt;
&lt;p&gt;One way to do it is to just ask the user to come up with some keying mechanism and pass it to the function.
This, however, is a very bad choice, because:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The user needs to put in extra mental effort to produce these keys.&lt;/li&gt;
&lt;li&gt;The correctness of your code now relies on the user doing everything right with no mistakes (which is usually not the case).&lt;/li&gt;
&lt;li&gt;It&#39;s just not a fun experience for the user at all, and software should be &lt;strong&gt;FUN&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So overall not a good solution. Moving on.&lt;/p&gt;
&lt;h3 id=&quot;2-per-frame-counter&quot;&gt;2. Per-frame counter&lt;/h3&gt;
&lt;p&gt;Another naive solution that comes to mind early is using a serial integer as the key: say we keep track of the widget count and use it as the widget key.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;function widget(...){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  widget_count = widget_count + 1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  WidgetKey key = widget_count;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  widget_state = state_map.get(key);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // remaining widget code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works fine, plus in case of a hashmap collision we only need to compare integers to find the value, not very costly in terms of performance.
However, this solution breaks as soon as a layout change happens. Consider the following example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bool agrees_to_tos = false;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{ // render loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  checkbox(&amp;quot;I agree to the terms of service&amp;quot;, &amp;amp;agrees_to_tos);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  if agrees_to_tos {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if button(&amp;quot;Start&amp;quot;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      launch_program();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  if button(&amp;quot;Exit&amp;quot;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    exit_program();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now what key value does the &lt;strong&gt;Exit&lt;/strong&gt; button get? Well, that depends: if &lt;code&gt;agrees_to_tos&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; the key value is &lt;strong&gt;2&lt;/strong&gt;, otherwise it&#39;s &lt;strong&gt;3&lt;/strong&gt; and the &lt;strong&gt;Start&lt;/strong&gt; button gets the key value &lt;strong&gt;2&lt;/strong&gt;.
And therefore both buttons access the wrong state and your UI starts looking weird.&lt;/p&gt;
&lt;h3 id=&quot;3-hashing-string-labels&quot;&gt;3. Hashing string labels&lt;/h3&gt;
&lt;p&gt;This one is pretty robust in most cases. It consists of asking the user for a string label that you then pass to a fast hash function to produce a key.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // function definition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  function widget(string_key,...) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget_state = state_map.get(compute_hash(string_key));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    // rest of the widget code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  { // render loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget(&amp;quot;my 1st widget&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget(&amp;quot;my 2nd widget&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget(&amp;quot;my 3rd widget&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This solution isn&#39;t affected by layout changes, also &lt;a rel=&quot;external&quot; href=&quot;https://github.com/ocornut/imgui/tree/master&quot;&gt;Dear ImGui&lt;/a&gt; uses a strategy similar to this one and it works. There are however some caveats to keep in mind.
First, it is again the responsibility of the user to give each widget an original unique string key (and I certainly don&#39;t have that ability just look at what I did up there in the previous example).
Another issue is loops. Consider the following example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  for i = 0 to 100 {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    list_item(&amp;quot;list_item&amp;quot;, some_list_data[i]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Can you see the problem? All 100 &lt;code&gt;list_items&lt;/code&gt; have the same string key and therefore the same state, so you click one and you end up clicking all of them. Your UI is broken, your software is buggy, and your users are unhappy :(
You might think that loops are not really necessary in UI code, but trust me they are. Just look at your email application: how do you think it displays the list of emails in your inbox?
In cases like these you just can&#39;t use a static string as a key, but instead need to dynamically generate them on the fly with information unique to the loop, and an obvious one is the loop index &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  for i = 0 to 100 {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    // now each item gets its own unique key.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    list_item(format(&amp;quot;list_item#%d&amp;quot;, i), some_data_to_display[i]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One more issue arises when composing widgets. Consider the following situation:
you want to refactor some common widget code into a single function that you can reuse so you did something like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  void big_widget(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget_a(&amp;quot;BIG_A&amp;quot;,...);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget_b();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if(widget_c(&amp;quot;BIG_C&amp;quot;,...)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      widget_d();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  { // inside your frame loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    big_widget();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    big_widget();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem now is that the widgets inside &lt;code&gt;big_widget&lt;/code&gt; are using the same state every time you call that function.
You can solve this by passing more information to &lt;code&gt;big_widget&lt;/code&gt; like a prefix string that gets concatenated with the widget key string.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  void big_widget(prefix){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget_a(CONCAT(prefix,&amp;quot;BIG_A&amp;quot;),...);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    widget_b();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if(widget_c(CONCAT(prefix,&amp;quot;BIG_C&amp;quot;),...)){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      widget_d();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  { // inside your frame loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    big_widget(&amp;quot;Big 1&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    big_widget(&amp;quot;Big 2&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But overall this remains a good robust keying strategy.
I, however, don&#39;t quite like it, coming up with string labels requires some engagement and thinking on my side to keep the labels unique, and I&#39;m honestly too lazy for this.
Luckily for me, there is another unique number that just gets passed automatically to every function at the machine code level and that I can derive a key from.
And that number is called &lt;strong&gt;the return address of the function&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Quick aside: if you need a quick explanation of how return addresses work, I wrote &lt;a href=&quot;https://imzerro.org/posts/generating-gui-keys-from-return-addresses/#appendix-a-quick-primer-on-return-addresses&quot;&gt;a brief explanation at the end of this post&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;return-addresses-as-keys&quot;&gt;Return addresses as keys&lt;/h2&gt;
&lt;p&gt;So when writing imgui code you tend to declare your widget building code as functions, as we have done so far.
So to get a key you just need to get the return address that the processor saved before jumping to your function.&lt;/p&gt;
&lt;p&gt;How can you do that? Well, it depends on your programming language&#39;s features. If you are using a systems programming language, there is possibly a way.
And if your language allows you to just write raw assembly then you can absolutely do this, however in most popular systems languages writing assembly is not necessary.
But if you&#39;re using some interpreted language like Python, or some VM-imprisoned language like Java, I am not sure it&#39;s possible.
In &lt;strong&gt;C/C++&lt;/strong&gt; there is a non-standard GCC/Clang intrinsic &lt;code&gt;__builtin_return_address&lt;/code&gt; that you can use:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(...) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  void&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;*&lt;/span&gt;&lt;span&gt; addr &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; __builtin_return_address&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt; // on MSVC there is _ReturnAddress() from &amp;lt;intrin.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span&gt; key &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span&gt;addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  UiState button_state &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; get_or_make_state&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // The rest of your logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;strong&gt;Zig&lt;/strong&gt; you can use the &lt;code&gt;@returnAddress&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; button&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;...&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; @returnAddress&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; button_state&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; get_or_make_state&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;key&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // The rest of your logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;strong&gt;Rust&lt;/strong&gt; there is currently an unstable &lt;code&gt;std::intrinsics&lt;/code&gt; function called &lt;code&gt;return_address&lt;/code&gt; that says it can do this,
but I have never tried it.&lt;/p&gt;
&lt;p&gt;However, there is a caveat here: when shipping code your compiler might choose to &lt;em&gt;inline&lt;/em&gt; your functions as an optimization.
Inlining a function just copies its code into the caller&#39;s code, and this way it avoids emitting a &lt;code&gt;call&lt;/code&gt; instruction.
If this were to happen, your function&#39;s return address becomes the caller&#39;s return address and is no longer guaranteed to be unique. Consider this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; show_ui&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(...) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // some ui code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;   button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;Prev&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;   button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;Next&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If these 2 button function calls get inlined they end up with the same return address, which is the return address of &lt;code&gt;show_ui&lt;/code&gt;, and consequently the same key and the same state.
And this breaks your UI logic.
The easy way to solve this is to tell your compiler to never inline your functions.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;C/C++&lt;/strong&gt; use the &lt;code&gt;noinline&lt;/code&gt; attribute:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; __attribute__&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;((&lt;/span&gt;&lt;span&gt;noinline&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(...) {&lt;/span&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt; //NOTE: if you are using the Microsoft MSVC toolchain it&amp;#39;s __declspec(noinline)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  void&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;*&lt;/span&gt;&lt;span&gt; addr &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; __builtin_return_address&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span&gt; key &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span&gt;addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  UiState button_state &lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; get_or_make_state&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // The rest of your logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;strong&gt;Zig&lt;/strong&gt; use the &lt;code&gt;noinline&lt;/code&gt; keyword:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;noinline&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; fn&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; button&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;...&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;void&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; @returnAddress&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // The rest of your logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;strong&gt;Rust&lt;/strong&gt; use this to disable inlining:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#[&lt;/span&gt;&lt;span&gt;inline&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;never&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // The rest of your button logic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Perfect, that solves the inlining problem. Now our keys are guaranteed to be unique, since the &lt;code&gt;call&lt;/code&gt; instruction is also unique in the machine code,
and best of all they are generated without any mental effort from the user, and since they fit into a 64-bit integer we only pay for an integer comparison upon collision.
Now, it&#39;s not all sunshine and rainbows; there&#39;s still more to do to make this robust.
But what about loops?&lt;/p&gt;
&lt;h3 id=&quot;widgets-in-loops&quot;&gt;Widgets in loops&lt;/h3&gt;
&lt;p&gt;Consider the following code:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt; // ui code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    checkbox&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All our checkboxes will get the same return address and therefore the same key. This is bad, but we can fix it.
We will just OR a loop index into the upper bits of the return address. You see, while our function return address is a 64-bit value (or 32-bit on 32-bit processors),
only the lower bits tend to change in return addresses. That&#39;s because when your program&#39;s instructions region is mapped to memory it occupies a single contiguous block of memory, so addresses tend to be serial.
Also, this contiguous block of memory is often not that big, usually less than 100 MiB, so really the upper bits stay kind of constant in the return address and don&#39;t change at all
throughout your program&#39;s execution, so it&#39;s safe to replace them with actually important information like our loop index.&lt;/p&gt;
&lt;p&gt;Let&#39;s implement our UI key logic.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; RET_ADDRESS __builtin_return_address&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;  32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UI_KEY_MASK&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  ((&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;ull&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; update&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;void*&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; uaddr&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; uaddr&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_MASK&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  private&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; __attribute__((&lt;/span&gt;&lt;span&gt;noinline&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; checkbox&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;UiKey&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;  key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;RET_ADDRESS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiState&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; checkbox_state&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; get_or_make_state&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  // remaining checkbox building code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;// somewhere in our ui code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    checkbox&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;i&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Impressive, very nice, but what about composing widgets?&lt;/p&gt;
&lt;h3 id=&quot;composing-widgets&quot;&gt;Composing widgets&lt;/h3&gt;
&lt;p&gt;It is inevitable that you would want to reuse your simple widgets to compose more complex widgets, for example a counter made of a  numeric input field and the increment/decrement buttons.
Well, since these 3 widgets are stateful you would need 3 different keys for this composition to work, and a function only has one return address, so how do we solve this?
The answer is the same as before: reserve more upper bits and generate the keys at the start of the function.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;NOTE: Any reusable widget code must also be a non-inlined function for this to work.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; RET_ADDRESS __builtin_return_address&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;  32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UI_KEY_CHILD_BITS&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;  4&lt;/span&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt; /* 16 children is more than enough, this leaves us with 28 address bits */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UI_KEY_MASK&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  ((&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;ull&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_CHILD_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;():&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; child_id_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; child_id_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; child_id&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; child_id_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;child_id&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; update&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;void*&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; uaddr&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;uaddr&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_MASK&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;  /* New */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey new_child&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;child_id_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;child_id_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; ((&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_CHILD_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    UiKey new_key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;child_id_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; new_key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;loop_index_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_CHILD_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;child_id_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;UI_KEY_ADDR_BITS&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UI_KEY_CHILD_BITS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;    v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;addr_&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  private&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  uint64_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; addr_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; INVALID_ADDR&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; loop_index_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;  int32_t&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; child_id_&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt; __attribute__((&lt;/span&gt;&lt;span&gt;noinline&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt; counter&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;UiKey&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FE8019;&quot;&gt;const char*&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;  key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;RET_ADDRESS&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; inc_btn_key&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;new_child&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; dec_btn_key&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;new_child&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  UiKey&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; input_key&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;new_child&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  start_row&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;    // Assume button and input_numeric widgets are already defined somewhere&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;dec_btn_key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;, &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    input_numeric&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;input_key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;buffer&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;    button&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;inc_btn_key&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;, &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  end_row&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #928374;font-style: italic;&quot;&gt;// somewhere in our ui code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  counter&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;{}&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;, &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;Counter A&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FABD2F;&quot;&gt;  counter&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;(&lt;/span&gt;&lt;span&gt;{}&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;, &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #B8BB26;&quot;&gt;Counter B&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;demo&quot;&gt;Demo&lt;/h2&gt;
&lt;p&gt;Time for a demonstration, the UI in the following video is built entirely with the concept of using the return address as a keying mechanism.&lt;/p&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/U-hCtN7uryI?si=5r_QhFsuGkbs1cmZ&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;limitations&quot;&gt;Limitations&lt;/h2&gt;
&lt;p&gt;Now, of course, this technique does have a limitation: in general you will find friction when trying to compose complex widgets into bigger, reusable, more complex widgets.
I would make the claim, however, that composing complex widgets together is not a sign of good UI, as good UI in my opinion tends to be more visually simple and easy on the eye.
You also need to remember to declare your functions with the &lt;code&gt;noinline&lt;/code&gt; attribute, you can make your developer experience better by adding checks for key uniqueness in debug mode.&lt;/p&gt;
&lt;h2 id=&quot;side-notes&quot;&gt;Side notes&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;UiKey&lt;/code&gt; struct fields can be packed into a 64-bit integer to reduce its memory footprint,
and depending on your programming language&#39;s ergonomics they can still be accessed easily.
In Zig for example you can do this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #EBDBB2; background-color: #1D2021;&quot; &gt;&lt;code data-lang=&quot;zig&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;pub const&lt;/span&gt;&lt;span style=&quot;color: #83A598;&quot;&gt; UiKey&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt; packed struct&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt;u64&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;  ret_addr&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt; u28&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;  child_id&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt; u4&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #83A598;&quot;&gt;  loop_index&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FB4934;&quot;&gt; u32&lt;/span&gt;&lt;span style=&quot;color: #8EC07C;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #D3869B;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A89984;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;How you reserve the bits is up to you, the most important decision is the number of return address bits, and that should be big enough so as not to lose call site information.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;/h2&gt;
&lt;p&gt;So that is the whole idea: derive your widget keys from the function return address.
You get keys that cost the user no mental effort at all, that survive layout changes, that fit in a single 64-bit integer so a hashmap collision only costs you an integer comparison,
and that need nothing from your language beyond an intrinsic most systems languages already hand you.
The price is three things to keep in mind: mark your widget functions &lt;code&gt;noinline&lt;/code&gt;, reserve some upper bits for a loop index when you build widgets in a loop,
and accept that complex widget composition is where the scheme breaks down.&lt;/p&gt;
&lt;p&gt;I have been using this in my own projects and it has been a far nicer experience than naming every stateful widget by hand.
This approach might not be the best for shipping an open source library, but for my case it does the job perfectly.
If you end up trying it in a language I didn&#39;t cover here, or you find a nicer way around the composition limit, I would be glad to hear about it.&lt;/p&gt;
&lt;h2 id=&quot;appendix-a-quick-primer-on-return-addresses&quot;&gt;Appendix: a quick primer on return addresses&lt;/h2&gt;
&lt;p&gt;This is a quick explanation of how return addresses work at the machine code level, for anyone who wants to see the ground underneath the trick above.&lt;/p&gt;
&lt;p&gt;When you compile a program&#39;s source code, the compiler generates machine code for the target processor. This machine code is just a contiguous block of bit patterns that the processor interprets as the instructions it needs to run the specified logic of your program.&lt;/p&gt;
&lt;p&gt;Trying to identify bit patterns is very hard for us humans, and that&#39;s where assembly comes in: assembly maps these bit patterns to mnemonics that are easy to read and understand.
We tend to use these mnemonics as names for the CPU instructions, and for the most part they describe very well what the CPU does.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;add&lt;/code&gt;: add 2 numbers.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sub&lt;/code&gt;: subtract 2 numbers.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mov&lt;/code&gt;: copy a value between memory and registers.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;jmp&lt;/code&gt;: jump to a different instruction in the machine code.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The one instruction we care about here is the &lt;code&gt;jmp&lt;/code&gt; instruction: it tells the processor to jump to another instruction in the machine code.
This could be the instruction right after it, or one somewhere far from the current instruction; the &lt;code&gt;jmp&lt;/code&gt; specifies exactly where, using the address of the instruction it wants the processor to jump to.
What is an &lt;strong&gt;address&lt;/strong&gt;, you might ask? You see, the machine code is usually loaded into your computer&#39;s memory, and each byte gets its own address, which is just a number counted from the beginning of the computer memory, with that beginning having an address of 0.
You just add 1 to the address to get the address of the next byte.&lt;/p&gt;
&lt;p&gt;Now usually your computer executes instructions in serial order, moving from one instruction to the one after it, however for some logic the serial order doesn&#39;t work.
For example, if you have an &lt;code&gt;if&lt;/code&gt; branch, then depending on the condition your computer needs to execute either instruction A or instruction B.
This is where &lt;code&gt;jmp&lt;/code&gt; instructions comes in handy.&lt;/p&gt;
&lt;p&gt;Functions are another reason &lt;code&gt;jmp&lt;/code&gt;s are useful, because your processor needs to jump to that function&#39;s code, execute it, and then come back.
Jumping to a function, however, doesn&#39;t use the &lt;code&gt;jmp&lt;/code&gt; instruction, because the processor needs to remember where to come back to. It instead uses another
instruction, usually named &lt;code&gt;call&lt;/code&gt;, that saves the &lt;strong&gt;return address&lt;/strong&gt; before jumping to the function&#39;s code. This return address is simply the address of the instruction immediately after the function call.&lt;/p&gt;
&lt;p&gt;This is all we needed to understand the keying trick, so we will wrap this tangent up here.&lt;/p&gt;
&lt;p&gt;For more information about the subject checkout this &lt;a rel=&quot;external&quot; href=&quot;https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64&quot;&gt;amazing blog post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;-Imad&lt;/p&gt;
</description>
      </item>
      <item>
          <title>Hello</title>
          <pubDate>Tue, 08 Sep 2026 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://imzerro.org/posts/hello/</link>
          <guid>https://imzerro.org/posts/hello/</guid>
          <description xml:base="https://imzerro.org/posts/hello/">&lt;p&gt;Hi everyone :)&lt;/p&gt;
</description>
      </item>
    </channel>
</rss>
