Consensys / linea-arithmetization

17 stars 18 forks source link

Limit the size of lines inside TraceSection #801

Open ahamlat opened 1 week ago

ahamlat commented 1 week ago

Currently, in TraceSection.java, the number of lines is initialized to 32, this generates a lot of overhead on big transactions that have hundreds of thousands of TraceSection objects. I suggest to limit the size of the arrayList lines to the expected maximum size.

image

Currently List<TraceLine> lines = new ArrayList<>(32);

Expected List<TraceLine> lines = new ArrayList<>(target_size);

I did some tests with Java Object Layout to show the difference between initializing the arrayList without any size, with the expected size (in this case 5) and with 32. The results shows a difference of 112 bytes difference in the shallow size of each lines object between the the current implementation (size = 32) and the implementation where we would set the size to 4.

The testing code

public class MemoryFootPrintArrayList {

    public static void main(String[] args) {
        System.out.println("**** without size *****");
        List<Integer> arrayList = new ArrayList<>();
        Collections.addAll(arrayList, 1, 2, 3, 4);
        printLayout(arrayList);

        System.out.println("**** without exact size *****");
        List<Integer> arrayList2 = new ArrayList<>(4);
        Collections.addAll(arrayList2, 1, 2, 3, 4);
        printLayout(arrayList2);

        System.out.println("**** without size *****");
        List<Integer> arrayList3 = new ArrayList<>(32);
        Collections.addAll(arrayList3, 1, 2, 3, 4);
        printLayout(arrayList3);
    }

    public static void printLayout(Object obj) {
        System.out.println("Class: " + obj.getClass().getSimpleName());
        System.out.println("Retained size: " + GraphLayout.parseInstance(obj).toPrintable());
    }

}

The result

**** without size *****
Class: ArrayList
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
Retained size: java.util.ArrayList@73f792cfd object externals:
          ADDRESS       SIZE TYPE                PATH                           VALUE
        70fd204a0         24 java.util.ArrayList                                (object)
        70fd204b8        200 (something else)    (somewhere else)               (something else)
        70fd20580         56 [Ljava.lang.Object; .elementData                   [1, 2, 3, 4, null, null, null, null, null, null]
        70fd205b8    1028632 (something else)    (somewhere else)               (something else)
        70fe1b7d0         16 java.lang.Integer   .elementData[0]                1
        70fe1b7e0         16 java.lang.Integer   .elementData[1]                2
        70fe1b7f0         16 java.lang.Integer   .elementData[2]                3
        70fe1b800         16 java.lang.Integer   .elementData[3]                4

Addresses are stable after 1 tries.

**** without exact size *****
Class: ArrayList
Retained size: java.util.ArrayList@7b2bbc3d object externals:
          ADDRESS       SIZE TYPE                PATH                           VALUE
        70f257ec0         24 java.util.ArrayList                                (object)
        70f257ed8         32 [Ljava.lang.Object; .elementData                   [1, 2, 3, 4]
        70f257ef8   12335320 (something else)    (somewhere else)               (something else)
        70fe1b7d0         16 java.lang.Integer   .elementData[0]                1
        70fe1b7e0         16 java.lang.Integer   .elementData[1]                2
        70fe1b7f0         16 java.lang.Integer   .elementData[2]                3
        70fe1b800         16 java.lang.Integer   .elementData[3]                4

Addresses are stable after 1 tries.

**** without size *****
Class: ArrayList
Retained size: java.util.ArrayList@1aafa419d object externals:
          ADDRESS       SIZE TYPE                PATH                           VALUE
        70f2607a0         24 java.util.ArrayList                                (object)
        70f2607b8        144 [Ljava.lang.Object; .elementData                   [1, 2, 3, 4, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
        70f260848   12300168 (something else)    (somewhere else)               (something else)
        70fe1b7d0         16 java.lang.Integer   .elementData[0]                1
        70fe1b7e0         16 java.lang.Integer   .elementData[1]                2
        70fe1b7f0         16 java.lang.Integer   .elementData[2]                3
        70fe1b800         16 java.lang.Integer   .elementData[3]                4

Addresses are stable after 1 tries.
letypequividelespoubelles commented 1 week ago

In https://github.com/Consensys/linea-arithmetization/pull/748/commits/5413f169ecadf223237d8f398b2e20c148022109 I modified the default nb of line to 22, which is the max in the arith. We have to give better limits for each instructions: