Merge: Interpreter FFI: custom compile_dir, use /tmp/ by default, and delete after...
authorJean Privat <jean@pryen.org>
Thu, 10 Mar 2016 04:55:19 +0000 (23:55 -0500)
committerJean Privat <jean@pryen.org>
Thu, 10 Mar 2016 04:55:19 +0000 (23:55 -0500)
Generate native files in the `/tmp/` directory and delete them after execution. The directory can be customized using `--compile-dir`.

This should fix #1974 and #1975.

To support some other OS, we will need to use something other than `getpid` and `/tmp/`. We could also improve upon this by reusing the generated libraries between executions.

Pull-Request: #1976
Reviewed-by: Jean Privat <jean@pryen.org>

benchmarks/markdown/engines/nitmd/nitmd.nit
lib/core/text/flat.nit
lib/markdown/markdown.nit
lib/markdown/test_markdown.nit
src/compiler/compiler_ffi/compiler_ffi.nit
src/nitni/nitni_utilities.nit
tests/sav/nitcg/test_text_stat.res
tests/sav/test_text_stat.res

index 2db91b2..09c7d07 100644 (file)
@@ -19,6 +19,7 @@ var n = args[1].to_i
 
 var str = file.to_path.read_all
 var parser = new MarkdownProcessor
+parser.no_location = true
 for i in [1..n] do
        print parser.process(str)
 end
index 47b8608..8081ecc 100644 (file)
@@ -52,20 +52,25 @@ redef class FlatText
        fun char_to_byte_index(index: Int): Int do
                var dpos = index - _position
                var b = _bytepos
+               var its = _items
 
-               if dpos == 0 then return b
                if dpos == 1 then
-                       b += _items.length_of_char_at(b)
+                       if its[b] & 0x80u8 == 0x00u8 then
+                               b += 1
+                       else
+                               b += its.length_of_char_at(b)
+                       end
                        _bytepos = b
                        _position = index
                        return b
                end
                if dpos == -1 then
-                       b = _items.find_beginning_of_char_at(b - 1)
+                       b = its.find_beginning_of_char_at(b - 1)
                        _bytepos = b
                        _position = index
                        return b
                end
+               if dpos == 0 then return b
 
                var ln = _length
                var pos = _position
@@ -74,7 +79,6 @@ redef class FlatText
                var delta_end = (ln - 1) - index
                var delta_cache = (pos - index).abs
                var min = delta_begin
-               var its = _items
 
                if delta_cache < min then min = delta_cache
                if delta_end < min then min = delta_end
@@ -292,7 +296,49 @@ redef class FlatText
        end
 
        redef fun [](index) do
-               assert index >= 0 and index < _length
+               var len = _length
+
+               # Statistically:
+               # * ~70% want the next char
+               # * ~23% want the previous
+               # * ~7% want the same char
+               #
+               # So it makes sense to shortcut early. And early is here.
+               var dpos = index - _position
+               var b = _bytepos
+               if dpos == 1 and index < len - 1 then
+                       var its = _items
+                       var c = its[b]
+                       if c & 0x80u8 == 0x00u8 then
+                               # We want the next, and current is easy.
+                               # So next is easy to find!
+                               b += 1
+                               _position = index
+                               _bytepos = b
+                               # The rest will be done by `dpos==0` bellow.
+                               dpos = 0
+                       end
+               else if dpos == -1 and index > 1 then
+                       var its = _items
+                       var c = its[b-1]
+                       if c & 0x80u8 == 0x00u8 then
+                               # We want the previous, and it is easy.
+                               b -= 1
+                               dpos = 0
+                               _position = index
+                               _bytepos = b
+                               return c.ascii
+                       end
+               end
+               if dpos == 0 then
+                       # We know what we want (+0 or +1) just get it now!
+                       var its = _items
+                       var c = its[b]
+                       if c & 0x80u8 == 0x00u8 then return c.ascii
+                       return items.char_at(b)
+               end
+
+               assert index >= 0 and index < len
                return fetch_char_at(index)
        end
 
@@ -1250,6 +1296,10 @@ redef class NativeString
        #
        # Very unsafe, make sure to have room for this char prior to calling this function.
        private fun set_char_at(pos: Int, c: Char) do
