#we have Index usize, but why not Index u8, u16, or u32?

119 messages · Page 1 of 1 (latest)

timber wharf
#

because having those impls would break pretty much every rust program

#

try uncommenting this code: ```rust
use core::ops::Index;
struct Foo;

impl Index<usize> for Foo{
type Output = ();
fn index(&self, _index: usize) -> &(){
&()
}
}

/*
impl Index<u8> for Foo{
type Output = ();
fn index(&self, _index: u8) -> &(){
&()
}
}
*/

fn main(){
let foo = Foo;
let _ = foo[42];
}```

formal cargo
#

ah... interesting

#

im running into this situation where i want to optimize the amount of memory i need to use

restive estuary
#

you should prob use c, c++ if you care about maximum efficency, but its a giant headache

formal cargo
#

I have many of a certain type of object which has many u8s which eventually will be used as indices

restive estuary
#

but i dont think using a smaller iterator is gonne hurt your memory usage, its mostly other thing

#

ah

formal cargo
#

so there are lots of these u8s

#

and in the program, I index fairly frequently

restive estuary
#

have you tryed type casting?

timber wharf
#

sounds like you'll have to do a bunch of thing[x as usize]

formal cargo
#

yeah but it is happening all over the place

#

exactly

timber wharf
#

it doesn't matter

formal cargo
#

so there is thing[x as usize] all over the place

restive estuary
#

so whats the problem?

formal cargo
#

im wondering if it A) inefficient or B) redundant

#

if I have u8, going to usize all the time seems inefficient and redundant

restive estuary
#

from what i measured in c++ using rdtsc, i couldend measure a diffrence in speed when i type casted, but im just an amateur so idk if it was accurte measurment

formal cargo
#

i suppose it would probably depend on the instructions being issued and perhaps the processor

#

i havnt figured out how to check that kind of stuff yet with rust

#

i know it is possible to disassemble but i dont have full fluency in assembly

#

however i do want to make sure i am squeezing the amount of memory used

#

if only because i want to make sure i know how to do it

restive estuary
#

what you need to do is use inline assembly to assign the rdtsc wich is pretty much a clock, and run your code, run rdtsc again and test the diffrence in timing

timber wharf
restive estuary
#

no clue how to do that tho, i used the function that comes with the compiled to assign it to a variable

restive estuary
formal cargo
restive estuary
#

where talking about nanosecond diffrence

#

the rest of your program and how efficent that is, is gonne mather allot more

#

like cache locality, wherby your program has to jump arround and take allot of time to fetch data from memory, instead of prefetching

#

best example is an array, your cpu is pretty sure that your gonne acces the next elements from the array so it already fetches it from ram

left spear
#

unless youre running on like a 6502, using 8 bytes for a usize isntead of one byte for a u8 is a welcome trade off to be able to have a standarized value that all containers implement

#

It's a non issue

#

if it is an issue, then you should be writing in assembly or C as the highest level

restive estuary
#

but if you have an array and you jump from index to index your cpu will not be able to predict that and stall, becuse its waiting for a memory fetch

formal cargo
#

im ultimately planning to target some tiny microprocessors

left spear
#

which ones

#

stm32s? atmega328s?

restive estuary
#

gameboy?

#

lol

formal cargo
#

8 bit cnn computation at edge

left spear
#

what's it called

formal cargo
#

custom chips

restive estuary
#

no clue about microproccesors

left spear
#

do you have an assembler for it?

formal cargo
#

yes

#

basically i have been evaluating rust for this sort of purpose

left spear
#

what ISA is it based off and what is your toolchain as of now

formal cargo
#

bit of a hybrid

left spear
#

can I get some actual concrete information and not just "custom chip"

#

because trying to target rust -> some weird ass chip probably isng going to work very well in the long term

#

its dodgy even on stm32h7 which is what I use at work.

#

which is a very well supported chip

formal cargo
#

it is custom hardware actually

#

there are arm cores at a high level but there is also a separate hardware layer underneath that

left spear
#

have you made documentaytion or is this a psssion project

formal cargo
#

neither

left spear
#

have you even gotten a rust hello world to run yet?

formal cargo
#

no, just c

#

i do not appreciate the condescension

left spear
#

imo you should stick with C. getting rust to run on that thing is almost certanlly going to be a nightmate

formal cargo
#

both unwelcome and unnecessary

#

you can stick with c for yours if you like

#

i like rust

left spear
#

well so do I, but I also like being practical and know when to cut my losses before I waste two work weeks getting a toolchain to work :)

#

is your C compiler llvm based by chance

#

it is possible for rust to emit llvm iirc

restive estuary
#

whats that?

left spear
# restive estuary whats that?

llvm is a mid level representation language that is like assembly but can be easily further compiled down to a lower level assembly. Imagine something like java byte code except entierly different lol

timber wharf
# formal cargo i do not appreciate the condescension

tbh i agree that this part of the conversation is not particularly helpful. i'll say again that the u8 vs usize thing most likely doesn't matter. if you find that it does, create a newtype and you can do whatever indexing you want

formal cargo
#

ultimately what i want to do is emit instructions for the chip directly

restive estuary
#

ah

formal cargo
#

like

#

flip these bits

left spear
formal cargo
#

flip those bits

#

actually

#

for an 8 bit architecture

#

all i need is a Vec<u8?

left spear
#

your best chance is compile rust -> MIR -> LLVM ->(use your c compiler if its LLVM based) your ARM like asm

formal cargo
#

Vec<u8>

#

no actually it is a Vec<u8>

#

0x00

left spear
#

If memory is such an issue why are you using a heap allocated vector.

formal cargo
#

0x01

#

0x02 etc

left spear
#

you should be using [u8; LENGTH]

formal cargo
#

sure yea

left spear
#

[u8; length] == char[LENGTH]

#

Vec == libstdc++std::vector<T>

formal cargo
#

gotta go -- doctors appt @ 11:30

#

thanks for the help i did learn some things

left spear
#

I highly suggest that you grab an arduino (atmega328p) and learn about that before you jump into custom architecture stuff

#

evidently neither do you lmfao

#

have a good day

#

good luck!

formal cargo
#

just saying

#

anyway, i gotta go

#

no point being late for the doc

#

✌️

left spear
#

✌️

formal cargo
#

If you dont have something nice to say, there is nothing to say

timber wharf
#

there's a time and place to recommend particular constructs, this is not it

left spear
formal cargo
#

I think what I am going to do is write a quick wrapper that can Index<u8> when I need it

timber wharf
#

most likely you will find it'll bottom out at some intrinsic add/offsetting a pointer by an usize/isize. so it won't matter

cinder dove
#

On x86_64, indexing with a u8 cast to a usize generates 1 additional movzx instruction, which should execute in a few hundred picoseconds on modern hardware. The cost should be similarly negligible for any ARM-based architecture. You shouldn’t have any problem using a u8 for storage and casting for indexing.

fading obsidian
#

So making it explicit can be a bit faster, reducing the need for conversions

cinder dove
formal cargo
#

thanks both 😃

#

makes total sense