Persistent Writes

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Persistent Writes

Post by Don_Kirby »

I thought this was covered in another topic here, but I couldn't find it.

If the contents of a persistent variable is the same as what I am writing to that variable, does the VM actually write the variable, or is the write instruction ignored?

-Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Persistent Writes

Post by dkinzer »

Don_Kirby wrote:If the contents of a persistent variable is the same as what I am writing to that variable, does the VM actually write the variable?
Originally, the VM always wrote the data irrespective of the current value. Since v2.0.0, however, the VM reads the destination address first and skips the write process if the content matches the value to be written. Although this is not described in the primary documentation, the modification is listed among the recent changes for v2.0.0.
- Don Kinzer
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

Great! That means I can get rid of a slew of If-Then statements.

...3 minutes later...

I just shaved 188 bytes of code space :)


-Don
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

Does the extra read operation bring any performance penalties?

-Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Don_Kirby wrote:Does the extra read operation bring any performance penalties?
Yes, but it is minor. The read and compare operation takes about 12 CPU cycles (less than a microsecond). On the other hand, the "typical" time documented for persistent writes is 3.3mS so this is a big win if the location doesn't really need to be written.

The actual write operation proceeds in parallel with other operations, however, so the time saved is meaningful only for multi-byte writes.
- Don Kinzer
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

Do the same rules apply for persistent variables such as ATNChar described in this thread (i.e. write only if required)?

-Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Don_Kirby wrote:Do the same rules apply for persistent variables such as ATNChar [...]
Yes. The same low level code is used for writing all Persistent Memory data items irrespective of their size, type and whether they are System or User data items.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

dkinzer wrote:The same low level code is used [...]
I should add that the decision whether to write or not is done on a byte-by-byte basis. So if you assign a value to a multi-byte Persistent variable, only the bytes that need to change are written.
- Don Kinzer
Post Reply