Your dataset should have fixed size? So the len method should be quite fast by just precomputing it before hand?
Why is your getitem is so slow? How do you usually use this dataset if you cannot access it’s elements?
Also do remember that the dataloader will use multiple processes to load the datas in your dataset, so even if getting each element is not blazing fast, it is ok. For example, the ImageFolder dataset reads each image individually from disk when they need to be accessed and it is fast enough to not slow down training.