#Please criticise my code

1 messages · Page 1 of 1 (latest)

radiant rose
azure cove
#

good code overall. I only have 2 comments:

if t.is_file() {
    ...
}
if t.is_dir() {
    ...
}

compiler might optimize these anyway, but I think it'd be better to use else if (reduces 1 line!)

if t.is_file() {
    let val = IO::File(entry.path());
    children.push(Tree {
        value: val,
        children: Vec::new(),
    });
}

given most items will probably end up being files instead of dirs, I think it might be a good idea to use Option<Vec<Tree>> for children

supple ruin
#

Though this representation does imply that you could have something that is a file and has children

azure cove
#

I thought about that (although I wasn't sure), but overall I feel like being more explicit would be better in this case

supple ruin
#

I'd probably represent the structure as follows:

pub enum Tree<T> {
  Branch(Vec<Self>),
  Leaf(T),
}
azure cove
#

although at this point you'll end up storing more data than by just using a struct, since an extra bit will be needed to distinguish between enums (I think)

supple ruin
azure cove
#

Since if you use branch/node you'll no longer need the enum for Dir/File, meaning they'll end up the same size

radiant rose
radiant rose
supple ruin
#

The tag sometimes can be optimized into one the variants' data, but that's not always possible

radiant rose
supple ruin
radiant rose
#

What if there's the need for 2 or more bits?

supple ruin
#

Also, some theoretically possible optimizations are not performed because optimizing for size can sometimes slow down matching the enum

radiant rose
supple ruin
#

Also remember Vec may be on a 32-bit system

radiant rose
supple ruin
#

ASLR (Addres Space Layout Randomization) is a security thing

radiant rose
#

How does it secure things?

supple ruin
#

Also, hardening. If you, say, never reuse malloced memory, you can trivially detect use-after free or double-free

supple ruin
radiant rose
supple ruin
#

Which in turn means an attacker that somehow got a way to write to memory (maybe a buffer overflow) can't easily know where to write

supple ruin
radiant rose
#

Idk all i'm saying is that it wouldn't be that bad to reserve a few bits of the address space for this

supple ruin
#

In the future, they may well extend to the entire pointer.

#

And Vec doesn't come with funny stipulations about how it's not allowed to exist at certain addresses because the pointer reserves them as scratch space

#

In fact, it can't have such a stipulation. It would break other stable promises.

radiant rose
supple ruin
radiant rose
#

What's scratch space?

supple ruin
#

Space that has no particular use, that you can use for your own purposes

#

In this case, I assume your idea is that you'd pack the discriminant in the 7 spare bits of a pointer.

#

But they are not actually spare

#

You're not supposed to use them.

radiant rose
#

why

supple ruin
#

CPU manufacturers say so

radiant rose
#

a

supple ruin
#

More practically, because future CPUs may claim to be backwards compatible, but also use those bits for the actual pointer

#

Which would ruin your clever encoding scheme

radiant rose
#

How much scratch space do we have in pointers then? only 1 if it's a non nullable pointer?

supple ruin
radiant rose
#

huh?

supple ruin
#

*const T and *mut T have zero scratch space. NonNull (and thus all the collection types), and &/&mut have one bitpattern

supple ruin
#

They reserve specifically the null bitpattern.

radiant rose
#

ah I see

supple ruin
#

This means that Option<NonNull<T>> can be optimized to a nullable pointer

#

But you can't do that for an Option-like enum with two different Nones

radiant rose
#

How do you know that much about the internals? What have you read?

supple ruin