Is there a way to programatically create a variable in Spin?
sccoupe
Posts: 118
Lets say that I have data incoming on the serial port that has a header ID followed by a set number of data bytes. I want to store that data in a variable named as the header. Even given thought to hooking up an external serial ram or something to just use the header a memory address. Any thoughts or ideas on this?
Thanks
Thanks
Comments
How fast is the data coming? Maybe it would be a solution to store it on a SD card, where the filename equals the header ID?
What is the lifetime of the data? How is it used later on?
-Phil
-Phil
-Phil
That should be easy to manage in N different files. Obviously one file of 64K headers can't be read and manipulated all at once by Propeller memory.
As a minimum you could create an object that maintains two simple arrays. One is a list of IDs received, the other is a list of pointers to the byte data.
It would have methods like:
put (id, dataPtr)
get (id)
If the distribution of possible keys is non-uniform, then the hash function may require a little more work if you want to minimize the number of collisions, thus speeding up writes and reads.
-Phil
The 16 bit ID would be used as the address of a pointer that points to the actual location of the data.
Very fast, but not very memory efficient.
An even faster alternative would be to use the ID as the address of the data by shifting it left 4 bits.
Define variables: VariableID[2000]
LastUsedLocation := 0xMemory Location
When data received: If VariableID[Header] = zero
VariableID[Header] = LastUsedLocation + 8 (8 data bytes)
Store the 8 bytes of data there
LastUsedLocation = LastUsedLocation + 8
else
Store the 8 bytes of data at memory location 0xVariableID[Header]
I like this option too. If the memory is of a ram type, cheap and accessed as easily as a serial eeprom, the coding should be real easy. I guess in this case it would be HeaderID is memory location for byte one, byte two is at memory location HeaderID*2, etc.
BTW, a binary search in a 2048-entry table can require as many as 11 compares. A properly-crafted hash function could entail far fewer than that.
-Phil
As Prop programmers our minds should reel at such waste.
It can be done with a simple arrays of ID's and data. Even the brute force linear searching and subsequent insertion into those arrays would do fine.
If performance is an issue we can look at hash tables or whatever.
So, your saying when the first data comes in, write the 10 bytes (header and data) to the first memory location. When the next byte comes in, compare the header ID with each header ID in memory until it comes to an ID that is higher and them move everything from that location up 10 bytes and drop the new data in the hole? We end up with a lump of data that is ordered numerically from low ID to high ID. This sounds the most efficient and cheapest. Moving the data around may be a bit sluggish, but after all ID's are sent once, then there is no more moving of data, just replacing.
Good point provided all the data will fit in hub ram, however if external memory has to be added it might as well be done to make the software and coding as fast and simple as possible.
I wrote a hash table in spin once. I just can't find it
I found this in C though: http://forums.parallax.com/attachment.php?attachmentid=93461&d=1339511663
Change to CMM mode and replace printf, etc... with simpletext library functions for a much smaller implementation.
-Phil
The longmove is totally done in PASM and is really fast .. it'll easily move MBs per second.
And yes, after data is settled there will be no more moves.
Well ... it's not the most efficient in terms of speed, as hashing would be faster if the memory allocated is big enough to avoid chained rehashes.
Nice work Phil.
How would you characterize the insert/search performance of that? Certainly not O(1).
Although given the circumstances, the performance is not as important as the code cost.
Then if performance is an issue think about hashes or binary searches etc.
However avoid linked lists, they are always slower.
Avoid premature optimization bla bla...
'Just got back from a bike ride. Once I shower, I'll update my example to include the locking.
I would submit that my hashing example is simpler still.
-Phil
-Phil