+               if c.code_point < 128 then
+                       self[pos] = c.code_point.to_b
+                       return
+               end
                var ln = c.u8char_len
                native_set_char(pos, c, ln)
        end
index 4c2428f..d63f3e4 100644 (file)
@@ -133,6 +133,14 @@ class MarkdownProcessor
        # ~~~
        var ext_mode = true
 
+       # Disable attaching MDLocation to Tokens
+       #
+       # Locations are useful for some tools but they may
+       # cause an important time and space overhead.
+       #
+       # Default = `false`
+       var no_location = false is writable
+
        init do self.emitter = new MarkdownEmitter(self)
 
        # Process the mardown `input` string and return the processed output.
@@ -397,11 +405,16 @@ class MarkdownProcessor
                        c2 = ' '
                end
 
-               var loc = new MDLocation(
-                       current_loc.line_start,
-                       current_loc.column_start + pos,
-                       current_loc.line_start,
-                       current_loc.column_start + pos)
+               var loc
+               if no_location then
+                       loc = null
+               else
+                       loc = new MDLocation(
+                               current_loc.line_start,
+                               current_loc.column_start + pos,
+                               current_loc.line_start,
+                               current_loc.column_start + pos)
+               end
 
                if c == '*' then
                        if c1 == '*' then
@@ -610,10 +623,12 @@ class MarkdownEmitter
        end
 
        # Append `c` to current buffer.
-       fun addc(c: Char) do add c.to_s
+       fun addc(c: Char) do
+               current_buffer.add c
+       end
 
        # Append a "\n" line break.
-       fun addn do add "\n"
+       fun addn do addc '\n'
 end
 
 # A Link Reference.
@@ -1938,7 +1953,7 @@ end
 abstract class Token
 
        # Location of `self` in the original input.
-       var location: MDLocation
+       var location: nullable MDLocation
 
        # Position of `self` in input independant from lines.
        var pos: Int
@@ -2330,18 +2345,11 @@ redef class Text
                        if c == '\\' and pos + 1 < length then
                                pos = escape(out, self[pos + 1], pos)
                        else
-                               var end_reached = false
-                               for n in nend do
-                                       if c == n then
-                                               end_reached = true
-                                               break
-                                       end
-                               end
-                               if end_reached then break
+                               for n in nend do if c == n then break label
                                out.add c
                        end
                        pos += 1
-               end
+               end label
                if pos == length then return -1
                return pos
        end
index 53c355f..8a2c7a0 100644 (file)
@@ -2830,7 +2830,7 @@ class TestTokenProcessor
        redef fun token_at(input, pos) do
                var token = super
                if token isa TokenNone then return token
-               var res = "{token.class_name} at {token.location}"
+               var res = "{token.class_name} at {token.location or else "?"}"
                var exp = test_stack.shift
                print ""
                print "EXP {exp}"
index 79c2f14..7a536b1 100644 (file)
@@ -262,18 +262,20 @@ redef class MExplicitCast
                #
 
                # In nitni files, declare internal function as extern
-               var full_friendly_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
+               var full_friendly_csignature = "int {v.compiler.mainmodule.c_name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
                ccu.header_decl.add("extern {full_friendly_csignature};\n")
 
                # In nitni files, #define friendly as extern
