summaryrefslogtreecommitdiff
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-17 12:12:40 +1100
committerDamien George <damien.p.george@gmail.com>2017-02-17 12:12:40 +1100
commit534b7c368dc2af7720f3aaed0c936ef46d773957 (patch)
treee1b71ef80c0b883728a8871fe277da33b4889c14 /py/compile.c
parent773278ec3030ea9ed809c5a248fde2278ce4b557 (diff)
py: Do adjacent str/bytes literal concatenation in lexer, not compiler.
It's much more efficient in RAM and code size to do implicit literal string concatenation in the lexer, as opposed to the compiler. RAM usage is reduced because the concatenation can be done right away in the tokeniser by just accumulating the string/bytes literals into the lexer's vstr. Prior to this patch adjacent strings/bytes would create a parse tree (one node per string/bytes) and then in the compiler a whole new chunk of memory was allocated to store the concatenated string, which used more than double the memory compared to just accumulating in the lexer. This patch also significantly reduces code size: bare-arm: -204 minimal: -204 unix x64: -328 stmhal: -208 esp8266: -284 cc3200: -224
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c59
1 files changed, 0 insertions, 59 deletions
diff --git a/py/compile.c b/py/compile.c
index 3fb45cd69..15e757d46 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -2301,65 +2301,6 @@ STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t
}
}
-STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
- // a list of strings
-
- // check type of list (string or bytes) and count total number of bytes
- int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
- size_t n_bytes = 0;
- int string_kind = MP_PARSE_NODE_NULL;
- for (int i = 0; i < n; i++) {
- int pn_kind;
- if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
- pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
- assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
- n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
- } else {
- assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
- mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
- if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
- pn_kind = MP_PARSE_NODE_STRING;
- } else {
- assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
- pn_kind = MP_PARSE_NODE_BYTES;
- }
- n_bytes += pns_string->nodes[1];
- }
- if (i == 0) {
- string_kind = pn_kind;
- } else if (pn_kind != string_kind) {
- compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
- return;
- }
- }
-
- // if we are not in the last pass, just load a dummy object
- if (comp->pass != MP_PASS_EMIT) {
- EMIT_ARG(load_const_obj, mp_const_none);
- return;
- }
-
- // concatenate string/bytes
- vstr_t vstr;
- vstr_init_len(&vstr, n_bytes);
- byte *s_dest = (byte*)vstr.buf;
- for (int i = 0; i < n; i++) {
- if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
- size_t s_len;
- const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
- memcpy(s_dest, s, s_len);
- s_dest += s_len;
- } else {
- mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
- memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
- s_dest += pns_string->nodes[1];
- }
- }
-
- // load the object
- EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
-}
-
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);