Merge: Attributes access in nitvm
authorJean Privat <jean@pryen.org>
Sat, 29 Nov 2014 00:59:23 +0000 (19:59 -0500)
committerJean Privat <jean@pryen.org>
Sun, 30 Nov 2014 01:35:32 +0000 (20:35 -0500)
Accessing attributes in the nitvm is now implemented by a faster mechanism than perfect hashing.

Attribute access is now implemented by two different techniques:
- perfect hashing, slow but multiple-inheritance compatible
- direct access, fast but only compatible with single inheritance

For a given access to an attribute, we try to use direct access if possible, i.e. if this attribute is always at the same position in objects.
If this attribute has multiple position in objects, we need to use perfect hashing.

Non-contractual perfs: the nitvm is now a little faster than before and these two different implementations will allow to work on optimization protocols in the futur.

Pull-Request: #943
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

1  2 
src/vm.nit

diff --cc src/vm.nit
@@@ -260,6 -267,18 +267,18 @@@ class VirtualMachine super NaiveInterpr
                return res;
        `}
  
+       # Return the attribute value in `instance` with a direct access (SST)
 -      #     `instance` is the attributes array of the receiver
 -      #     `offset` is the absolute offset of this attribute
++      # * `instance` is the attributes array of the receiver
++      # * `offset` is the absolute offset of this attribute
+       private fun read_attribute_sst(instance: Pointer, offset: Int): Instance `{
+               /* We can make a direct access to the attribute value
+                  because this attribute is always at the same position
+                  for the class of this receiver */
+               Instance res = ((Instance *)instance)[offset];
+               return res;
+       `}
        # Replace in `recv` the value of the attribute `mproperty` by `value`
        redef fun write_attribute(mproperty: MAttribute, recv: Instance, value: Instance)
        do
                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
++      # * `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
@@@ -547,13 -591,13 +591,13 @@@ redef class MClas
                return res
        end
  
-       # Update positions of self class in `parent`
+       # Update positions of the class `cl`
 -      #     `attributes_offset`: absolute offset of introduced attributes
 -      #     `methods_offset`: absolute offset of introduced methods
 +      # * `attributes_offset`: absolute offset of introduced attributes
 +      # * `methods_offset`: absolute offset of introduced methods
-       private fun update_positions(attributes_offsets: Int, methods_offset:Int, parent: MClass)
+       private fun update_positions(attributes_offsets: Int, methods_offset:Int, cl: MClass)
        do
-               parent.positions_attributes[self] = attributes_offsets
-               parent.positions_methods[self] = methods_offset
+               positions_attributes[cl] = attributes_offsets
+               positions_methods[cl] = methods_offset
        end
  end