Oren Eini

aka Ayende Rahien

Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,575
|
Comments: 51,188

Copyright ©️ Ayende Rahien 2004 — 2025

Privacy Policy · Terms
filter by tags archive
stack view grid view
  • architecture (606) rss
  • bugs (450) rss
  • challanges (123) rss
  • community (377) rss
  • databases (481) rss
  • design (893) rss
  • development (640) rss
  • hibernating-practices (71) rss
  • miscellaneous (592) rss
  • performance (397) rss
  • programming (1085) rss
  • raven (1442) rss
  • ravendb.net (526) rss
  • reviews (184) rss
  • 2025
    • May (7)
    • April (10)
    • March (10)
    • February (7)
    • January (12)
  • 2024
    • December (3)
    • November (2)
    • October (1)
    • September (3)
    • August (5)
    • July (10)
    • June (4)
    • May (6)
    • April (2)
    • March (8)
    • February (2)
    • January (14)
  • 2023
    • December (4)
    • October (4)
    • September (6)
    • August (12)
    • July (5)
    • June (15)
    • May (3)
    • April (11)
    • March (5)
    • February (5)
    • January (8)
  • 2022
    • December (5)
    • November (7)
    • October (7)
    • September (9)
    • August (10)
    • July (15)
    • June (12)
    • May (9)
    • April (14)
    • March (15)
    • February (13)
    • January (16)
  • 2021
    • December (23)
    • November (20)
    • October (16)
    • September (6)
    • August (16)
    • July (11)
    • June (16)
    • May (4)
    • April (10)
    • March (11)
    • February (15)
    • January (14)
  • 2020
    • December (10)
    • November (13)
    • October (15)
    • September (6)
    • August (9)
    • July (9)
    • June (17)
    • May (15)
    • April (14)
    • March (21)
    • February (16)
    • January (13)
  • 2019
    • December (17)
    • November (14)
    • October (16)
    • September (10)
    • August (8)
    • July (16)
    • June (11)
    • May (13)
    • April (18)
    • March (12)
    • February (19)
    • January (23)
  • 2018
    • December (15)
    • November (14)
    • October (19)
    • September (18)
    • August (23)
    • July (20)
    • June (20)
    • May (23)
    • April (15)
    • March (23)
    • February (19)
    • January (23)
  • 2017
    • December (21)
    • November (24)
    • October (22)
    • September (21)
    • August (23)
    • July (21)
    • June (24)
    • May (21)
    • April (21)
    • March (23)
    • February (20)
    • January (23)
  • 2016
    • December (17)
    • November (18)
    • October (22)
    • September (18)
    • August (23)
    • July (22)
    • June (17)
    • May (24)
    • April (16)
    • March (16)
    • February (21)
    • January (21)
  • 2015
    • December (5)
    • November (10)
    • October (9)
    • September (17)
    • August (20)
    • July (17)
    • June (4)
    • May (12)
    • April (9)
    • March (8)
    • February (25)
    • January (17)
  • 2014
    • December (22)
    • November (19)
    • October (21)
    • September (37)
    • August (24)
    • July (23)
    • June (13)
    • May (19)
    • April (24)
    • March (23)
    • February (21)
    • January (24)
  • 2013
    • December (23)
    • November (29)
    • October (27)
    • September (26)
    • August (24)
    • July (24)
    • June (23)
    • May (25)
    • April (26)
    • March (24)
    • February (24)
    • January (21)
  • 2012
    • December (19)
    • November (22)
    • October (27)
    • September (24)
    • August (30)
    • July (23)
    • June (25)
    • May (23)
    • April (25)
    • March (25)
    • February (28)
    • January (24)
  • 2011
    • December (17)
    • November (14)
    • October (24)
    • September (28)
    • August (27)
    • July (30)
    • June (19)
    • May (16)
    • April (30)
    • March (23)
    • February (11)
    • January (26)
  • 2010
    • December (29)
    • November (28)
    • October (35)
    • September (33)
    • August (44)
    • July (17)
    • June (20)
    • May (53)
    • April (29)
    • March (35)
    • February (33)
    • January (36)
  • 2009
    • December (37)
    • November (35)
    • October (53)
    • September (60)
    • August (66)
    • July (29)
    • June (24)
    • May (52)
    • April (63)
    • March (35)
    • February (53)
    • January (50)
  • 2008
    • December (58)
    • November (65)
    • October (46)
    • September (48)
    • August (96)
    • July (87)
    • June (45)
    • May (51)
    • April (52)
    • March (70)
    • February (43)
    • January (49)
  • 2007
    • December (100)
    • November (52)
    • October (109)
    • September (68)
    • August (80)
    • July (56)
    • June (150)
    • May (115)
    • April (73)
    • March (124)
    • February (102)
    • January (68)
  • 2006
    • December (95)
    • November (53)
    • October (120)
    • September (57)
    • August (88)
    • July (54)
    • June (103)
    • May (89)
    • April (84)
    • March (143)
    • February (78)
    • January (64)
  • 2005
    • December (70)
    • November (97)
    • October (91)
    • September (61)
    • August (74)
    • July (92)
    • June (100)
    • May (53)
    • April (42)
    • March (41)
    • February (84)
    • January (31)
  • 2004
    • December (49)
    • November (26)
    • October (26)
    • September (6)
    • April (10)
