Home » Archives for Oren Eini

Author Archives: Oren Eini

The importance of a data format: Part II – The environment matters

When designing a new data format, it is important to remember in what environment we’ll operate in, what are the requirements and what type of scenarios we’ll face. With RavenDB, we are talking about the internal storage format, so it isn’t something that is available externally. That means that we don’t have to worry about interchange with anything, that frees ...

Read More »

Find the bug – The case of the degrading system – Answer

In my previous post I showed the following code, and asked what the bug was, and what the implications of that would be. class Program { private Timer nextcheck; public event EventHandler ServerSigFailed; static void Main(string[] args) { var program = new Program(); if(program.ValidateServerSig() == false) return; program.DoOtherStuff(); } public bool ValidateServerSig() { nextcheck = new Timer(state => ValidateServerSig()); var ...

Read More »

Find the bug – The case of the degrading system

The following code contains a bug, can you spot it? class Program { private Timer nextcheck; public event EventHandler ServerSigFailed; static void Main(string[] args) { var program = new Program(); if(program.ValidateServerSig() == false) return; program.DoOtherStuff(); } public bool ValidateServerSig() { nextcheck = new Timer(state => ValidateServerSig()); var response = DoRequest("http://remote-srv/signature"); if(response.Failed) { var copy = ServerSigFailed; if(copy!=null) copy(this, EventArgs.Empty); return ...

Read More »

Code review challenge – The concurrent dictionary refactoring – answer

Here is the full method that we refactored: public void ReturnMemory(byte* pointer) { var memoryDataForPointer = GetMemoryDataForPointer(pointer); _freeSegments.AddOrUpdate(memoryDataForPointer.SizeInBytes, x => { var newQueue = new ConcurrentStack<AllocatedMemoryData>(); newQueue.Push(memoryDataForPointer); return newQueue; }, (x, queue) => { queue.Push(memoryDataForPointer); return queue; }); } And here is the allocation map for this method: public unsafe void ReturnMemory(byte* pointer) { <>c__DisplayClass9_0 CS$<>8__locals0 = new <>c__DisplayClass9_0(); CS$<>8__locals0.memoryDataForPointer ...

Read More »

Code review challenge – The concurrent dictionary refactoring

In a recent code review, I had modified the following code:   _freeSegments.AddOrUpdate(memoryDataForPointer.SizeInBytes, x => { var newQueue = new ConcurrentQueue<AllocatedMemoryData>>(); newQueue.Enqueue(memoryDataForPointer); return newQueue; }, (x, queue) => { queue.Enqueue(memoryDataForPointer); return queue; }); Into this code: var q = _freeSegments.GetOrAdd(memoryDataForPointer.SizeInBytes, size => new ConcurrentQueue<AllocatedMemoryData>()); q.Enqueue(memoryDataForPointer); Can you tell me why? Reference: Code review challenge – The concurrent dictionary refactoring from ...

Read More »

Configuration is user input too

Every programmer knows that input validation is important for good application behavior. If you aren’t validating the input, you will get… interesting behavior, to say the least. The problem is that what developers generally don’t consider is that the system configuration is also users’ input, and it should be treated with suspicion. It can be something as obvious as a ...

Read More »

Micro benchmarks and hot paths

I’m doing some work to refactor a complex piece of code, and I have a piece of memory that is allocated, then accessed via a struct pointer. This piece of code gets called a lot, so I wondered about the tradeoff of holding a pointer that is already casted to the right struct pointer vs the cost of another pointer ...

Read More »