.Net 4 intro­duced the Lazy<T> type which allows you to cre­ate an object that can be lazily ini­tial­ized so that you can delay the cre­ation of large objects, for instance.

How­ever, if your ini­tial­iza­tion logic has the poten­tial to except at run­time (e.g. time out excep­tions read­ing from some exter­nal data source) then you should pay close atten­tion to which con­struc­tor you use to cre­ate a new instance of the Lazy<T> type. Depend­ing on the selected LazyThread­Safe­t­y­Mode, excep­tions in the ini­tial­iza­tion code might be cached and rethrown on all sub­se­quent attempts to fetch the lazily ini­tial­ized value. Whilst this ensures that threads will always get the same result, hence remov­ing ambi­gu­ity, it does mean that you’ve got only one shot at ini­tial­iz­ing that value…

 

LazyThread­Safe­t­y­Mode

In cases where you need to be able to tol­er­ate occa­sional ini­tial­iza­tion errors (e.g. read­ing a large object from S3 can fail from time to time for a num­ber of rea­sons) and be able to try again at a sec­ond attempt, the rule of thumb is to instan­ti­ate the Lazy<T> type by set­ting LazyThread­Safe­t­y­Mode to Pub­li­ca­tionOnly. In Pub­li­ca­tionOnly thread safety mode, mul­ti­ple threads can invoke the ini­tial­iza­tion logic but the first thread to com­plete the ini­tial­iza­tion suc­cess­fully sets the value of the Lazy<T> instance.

For exam­ple, the fol­low­ing only works under the Pub­li­ca­tionOnly mode:

 

F#

F# pro­vides a slightly nicer syn­tax for defin­ing a lazy com­pu­ta­tion:

image

the Control.Lazy<T> type is an abbre­vi­a­tion of the BCL Lazy<T> type with a Force exten­sion method which under the hood just calls Lazy<T>.Value.

Pre­sum­ably the above trans­lates roughly to the fol­low­ing C# code:

var x = 10;

var result = new Lazy<int>(() => x + 10);

and the thread safety mode using the Lazy(Func<T>) con­struc­tor is LazyThreadSafetyMode.ExecutionAndPublication which caches and rethrows any excep­tions caught in the ini­tial­iza­tion. E.g.:

image

Share

After a long Easter hol­i­day filled with late night cod­ing ses­sions I find myself wide awake at 2am… good job I’ve still got my plu­ral­sight sub­scrip­tion and a quick look at the Algo­rithms and Data Struc­tures course again at least gave me some­thing to do to relax the mind with some back-to-school style imple­men­ta­tion of com­mon sort­ing algo­rithms in F#:

Whilst not the most per­for­mant imple­men­ta­tions I’m sure, I hope at least it goes to show how eas­ily and con­cisely these sim­ple algo­rithms can be expressed in F#! Now back to that sleep thing…

Share

I talked about the XML and JSON seri­al­iza­tion of F# types in a pre­vi­ous post, and since then F# 3.0 has been released and a new [CLIMutable] was qui­etly added amidst the hype sur­round­ing the awe­some type providers!

 

CLIMutable Attribute

When applied to a F# record type, the CLIMutable attribute tells the F# com­piler to gen­er­ate a default para­me­ter­less con­struc­tor as well as prop­erty get­ters and set­ters so that when work­ing with the record type from other .Net lan­guages it’ll appear as a pretty stan­dard, muta­ble CLI type that you’d expect.

How­ever, when work­ing in F#, the nor­mal rules as far as record types are con­cerned still apply – immutabil­ity by default, pat­tern match­ing, cloning using the with key­word, etc.

For a sim­ple record type Per­son, let’s have a look at the com­pile code with and with­out the CLIMutable attribute:

image image

With­out the CLIMutable attribute, this is how the record type is nor­mally compiled:

image

With the CLIMutable attribute applied, things look a lot different:

image

That’s all well and good, but why is this use­ful you might ask?

Because many seri­al­iz­ers, such as the BCL’s XmlSe­ri­al­izer (as well as other seri­al­iz­ers avail­able in the open source space) only work with types that has a default con­struc­tor and con­tains prop­erty setters.

 

XmlSe­ri­al­izer and Record types

Unlike the Dat­a­Con­tract­Se­ri­al­izer which only requires a type to be dec­o­rated with a Seri­al­iz­able or Dat­a­Con­tract attribute, the XmlSe­ri­al­izer on the other hand, also requires the type to define a para­me­ter­less constructor.

Whilst both seri­al­iz­ers gen­er­ate XML out­puts, Dat­a­Con­tract­Se­ri­al­izer is intended for mar­shalling data (that con­forms to a ‘con­tract’) for trans­porta­tion over the wire and there­fore less con­cerned with read­abil­ity of its out­put and offers lit­tle option for you to cus­tomize the gen­er­ated XML. It’s there­fore quite happy to spit out rather unread­able XML for our Per­son record type above:

 

<FSI_0004.Person xmlns=“http://schemas.datacontract.org/2004/07/”  xmlns:i=“http://www.w3.org/2001/XMLSchema-instance”><Age_x0040_>30</Age_x0040_><Name_x0040_>Yan Cui</Name_x0040_></FSI_0004.Person>

 

Mean­while, in the land of the XmlSe­ri­al­izer, its sole pur­pose of exis­tence is to gen­er­ate XML and offers a pla­toon of options to help ensure its out­put meets your func­tional and aes­thetic needs. By default it’ll gen­er­ate nice, indented out­put for our CLIMutable ver­sion of Per­son record type:

 