Comparison page for RavenDB and MongoDB
  previous post next post  
Jan 02 2017

Electric fenced memory results

time to read 1 min | 77 words

A few days ago I posted about electric fence memory and its usages. Here is one problem that if found for us.

image

Do you see the bug? And can you imagine how hard it would be for us to figure this out if we didn’t hotwire the memory?

Tweet Share Share 18 comments
Tags:
  • bugs

  previous post next post  

Comments

Dennis
02 Jan 2017
10:34 AM
Dennis

Quick guess. The allocation is in bytes, but the indexing is in char.

John
02 Jan 2017
11:18 AM
John

Encoding issue?

Uri
02 Jan 2017
11:24 AM
Uri

the exception indicates that you somehow overflow the destChars, hence the size is probably wrong. so the only issue i can think is there might be a problem with the linq Sum, if its lazy it might give wrong calculation, but I'm not sure.

wqw
02 Jan 2017
12:54 PM
wqw

GetNativeTempBuffer allocates less than Size bytes?

Stuart Turner
02 Jan 2017
16:36 PM
Stuart Turner

Assuming that query.FieldsToFetch is a series of IList<char>, then: you are assembling the total length of all characters to put them into a single buffer area to hash. The problem is that char is two bytes wide. You are counting the number of chars, but context.GetNativeTempBuffer() is expecting the number of bytes, which is twice the number of chars. This means you overrun your buffer exactly halfway through loading the string into the buffer.

This would definitely take a while to identify without your electric fence. Nice catch!

Oren Eini
03 Jan 2017
08:47 AM
Oren Eini

Stuart, Yes, that is indeed the issue. The actual problem is even worse, leaving aside the fact that the code read correctly. The native buffer works in power of 2, so most of the time, this would be fine, and then we'll have silent heap corruption...

Dennis
03 Jan 2017
11:25 AM
Dennis

This ought to be a trivial matter for a static code analyzer to find. Isnt there any good ones available for unsafe c# code?

Oren Eini
03 Jan 2017
13:04 PM
Oren Eini

Dennis, I haven't seen any for C#, no.

Alois Kraus
03 Jan 2017
22:32 PM
Alois Kraus

Nice catch. I would love to see you more going into the BlittableJson stuff. How do you deal with updates to a large document and will be there a BlittableJsonWriter?

Oren Eini
04 Jan 2017
10:03 AM
Oren Eini

Alois, There is :-)

https://github.com/ravendb/ravendb/blob/v4.0/src/Raven.NewClient/Json/BlittableJsonWriter.cs

Can you explain about large documents?

Alois Kraus
04 Jan 2017
12:54 PM
Alois Kraus

