nitvm: Attributes access is fully functionnal with direct access when possible
authorJulien Pagès <julien.projet@gmail.com>
Thu, 27 Nov 2014 13:17:44 +0000 (14:17 +0100)
committerJulien Pagès <julien.projet@gmail.com>
Fri, 28 Nov 2014 21:56:16 +0000 (22:56 +0100)
Signed-off-by: Julien Pagès <julien.projet@gmail.com>

src/vm.nit

index d745909..65e03a1 100644 (file)
@@ -284,11 +284,17 @@ class VirtualMachine super NaiveInterpreter
        do
                assert recv isa MutableInstance
 
-               var id = mproperty.intro_mclassdef.mclass.vtable.id
-
                # Replace the old value of mproperty in recv
-               write_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
+               if mproperty.intro_mclassdef.mclass.positions_attributes[recv.mtype.as(MClassType).mclass] != -1 then
+                       # if this attribute class has an unique position for this receiver, then use direct access
+                       write_attribute_sst(recv.internal_attributes, mproperty.absolute_offset, value)
+               else
+                       # Otherwise, use perfect hashing to replace the old value
+                       var id = mproperty.intro_mclassdef.mclass.vtable.id
+
+                       write_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
                                        recv.vtable.mask, id, mproperty.offset, value)
+               end
        end
 
        # Replace the value of an attribute in an instance
@@ -310,6 +316,16 @@ class VirtualMachine super NaiveInterpreter
                Instance_incr_ref(value);
        `}
 
+       # Replace the value of an attribute in an instance with direct access
+       #     `instance` is the attributes array of the receiver
+       #     `offset` is the absolute offset of this attribute
+       #     `value` is the new value for this attribute
+       private fun write_attribute_sst(instance: Pointer, offset: Int, value: Instance) `{
+               // Direct access to the position with the absolute offset
+               ((Instance *)instance)[offset] = value;
+               Instance_incr_ref(value);
+       `}
+
        # Is the attribute `mproperty` initialized in the instance `recv`?
        redef fun isset_attribute(mproperty: MAttribute, recv: Instance): Bool
        do