From 3dea8c9e926d0c1b1049ff9d5abeedfb001fc201 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Fri, 30 Sep 2016 12:34:05 +1000
Subject: [PATCH] py/scope: Use lookup-table to determine a scope's simple
 name.

Generates slightly smaller and more efficient code.
---
 py/compile.c    |  2 +-
 py/emitcommon.c |  8 ++++----
 py/scope.c      | 41 +++++++++++++++--------------------------
 py/scope.h      | 14 +++++++++++++-
 4 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/py/compile.c b/py/compile.c
index 3039f98b5..2253ccd3f 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -3288,7 +3288,7 @@ STATIC void scope_compute_things(scope_t *scope) {
             // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
             continue;
         }
-        if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
+        if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
             id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
         }
         // params always count for 1 local, even if they are a cell
diff --git a/py/emitcommon.c b/py/emitcommon.c
index 435188f36..2925f4caf 100644
--- a/py/emitcommon.c
+++ b/py/emitcommon.c
@@ -50,12 +50,12 @@ void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
     bool added;
     id_info_t *id = scope_find_or_add_id(scope, qst, &added);
     if (added) {
-        if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
-            id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
-        } else {
+        if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
             id->kind = ID_INFO_KIND_LOCAL;
+        } else {
+            id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
         }
-    } else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
+    } else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
         // rebind as a local variable
         id->kind = ID_INFO_KIND_LOCAL;
     }
diff --git a/py/scope.c b/py/scope.c
index 632e52752..d080fbcbf 100644
--- a/py/scope.c
+++ b/py/scope.c
@@ -30,37 +30,26 @@
 
 #if MICROPY_ENABLE_COMPILER
 
+// these low numbered qstrs should fit in 8 bits
+STATIC const uint8_t scope_simple_name_table[] = {
+    [SCOPE_MODULE] = MP_QSTR__lt_module_gt_,
+    [SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_,
+    [SCOPE_LIST_COMP] = MP_QSTR__lt_listcomp_gt_,
+    [SCOPE_DICT_COMP] = MP_QSTR__lt_dictcomp_gt_,
+    [SCOPE_SET_COMP] = MP_QSTR__lt_setcomp_gt_,
+    [SCOPE_GEN_EXPR] = MP_QSTR__lt_genexpr_gt_,
+};
+
 scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
     scope_t *scope = m_new0(scope_t, 1);
     scope->kind = kind;
     scope->pn = pn;
     scope->source_file = source_file;
-    switch (kind) {
-        case SCOPE_MODULE:
-            scope->simple_name = MP_QSTR__lt_module_gt_;
-            break;
-        case SCOPE_FUNCTION:
-        case SCOPE_CLASS:
-            assert(MP_PARSE_NODE_IS_STRUCT(pn));
-            scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
-            break;
-        case SCOPE_LAMBDA:
-            scope->simple_name = MP_QSTR__lt_lambda_gt_;
-            break;
-        case SCOPE_LIST_COMP:
-            scope->simple_name = MP_QSTR__lt_listcomp_gt_;
-            break;
-        case SCOPE_DICT_COMP:
-            scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
-            break;
-        case SCOPE_SET_COMP:
-            scope->simple_name = MP_QSTR__lt_setcomp_gt_;
-            break;
-        case SCOPE_GEN_EXPR:
-            scope->simple_name = MP_QSTR__lt_genexpr_gt_;
-            break;
-        default:
-            assert(0);
+    if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) {
+        assert(MP_PARSE_NODE_IS_STRUCT(pn));
+        scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
+    } else {
+        scope->simple_name = scope_simple_name_table[kind];
     }
     scope->raw_code = mp_emit_glue_new_raw_code();
     scope->emit_options = emit_options;
diff --git a/py/scope.h b/py/scope.h
index fac936a72..23a863ac5 100644
--- a/py/scope.h
+++ b/py/scope.h
@@ -52,8 +52,20 @@ typedef struct _id_info_t {
     qstr qst;
 } id_info_t;
 
+#define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA)
+
 // scope is a "block" in Python parlance
-typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOPE_DICT_COMP, SCOPE_SET_COMP, SCOPE_GEN_EXPR, SCOPE_CLASS } scope_kind_t;
+typedef enum {
+    SCOPE_MODULE,
+    SCOPE_CLASS,
+    SCOPE_LAMBDA,
+    SCOPE_LIST_COMP,
+    SCOPE_DICT_COMP,
+    SCOPE_SET_COMP,
+    SCOPE_GEN_EXPR,
+    SCOPE_FUNCTION,
+} scope_kind_t;
+
 typedef struct _scope_t {
     scope_kind_t kind;
     struct _scope_t *parent;
-- 
GitLab