I have played around with the Blittable classes a bit. The managed heap is basically empty if I deserialize a 200MB json file. That is great. Even when I access the strings as LazyStrings it is still quite fast. But what is the story when you have deseralized the json into a BlittableJsonReaderObject and you want to keep it? You cannot add or change items in it since it is read only. Is this class meant as intermediary only to later materialize a normal CLR object or is it possible to mutate the BlittableJsonReader..... objects. If you still need to copy things into normal objects you loose the gained speed while reading very quickly. My current test looks like

                using (var jsonContext = new JsonOperationContext(20 * 1024 * 1024, 30 * 1024 * 1024))
                {
                    var json = jsonContext.ReadForDisk(inFile, "state");
                    var list = (BlittableJsonReaderArray)json["LargeList"];
                    List<string> data = new List<string>();
                    
                    var strSearch = "List Element 9999999";
                    for (int i=0;i<list.Length;i++)
                    {
                        var tmp = (LazyStringValue) list[i];
                        if(strSearch == tmp )
                        {
                            Console.WriteLine($"Found at {i}");
                        }
                        
                    }
                    json.Dispose();
                }

which uses this definition

    [ProtoContract]
    [DataContract]
    public class Data
    {
        [ProtoMember(1)]
        [DataMember(Order=1)]
        public string Name
        {
            get;
            set;
        }

        [ProtoMember(2)]
        [DataMember(Order = 2)]
        public string Description
        {
            get;
            set;
        }

        [ProtoMember(3)]
        [DataMember(Order = 3)]
        public List<string> LargeList
        {
            get;
            set;
        }

        internal DynamicJsonValue ToJson()
        {
            return new DynamicJsonValue
            {
                ["Name"] = Name,
                ["Description"] = Description,
                ["LargeList"] = new DynamicJsonArray(LargeList),
            };
        }
    }
Oren Eini
04 Jan 2017
14:41 PM
Oren Eini

Alois,

Yes, blittable is meant to be immutable, that make both the structure and working with it much simpler. You are not meant to really mutate a blittable, you would typically send it to the user (CLR class, network, etc) and then get a whole new object back.

While we do have support of applying mutations, they require re-generating the blittable.

Note that stuff like searching inside the blittable like you do would typically be the other way around. Take the CLR string and turn that into a LazyString, then compare it directly to the blittable value.

Alois Kraus
04 Jan 2017
15:10 PM
Alois Kraus

Ahh ok. So this whole Blittable thing is only there to make queries over many small or some large documents cheap for the managed heap to prevent long GC pauses if the next Gen2 or Gen1 from the finalizer due to low memory kicks in? I was hoping to get something faster than Protocol Buffers or JSON.NET which also allows mutation of the read objects. Of course it is possible to come up with hybrid objects which store the modifications in normal objects but that is not something for the generic case. No matter how hard I try I seem to hit a wall at ca. 50-80 MB/s with managed code. The only thing left would be to deserialize at different locations from the stream in parallel. But that would impose severe limitations on the object design.

Oren Eini
04 Jan 2017
15:13 PM
Oren Eini

Alois, No, blittable is how we work with json in 4.0 Server & client side. However, we generate don't mutate json objects directly, we either pass them around (From server to client, etc) or we build them directly and sending them over the network.

Oren Eini
04 Jan 2017
15:14 PM
Oren Eini

Alois, Let us go back a few steps. What is it that you are trying to do ?

Alois Kraus
04 Jan 2017
15:50 PM
Alois Kraus

I currently have a large (up to 200 MB) Xml document serialized with DataContracts on disk which needs to be deserialized. I am searching for something significantly faster than plain DataContracts. If I redesign that stuff I want to use the fastest library out there. Blittable Json seems like a good idea if I can easily round trip the data with modifications. I care less about serialization but mostly about deserialization performance. So far it looks like JSON.NET would be significantly faster without requiring many changes to the current object model. While reading Blittable JSON is very fast I would loose the gained speed when I need to convert the Blittable JSON into the original object model back again. Since Blittable JSON is read only I cannot toss the original object model out of the window since the whole thing needs to be modifyable as well.

Oren Eini
04 Jan 2017
16:51 PM
Oren Eini

