RustPython / Parser

MIT License
73 stars 28 forks source link

Consider using `Box` on type parameters to reduce statement allocation size #94

Open zanieb opened 1 year ago

zanieb commented 1 year ago

In https://github.com/RustPython/Parser/pull/93, we add type_params: Vec to ClassDef and FunctionDef which increases the size of a statement. We may want to optimize this field to reduce the size of the statement struct.

The main reason for the assertion is to be aware of how changing the AST changes its size. This is important because ruff and RustPython hold many instances of these types and increasing their size mean:

  • Each element in a Vec now requires more storage -> Overall increased memory consumption
  • Reading or writing an element becomes slower because your computer must read or write more bytes. This is especially relevant for reading where the L1 cache short circuits reads by caching about 64 bytes of neighboring memory to avoid subsequent reads from (the very slow) memory.

There's not much we can do about this here. This could be a place where using tinyvec over a regular Vec would be beneficial, considering that the vec will be empty for almost all instances. But I think this is fine for now.

_Originally posted by @MichaReiser in https://github.com/RustPython/Parser/pull/93#discussion_r1259220991_

zanieb commented 1 year ago

Best implemented once Box matching is available in stable rust (nightly at the time of this comment).