What does 'dirty-flag' / 'dirty-values' mean?
Asked Answered
H

4

28

I see some variables named 'dirty' in some source code at work and some other code. What does it mean? What is a dirty flag?

Hunnish answered 21/11, 2012 at 22:41 Comment(4)
This is impossible to answer without more context. In a DBMS buffer pool, a page might be marked 'dirty' because it has been changed since it was read and must be rewritten before the page can be reused. There are likely a myriad other possible reasons for the term to be used.Seidule
What I've typically seen it used for is to indicate that something needs updating.Linguiform
This question isn't really specific to C++ or coding-style though. It is more related to data structures and algorithms.Linguiform
I think this is a dupe of one of these, or maybe a kind of a merge: #1746388 and #554382Iz
R
42

Generally, dirty flags are used to indicate that some data has changed and needs to eventually be written to some external destination. It isn't written immediate because adjacent data may also get changed and writing bulk of data is generally more efficient than writing individual values.

Rumpus answered 21/11, 2012 at 22:44 Comment(1)
Often change is not tracked but rather writes (i.e., a "silent store" still marks data as dirty).Littman
V
18

There's a deeper issue here - rather than "What does 'dirty mean?" in the context of code, I think we should really be asking - is 'dirty' an appropriate term for what is generally intended.

'Dirty' is potentially confusing and misleading. It will suggest to many new programmers corrupt or erroneous form data. The work 'dirty' implies that something is wrong and that the data needs to be purged or removed. Something dirty is, after all undesirable, unclean and unpleasant.

If we mean 'the form has been touched' or 'the form has been amended but the changes haven't yet been written to the server', then why not 'touched' or 'writePending' rather than 'dirty'?

That I think, is a question the programming community needs to address.

Vaginitis answered 10/4, 2013 at 0:20 Comment(4)
I like writePending much more than the ubiquitous 'dirty'. Thanks.Alvar
I think "dirty" is a good word. It's short (much shorter than writePending), and it doesn't get used for other concepts. A write could be pending for other reasons, or the reason it's interesting could be different. "dirty" in a computer context has a well-established meaning of modified data in any kind of cache / coherency / write-back system. It's easy to look up on wikipedia: The disambiguation page for "dirty" has that link. Concise and precise technical language is useful and important.Tryst
Amen @Adrian NH. I just wasted 5 minutes of my life trying to figure out what "dirty cache" meant with regard to WiredTiger's cache. The term 'Dirty' in the context of a database cache is pure ego and terrible nomenclature — classic programmer fancy-dancing BS.Elegist
Dirty is ambiguous and requires the term to be learned. Established or not. How about using the word modified? It's descriptive and unambiguous. The wikipedia page referenced above references another page on the Dirty bit which it also refers to as the modified bit.Corinecorinna
D
8

Dirty could mean a number of things, you need to provide more context. But in a very general sense a "dirty flag" is used to indicate whether something has been touched / modified.

For instance, see usage of "dirty bit" in the context of memory management in the wiki for Page Table

Distraction answered 21/11, 2012 at 22:44 Comment(0)
S
5

"Dirty" is often used in the context of caching, from application-level caching to architectural caching.

In general, there're two kinds of caching mechanisms: (1) write through; and (2) write back. We use WT and WB for short.

WT means that the write is done synchronously both to the cache and to the backing store. (By saying the cache and the backing store, for example, they can stand for the main memory and the disk, respectively, in the context of databases).

In contrast, for WB, initially, writing is done only to the cache. The write to the backing store is postponed until the cache blocks containing the data are about to be modified/replaced by new content.

The data is the dirty values. When implementing a WB cache, you can set dirty bits to indicate whether a cache block contains dirty value or not.

Sainted answered 23/11, 2012 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.