Do you need to read & write, or just read? Note that we provide APIs to both read & write, but it is meant for streaming, not ongoing mutation

Alois Kraus
04 Jan 2017
20:50 PM
Alois Kraus

Hard to tell since ther are quite some types serialized into that container. It will be mostly read only but for some state machine states I am not so sure if they will not change anymore after they have been deserialized back. I mainly edit other peoples code where I am seldom 100% sure what it exactly does ;-).

Comment preview

Comments have been closed on this topic.

Markdown formatting

ESC to close

Markdown turns plain text formatting into fancy HTML formatting.

Phrase Emphasis

*italic*   **bold**
_italic_   __bold__

Links

Inline:

An [example](http://url.com/ "Title")

Reference-style labels (titles are optional):

An [example][id]. Then, anywhere
else in the doc, define the link:
  [id]: http://example.com/  "Title"

Images

Inline (titles are optional):

![alt text](/path/img.jpg "Title")

Reference-style:

![alt text][id]
[id]: /url/to/img.jpg "Title"

Headers

Setext-style:

Header 1
========
Header 2
--------

atx-style (closing #'s are optional):

# Header 1 #
## Header 2 ##
###### Header 6

Lists

Ordered, without paragraphs:

1.  Foo
2.  Bar

Unordered, with paragraphs:

*   A list item.
    With multiple paragraphs.
*   Bar

You can nest them:

*   Abacus
    * answer
*   Bubbles
    1.  bunk
    2.  bupkis
        * BELITTLER
    3. burper
*   Cunning

Blockquotes

> Email-style angle brackets
> are used for blockquotes.
> > And, they can be nested.
> #### Headers in blockquotes
> 
> * You can quote a list.
> * Etc.

Horizontal Rules

Three or more dashes or asterisks:

---
* * *
- - - - 

Manual Line Breaks

End a line with two or more spaces:

Roses are red,   
Violets are blue.

Fenced Code Blocks

Code blocks delimited by 3 or more backticks or tildas:

```
This is a preformatted
code block
```

Header IDs

Set the id of headings with {#<id>} at end of heading line:

## My Heading {#myheading}

Tables

Fruit    |Color
---------|----------
Apples   |Red
Pears	 |Green
Bananas  |Yellow

Definition Lists

Term 1
: Definition 1
Term 2
: Definition 2

Footnotes

Body text with a footnote [^1]
[^1]: Footnote text here

Abbreviations

MDD <- will have title
*[MDD]: MarkdownDeep

 

FUTURE POSTS

  1. Optimizing the cost of clearing a set - 2 days from now
  2. Scaling HNSW in RavenDB: Optimizing for inadequate hardware - 4 days from now

There are posts all the way to May 14, 2025

RECENT SERIES

  1. RavenDB News (2):
    02 May 2025 - May 2025
  2. Recording (15):
    30 Apr 2025 - Practical AI Integration with RavenDB
  3. Production Postmortem (52):
    07 Apr 2025 - The race condition in the interlock
  4. RavenDB (13):
    02 Apr 2025 - .NET Aspire integration
  5. RavenDB 7.1 (6):
    18 Mar 2025 - One IO Ring to rule them all
View all series

RECENT COMMENTS

  • But in case you have nullability checks enabled (i.e. `<Nullable>enable</Nullable>`), then you'll have a compiler warning on ...
    By Samyon Ristov on The null check that didn't check for nulls
  • Grok wasn't *wrong*. It only said that `_items` can't be null for the condition to evaluate to `true`, but didn't say anythi...
    By Johannes Egger on The null check that didn't check for nulls
  • When I started enabling NRT, I remember I was initially confused when all variables (for reference types) declared with `var`...
    By riccardo on The null check that didn't check for nulls
  • That is surprising - I think of var as a shorthand that does not affect the final result of the compilation. I wouldn't expec...
    By Chris B on The null check that didn't check for nulls
  • "It is also about as friendly as a monkey with a sore tooth and an alcohol addiction." And I have to clean my monitor.
    By Tim on When racing the Heisenbug, code quality goes out the Windows

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats
}