-               ccu.header_decl.add("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
+               ccu.header_decl.add "#ifndef {check_cname}\n"
+               ccu.header_decl.add "#define {check_cname} {v.compiler.mainmodule.c_name}___{check_cname}\n"
+               ccu.header_decl.add "#endif\n"
 
                if compile_implementation_too then
                        # Internally, implement internal function
                        var nitni_visitor = v.compiler.new_visitor
                        nitni_visitor.frame = v.frame
 
-                       var full_internal_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
+                       var full_internal_csignature = "int {v.compiler.mainmodule.c_name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
 
                        nitni_visitor.add_decl("/* nitni check for {from} to {to} */")
                        nitni_visitor.add_decl("{full_internal_csignature} \{")
@@ -289,7 +291,9 @@ redef class MExplicitCast
                # special checks
                if from == to.as_nullable then
                        # format A_is_null
-                       ccu.header_decl.add("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
+                       ccu.header_decl.add "#ifndef {from.mangled_cname}_is_null\n"
+                       ccu.header_decl.add "#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n"
+                       ccu.header_decl.add "#endif\n"
                end
 
                #
@@ -297,18 +301,20 @@ redef class MExplicitCast
                #
 
                # In nitni files, declare internal function as extern
-               full_friendly_csignature = "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
+               full_friendly_csignature = "{to.cname_blind} {v.compiler.mainmodule.c_name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
                ccu.header_decl.add("extern {full_friendly_csignature};\n")
 
                # In nitni files, #define friendly as extern
-               ccu.header_decl.add("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
+               ccu.header_decl.add "#ifndef {cast_cname}\n"
+               ccu.header_decl.add "#define {cast_cname} {v.compiler.mainmodule.c_name}___{cast_cname}\n"
+               ccu.header_decl.add "#endif\n"
 
                if compile_implementation_too then
                        # Internally, implement internal function
                        var nitni_visitor = v.compiler.new_visitor
                        nitni_visitor.frame = v.frame
 
-                       var full_internal_csignature = "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
+                       var full_internal_csignature = "{to.cname_blind} {v.compiler.mainmodule.c_name }___{from.mangled_cname}_as_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
                        nitni_visitor.add_decl("/* nitni cast for {from} to {to} */")
                        nitni_visitor.add_decl("{full_internal_csignature} \{")
 
@@ -333,12 +339,16 @@ redef class MExplicitCast
                # special casts
                if from.as_nullable == to then
                        # format A_as_nullable
-                       ccu.header_decl.add("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
+                       ccu.header_decl.add "#ifndef {from.mangled_cname}_as_nullable\n"
+                       ccu.header_decl.add "#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n"
+                       ccu.header_decl.add "#endif\n"
                end
 
                if from == to.as_nullable then
                        # format A_as_nullable
-                       ccu.header_decl.add("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
+                       ccu.header_decl.add "#ifndef {to.mangled_cname}_as_not_nullable\n"
+                       ccu.header_decl.add "#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n"
+                       ccu.header_decl.add "#endif\n"
                end
        end
 end
index c0a8a15..910877b 100644 (file)
@@ -19,7 +19,7 @@ import nitni_base
 
 redef class MMethod
        # Build a C function name for the FFI implementation (uses friendly naming).
-       # * On a specific static receiver mype `recv_mtype` 
+       # * On a specific static receiver type `recv_mtype`
        # * In referene to the module `from_module` (used for type resolving and as a possible prefix)
        # * Has a possible `suffix` to the method name (may be "__super", "__impl", null, etc.)
        # * With a specified length indicating whether it uses the sort name or the long name with
@@ -39,13 +39,13 @@ redef class MMethod
 
                if suffix != null then cname = "{cname}{suffix}"
 
-               if length.long then cname = "{from_mmodule.name}___{cname}"
+               if length.long then cname = "{from_mmodule.c_name}___{cname}"
 
                return cname
        end
 
        # Build a C function signature for the FFI implementation (uses friendly naming).
-       # * On a specific static receiver mype `recv_mtype` 
+       # * On a specific static receiver type `recv_mtype`
        # * In referene to the module `from_module` (used for type resolving and as a possible prefix)
        # * Has a possible `suffix` to the method name (may be "__super", "__impl", null, etc.)
        # * With a specified length indicating whether it uses the sort name or the long name with
@@ -83,7 +83,7 @@ redef class MMethod
        end
 
        # Build a C function call for the FFI implementation (uses friendly naming).
-       # * On a specific static receiver mype `recv_mtype` 
+       # * On a specific static receiver type `recv_mtype`
        # * In referene to the module `from_module` (used for type resolving and as a possible prefix)
        # * Has a possible `suffix` to the method name (may be "__super", "__impl", null, etc.)
        # * With a specified length indicating whether it uses the sort name or the long name with
index 8fd2394..e28137f 100644 (file)
@@ -9,37 +9,37 @@ Allocations, by type:
        -RopeBuffer = 0
 
 Calls to length, by type:
-       FlatString = 23 (cache misses 5, 21.73%)
+       FlatString = 18 (cache misses 5, 27.77%)
 Indexed accesses, by type:
-       FlatString = 13
+       FlatString = 8
 Calls to bytelen for each type:
        FlatString = 61
 Calls to position for each type:
-       FlatString = 27
+       FlatString = 17
 Calls to bytepos for each type:
-       FlatString = 14
-Calls to first_byte on FlatString 216
+       FlatString = 9
+Calls to first_byte on FlatString 191
 Calls to last_byte on FlatString 19
 FlatStrings allocated with length 78 (86.813%)
 Length of travel for index distribution:
-* null = 16 => occurences 80.0%, cumulative 80.0% 
-* 1 = 14 => occurences 35.0%, cumulative 75.0% 
+* null = 11 => occurences 73.333%, cumulative 73.333% 
+* 1 = 8 => occurences 27.586%, cumulative 65.517% 
 Byte length of the FlatStrings created:
 * null = 6 => occurences 4.444%, cumulative 4.444% 
-* 1 = 21 => occurences 14.094%, cumulative 18.121% 
-* 2 = 33 => occurences 20.245%, cumulative 36.81% 
+* 1 = 24 => occurences 16.107%, cumulative 20.134% 
+* 2 = 30 => occurences 18.405%, cumulative 36.81% 
 * 3 = 29 => occurences 16.292%, cumulative 50.0% 
-* 4 = 9 => occurences 4.663%, cumulative 50.777% 
-* 5 = 20 => occurences 9.615%, cumulative 56.731% 
-* 6 = 21 => occurences 9.417%, cumulative 62.332% 
+* 4 = 5 => occurences 2.591%, cumulative 48.705% 
+* 5 = 20 => occurences 9.615%, cumulative 54.808% 
+* 6 = 25 => occurences 11.211%, cumulative 62.332% 
 * 9 = 1 => occurences 0.42%, cumulative 58.824% 
 * 10 = 9 => occurences 3.557%, cumulative 58.893% 
 * 11 = 2 => occurences 0.746%, cumulative 56.343% 
 * 12 = 1 => occurences 0.355%, cumulative 53.901% 
 * 13 = 1 => occurences 0.339%, cumulative 51.864% 
 * 14 = 1 => occurences 0.325%, cumulative 50.0% 
-* 15 = 5 => occurences 1.558%, cumulative 49.533% 
-* 16 = 7 => occurences 2.083%, cumulative 49.405% 
+* 15 = 7 => occurences 2.181%, cumulative 50.156% 
+* 16 = 5 => occurences 1.488%, cumulative 49.405% 
 * 17 = 1 => occurences 0.285%, cumulative 47.578% 
 * 25 = 2 => occurences 0.549%, cumulative 46.429% 
 * 26 = 1 => occurences 0.265%, cumulative 44.974% 
@@ -53,5 +53,5 @@ Byte length of the FlatStrings created:
 * 40 = 1 => occurences 0.207%, cumulative 37.19% 
 * 43 = 1 => occurences 0.201%, cumulative 36.419% 
 * 46 = 1 => occurences 0.196%, cumulative 35.686% 
-* 48 = 1 => occurences 0.191%, cumulative 34.99% 
-* 51 = 21 => occurences 3.918%, cumulative 38.06% 
+* 51 = 20 => occurences 3.824%, cumulative 38.623% 
+* 55 = 1 => occurences 0.186%, cumulative 37.732% 
index e320033..702e2b2 100644 (file)
@@ -9,48 +9,49 @@ Allocations, by type:
        -RopeBuffer = 0
 
 Calls to length, by type:
-       FlatString = 23 (cache misses 5, 21.73%)
+       FlatString = 18 (cache misses 5, 27.77%)
 Indexed accesses, by type:
-       FlatString = 13
+       FlatString = 8
 Calls to bytelen for each type:
        FlatString = 61
 Calls to position for each type:
-       FlatString = 27
+       FlatString = 17
 Calls to bytepos for each type:
-       FlatString = 14
-Calls to first_byte on FlatString 216
+       FlatString = 9
+Calls to first_byte on FlatString 191
 Calls to last_byte on FlatString 19
 FlatStrings allocated with length 78 (86.813%)
 Length of travel for index distribution:
-* 0 = 16 => occurences 80.0%, cumulative 80.0% 
-* 1 = 14 => occurences 35.0%, cumulative 75.0% 
+* 0 = 11 => occurences 73.333%, cumulative 73.333% 
+* 1 = 8 => occurences 27.586%, cumulative 65.517% 
 Byte length of the FlatStrings created:
 * 0 = 6 => occurences 4.478%, cumulative 4.478% 
-* 1 = 21 => occurences 14.189%, cumulative 18.243% 
-* 2 = 33 => occurences 20.37%, cumulative 37.037% 
+* 1 = 24 => occurences 16.216%, cumulative 20.27% 
+* 2 = 30 => occurences 18.519%, cumulative 37.037% 
 * 3 = 29 => occurences 16.384%, cumulative 50.282% 
-* 4 = 7 => occurences 3.646%, cumulative 50.0% 
-* 5 = 20 => occurences 9.662%, cumulative 56.039% 
-* 6 = 21 => occurences 9.459%, cumulative 61.712% 
-* 9 = 1 => occurences 0.422%, cumulative 58.228% 
-* 10 = 9 => occurences 3.571%, cumulative 58.333% 
-* 11 = 2 => occurences 0.749%, cumulative 55.805% 
-* 12 = 1 => occurences 0.356%, cumulative 53.381% 
-* 13 = 1 => occurences 0.34%, cumulative 51.361% 
-* 14 = 1 => occurences 0.326%, cumulative 49.511% 
-* 15 = 5 => occurences 1.563%, cumulative 49.063% 
-* 16 = 7 => occurences 2.09%, cumulative 48.955% 
-* 17 = 1 => occurences 0.286%, cumulative 47.143% 
-* 25 = 2 => occurences 0.551%, cumulative 46.006% 
-* 26 = 1 => occurences 0.265%, cumulative 44.562% 
-* 31 = 2 => occurences 0.513%, cumulative 43.59% 
-* 32 = 1 => occurences 0.248%, cumulative 42.327% 
-* 33 = 1 => occurences 0.24%, cumulative 41.247% 
-* 34 = 2 => occurences 0.465%, cumulative 40.465% 
-* 35 = 1 => occurences 0.225%, cumulative 39.414% 
-* 37 = 1 => occurences 0.219%, cumulative 38.512% 
-* 39 = 1 => occurences 0.213%, cumulative 37.66% 
-* 40 = 1 => occurences 0.207%, cumulative 36.853% 
-* 43 = 1 => occurences 0.202%, cumulative 36.089% 
-* 46 = 1 => occurences 0.196%, cumulative 35.363% 
-* 48 = 3 => occurences 0.575%, cumulative 35.057% 
+* 4 = 3 => occurences 1.563%, cumulative 47.917% 
+* 5 = 20 => occurences 9.662%, cumulative 54.106% 
+* 6 = 26 => occurences 11.712%, cumulative 62.162% 
+* 9 = 1 => occurences 0.422%, cumulative 58.65% 
+* 10 = 9 => occurences 3.571%, cumulative 58.73% 
+* 11 = 2 => occurences 0.749%, cumulative 56.18% 
+* 12 = 1 => occurences 0.356%, cumulative 53.737% 
+* 13 = 1 => occurences 0.34%, cumulative 51.701% 
+* 14 = 1 => occurences 0.326%, cumulative 49.837% 
+* 15 = 7 => occurences 2.188%, cumulative 50.0% 
+* 16 = 5 => occurences 1.493%, cumulative 49.254% 
+* 17 = 1 => occurences 0.286%, cumulative 47.429% 
+* 25 = 2 => occurences 0.551%, cumulative 46.281% 
+* 26 = 1 => occurences 0.265%, cumulative 44.828% 
+* 31 = 2 => occurences 0.513%, cumulative 43.846% 
+* 32 = 1 => occurences 0.248%, cumulative 42.574% 
+* 33 = 1 => occurences 0.24%, cumulative 41.487% 
+* 34 = 2 => occurences 0.465%, cumulative 40.698% 
+* 35 = 1 => occurences 0.225%, cumulative 39.64% 
+* 37 = 1 => occurences 0.219%, cumulative 38.731% 
+* 39 = 1 => occurences 0.213%, cumulative 37.872% 
+* 40 = 1 => occurences 0.207%, cumulative 37.06% 
+* 43 = 1 => occurences 0.202%, cumulative 36.29% 
+* 46 = 1 => occurences 0.196%, cumulative 35.56% 
+* 51 = 14 => occurences 2.682%, cumulative 37.356% 
+* 52 = 5 => occurences 0.931%, cumulative 37.244%