<?xml version=“1.0″?>

<Per­son xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema”>

  <Name>Yan Cui</Name>

  <Age>30</Age>

</Person>

 

In addi­tion, you can cus­tomize the XML out­put using the nor­mal XML-related attributes:

image

<?xml version=“1.0″?>

<Per­son xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” PersonName=“Yan Cui”>

  <PersonAge>30</PersonAge>

</Person>

 

OSS Seri­al­iz­ers

Of the OSS JSON seri­al­iz­ers I bench­mark reg­u­larly, JSON.Net (v4.5.11 tested) works with both nor­mal and CLIMutable record types from F#. ServiceStack.Text (v3.9.37 tested) doesn’t work with ‘vanilla’ record types on dese­ri­al­iza­tion (fields are left with default val­ues), but you can workaround by mak­ing the fields muta­ble or add the CLIMutable attribute to the type.

 

I hope this proves to be use­ful to you, and if like us you have a lot of C# code hang­ing around just to get pretty XML out of your F# record types, dare I say it’s about to throw away those C# code! Winking smile

Share

Just a quick note to say that another minor update to DynamoDB.SQL has been release, you can view the release notes here.

 

The lat­est update adds sup­port for a TSQL style WITH key­word for spec­i­fy­ing optional para­me­ters for tweak­ing the query/scan oper­a­tion. For queries, you can spec­ify the NoCon­sis­ten­tRead and Page­Size options to use even­tu­ally con­sis­tent read and throt­tle the num­ber of items returned per request respec­tively. Sim­i­larly for scans, you can use the Page­Size option for throt­tling your scan requests too, but the DynamoDB scans does not sup­port strong con­sis­tency.

 

Accord­ing to DynamoDB best prac­tices, you should avoid sud­den bursts of read activ­ity, using the new Page­Size option you can make sure that your query/scan does not con­sume too many read capac­ity unit in a short burst and end up caus­ing more crit­i­cal reads to be throttled.

 

For exam­ple, a query which returns 10 items per request using even­tu­ally con­sis­tent read will look some­thing like this:

image

whereas a scan will look like:

image

For more details about the full syn­tax, please refer to the Get­ting Started doc­u­ment, which has been updated to include the new WITH key­word.

 

Enjoy!

Share

Whilst search­ing for an ele­gant solu­tion to apply string intern­ing across a large num­ber of classes (we’re talk­ing about hun­dreds of classes here..) it dawned on me that I can achieve this with ease using PostSharp’s Loca­tion­In­ter­cep­tionAspect. All I needed was some­thing along the lines of:

You can apply this attribute to a class or even a whole assem­bly and it’ll ensure every piece of string con­structed is interned, includ­ing string prop­er­ties and fields defined by its sub­class, which is exactly what I was after.

For exam­ple, take this triv­ial piece of code:

image

If you inspect the com­piled code for the Base class in ILSpy you will see some­thing along the lines of:

image

notice how the set­ter for BaseS­tring­Prop­erty has been mod­i­fied to invoke the OnSet­Value method defined in our aspect above as opposed to the set­ter method. In this case, it’ll call the String.Intern method to retrieve a ref­er­ence to an interned instance of the string and set the prop­erty to that reference.

For more details on PostSharp’s inter­cep­tion aspects, I rec­om­mend read­ing Dustin Davis’s excel­lent posts on the topic:

Post­Sharp Prin­ci­ples: Day 7 Inter­cep­tion Aspects – Part 1

Post­Sharp Prin­ci­ples: Day 8 Inter­cep­tion Aspects – Part 2

 

As we’ve spec­i­fied the mul­ti­cast inher­i­tance behav­iour to mul­ti­cast the attribute to mem­bers of the chil­dren of the orig­i­nal ele­ment, the string prop­er­ties defined in both A and B classes are also sub­ject to the same string intern­ing treat­ment with­out us hav­ing to explic­itly apply the Inter­nAt­tribute on them:

image

 

F# Com­pat­i­ble

What’s more, this attribute also works with F# types too, includ­ing record and dis­crim­i­nated unions types. Take for instance:

image

If you look at the gen­er­ated C# code for the dis­crim­i­nated union type, the inter­nal MyDuType.CaseB type would look some­thing like the following:

image

notice how the two inter­nal item1 and item2 properties’s set­ter meth­ods have been mod­i­fied in much the same way as the C# exam­ples above? The pub­lic Item1 and Item2 prop­er­ties are read-only and get their val­ues from the inter­nal prop­er­ties instead.

Indeed, when a new instance of the CaseB type is con­structed, it is the inter­nal prop­er­ties whose val­ues are initialized:

image

 

Finally, let’s look at the record type, which inter­est­ingly also defines a non-string field:

image

because we have spec­i­fied that the Inter­nAt­tribute should only be applied to prop­er­ties or fields of type string (via the Com­pile­TimeVal­i­date method which is exe­cuted as part of the post-compilation weav­ing process as opposed to run­time), so the inter­nal rep­re­sen­ta­tion of the Age field is left unaltered.

The Name field, how­ever, being of string type, was sub­ject to the same trans­for­ma­tion as all our other examples.

 

I hope this lit­tle attribute can prove to be use­ful to you too, it has cer­tainly saved me from an unbear­able amount of grunt work!

Share