All 56 entries tagged Coding

View all 78 entries tagged Coding on Warwick Blogs | View entries tagged Coding at Technorati | There are no images tagged Coding on this blog

October 18, 2006

Ohloh

Writing about web page http://next.ohloh.net/projects/3222

http://www.ohloh.com/ is a web site that analyses open source projects and tells you how much time/money it would have taken develop them.

I just added BindingListView

Stats calculated by ohloh:
Codebase 3,723 LOC
Effort (est.) 1 Man Years
Avg. Salary 55000$/year
Total $43,431

Damn! That would solve my current money situation…
Of course the project is open source and so far I’ve had zero donations (despite 796 downloads!).
Although I do use the library in some private applications, where it saves me a whole bunch of time.


October 07, 2006

Nemerle VSIP

I am planning to write my project’s code using Nemerle . It has a good cross section of language features that I will leverage. The concentration on efficient algorithms and data structures will leverage the functional aspects of Nemerle. Whilst the vast .NET libraries and imperative constructs will make image handling and UI code easy.

Of course, being a Visual Studio junkie means I will require decent tool support. Luckily I managed to get the Nemerle VSIP (Visual Studio Integration Package) to work yesterday. Now I can edit code inside Visual Studio with the aid of intellisense, syntax highlighting and built-in compiler support.
The package is still somewhat under development, so I’m not sure how stable it will be. My project will not require too much code so I think it will hold up OK. To be honest, just having a Visual Studio “project” to manage building source, etc is a God send compared to manually writing build scripts and command line compiling!

I’m looking forward to getting this project off the ground. My current goal is writing the proposal document by week 3…


October 01, 2006

Circular, doubly–linked list

Follow-up to Loopy linked lists from codeMonkey.Weblog();

After taking considerable inspiration from a Haskell implementation , I managed to write some Nemerle code that creates a circular, doubly-linked list, without using any mutable variables.
It is not as elegant as the Haskell version because I had to use class fields to get lazy access to variables not yet assigned. Translating lines like:

mkDList xs = let (first,last) = go last xs first
             in  first

into Nemerle is tricky, even with the “lazy” macro.

So here’s the code…

#pragma indent

using System
using Nemerle

[Record] struct Node[T]
    public Prev : LazyValue[Node[T]]
    public Value : T
    public Next : LazyValue[Node[T]]

class LoopList[T]
    private first : Node[T]
    private last : Node[T]

    private this(items : list[T])
        def go = Go(lazy(last), items, lazy(first))
        (first,last) = (go.me, go.last)

    private class Go
        private rest : LazyValue[Node[T]]
        public me : LazyValue[Node[T]]
        public last : LazyValue[Node[T]]

        public this(prev : LazyValue[Node[T]], items : list[T], next : LazyValue[Node[T]])
            match((prev, items, next))
                | (prev, [], next) =>
                    (me, last) = (next, prev)

                | (prev, x::xs, next) =>
                    me = lazy(Node(prev, x, rest))
                    def go = Go(me, xs, next)
                    (rest, last) = (go.me, go.last)

    public static Create(xs : list[T]) : Node[T]
        LoopList(xs).first

// Test Code

mutable n = LoopList.Create([1,2,3,4])

Console.WriteLine("Forwards")
repeat(10)
    Console.Write($"$(n.Value) ")
    n = n.Next

n = n.Prev
Console.WriteLine()
Console.WriteLine("and back again")
repeat(10)
    Console.Write($"$(n.Value) ")
    n = n.Prev

// Output
/*
Forwards
1 2 3 4 1 2 3 4 1 2
and back again
2 1 4 3 2 1 4 3 2 1
*/

I’m planning to use this in my 3rd year project. The Weiler-Atherton algorithm requires a CDDL.


August 06, 2006

Loopy linked lists

I've been playing with Nemerle today. It's a functional/OO hybrid for the .NET CLR. I needed a circular linked list for a project I'm working on. Basically a linked list where the end points back to the start.
Whilst Nemerle allows mutable variables, I felt dirty leaving all the beauty of "no side effects" programming behind. Luckily Nemerle allows lazy evaluation using some macro magic behind the scenes (That's right Dan, I said the "L" word ;)). This meant I could reference the first item effectively inside its own definition. (The laziness is not exactly first class like in Haskell, but it certainly works for me here.)
#pragma indent


using System
using Nemerle


class LoopList[T]
private first : LazyValue[LoopListItem[T]]


public this(values : list [T])
def build(xs)
| [] => first
| head::tail => lazy( LoopListItem(head, build(tail)) )
first = build(values)


public First : LoopListItem[T]
get
first



[Record] struct LoopListItem[T]
public Value : T
public Next : LazyValue[LoopListItem[T]]



// Test code:
mutable item = LoopList([1,2,3]).First


repeat(10)
Console.Write($"$(item.Value) ")
item = item.Next


// Outputs:
// 1 2 3 1 2 3 1 2 3 1
Next I need to figure out a circular doubly linked list…

August 05, 2006

BindingListView speedy sorting

I received some feedback about my BindingListView library saying it was very slow for sorting large lists. At first I thought 20,000 objects in a view was a bit extreme, but I tested with a DataView object and it worked OK. So I decided to hunt down the bottleneck in BindingListView sorting.
Using the CLR Profiler I determined it was reflection calls taking most of the execution time (no suprises there, reflection is very slow!)
When sorting the BLV used reflection to read the sorted property value from each object. The sorting 20,000 objects means and lot of calls to "GetValue", resulting in crippled performance.
The solution came from .NET 2.0's System.Reflection.Emit.DynamicMethod class. Using this class I was able to emit the IL (Microsoft .NET Intermediate Language) that directly accessed an object's property. (Of course the name of the property is supplied at runtime.)
I actually generate a few different methods depending on it the property is a value type or reference type. Since a value type needs to be boxed before we can compare using IComparable.
Here is one of the methods:
private static Comparison BuildRefTypeComparison(PropertyInfo pi, ListSortDirection direction)
{
MethodInfo getMethod = pi.GetGetMethod();
Debug.Assert(getMethod != null);

DynamicMethod dm = new DynamicMethod("Get" + pi.Name, typeof(int), new Type[] { typeof(T), typeof(T) }, typeof(T));
ILGenerator il = dm.GetILGenerator();

// Get the value of the first object's property.
il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Call, getMethod, null);

// Get the value of the second object's property.
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Call, getMethod, null);

// Cast the first value to IComparable and call CompareTo,
// passing the second value as the argument.
il.Emit(OpCodes.Castclass, typeof(IComparable));
il.EmitCall(OpCodes.Call, typeof(IComparable).GetMethod("CompareTo"), null);

// If descending then multiply comparison result by -1
// to reverse the ordering.
if (direction == ListSortDirection.Descending)
{
il.Emit(OpCodes.Ldc_I4_M1);
il.Emit(OpCodes.Mul);
}

// Return the result of the comparison.
il.Emit(OpCodes.Ret);

// Create the delegate pointing at the dynamic method.
return (Comparison)dm.CreateDelegate(typeof(Comparison));
}

Emitting IL by hand is not pretty (see my previous post for an idea to improve this problem). Using Reflector (or ILdasm) certainly is a must if, like me, you are an IL newbie. I wrote some simple example methods in C#, compiled them, then viewed in Reflector to see the IL they produced.

The end result in my speed test harness was BindingListView sort actually out performing the DataView!
Using 200,000 objects (10x more than the reported amount). Sorting on an Int32 property.
Old BindingListView took 146,823ms
New BindingListView took 16,564ms
DataView took 20,654ms

All the code is online at Source Forge


August 04, 2006

Runtime code generation, with macro ease

.NET 2.0 has some very cool stuff in System.Reflection.Emit, namely the DynamicMethod class. DynamicMethod will let you create a new method at runtime that lives inside your current assembly. The only hitch is that you have to create the method using IL – not a high level language like C#/VB/Boo/etc.

My thought for today is: Can we use the syntactic macro abilities of .NET languages like Boo and Nemerle to create dynamic method code whilst remaining in the source language.

For example (psuedo–Nemerle):

myMethod = dynamic( Console.WriteLine("Hello, World") );
myMethod() // Call the dynamically generated method

At compile time this would take the AST passed to the "dynamic" macro, call out to the Nemerle compiler and retrieve the IL. This IL is then used to create a sequence of Emit*() calls needed to create the dynamic method. Finally the macro creates a delegate that can be used to call the method.

Whilst that may seem rather complex, once working it totally removes the need to drop to IL to generate dynamic methods at runtime. I've not had time to think through the implementation details of this. For now I'm just throwing the idea out there – If someone wants have a go, please feel free! The main sticking point is really working out an escaping system to allow interpolation of real code and IL emits. This can probably be done using a second macro "normal" that switches back to normal code.

There is something rather magic about using compile–time macros to generate code for runtime methods. It feels like both sides of meta–programming sync up so nicely, adding a whole new dimension to "code that writes code"! :)


July 17, 2006

Busy times ahead

It's so nice to have the summer vacation to do what ever I please. However it comes at a price: having to choose!

I'm currently involved in a few open source projects (BindingListView , Specter and BooML )
I also have to eat… so I'm working on some commerical software too.
I really should try to get out and see friends too! Damn, 3 months may not be enough for it all.

Hopefully I'll find time to keep blogging too… :)


BooML Source Online

Follow-up to BooML SourceForge Project Pending from codeMonkey.Weblog();

The source for BooML is now online. The project is hosted by SourceForge. link

So far I've just got the source into SubVersion. I'm so busy at the moment (too many pies, not enough fingers!) – but I really must get some docs online too.


July 01, 2006

BooML SourceForge Project Pending

Follow-up to BooML – Language Integrated XML in Boo from codeMonkey.Weblog();

I have submitted a project request to SourceForge for BooML. I hope it is processed faster than Specter was!

BooML – Language Integrated XML in Boo

Creating XML using the .NET XmlWriter object model tends to make your code look awful. I realised that using some mackery I could make Boo support creating XML right in the language. Thus was born "BooML"

Here is a little example.

import System
import System.IO
import System.Xml
import BooML

class Book:
[property(Title)]
private _title as string
[property(Author)]
private _author as string

def GetBooks():
return [
Book(Title: "Foo Bar", Author: "Bob Smith"),
Book(Title: "Test Title", Author: "Andrew Davey")
]

[XmlLiteral()]
def GetBooksXml(books):
books:
for book as Book in books:
book:
@id = book.GetHashCode()
title book.Title
author book.Author


xml = GetBooksXml(GetBooks())
print xml.ToString()
The resulting XML is:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="54267293">
<title>Foo Bar</title>
<author>Bob Smith</author>
</book>
<book id="18643596">
<title>Test Title</title>
<author>Andrew Davey</author>
</book>
</books>

The magic is in the XmlLiteral AST Attribute. It transforms macros (in the example: books, book, @id, title and author) into XML macros. The XML macros then in turn create the XmlTextWriter calls.

I think this could be a really powerful language enhancement for people doing web development who want to return abitrary XML from methods.

I may start a SourceForge project to open source the development.


Google Ads

Search this blog

Most recent comments

  • I scratched my eye while i was holding some kind of plastic packaging.. Anyways the corner of the pl… by Ercan on this entry
  • About a year ago my contacts that i was wearing, i guess were fautly, because shortly after they wer… by Jon on this entry
  • I got shower gel in my eye 4 and a half days ago, and becasue i rubbed my eyes a lot, i have scratch… by Chris on this entry
  • This website may help http://www.webmd.com/eye–health/tc/Eye–Injuries–Home–Treatment by S on this entry
  • I somehow got dirt, or debris in my eye. The terrible pain sent me in a tailspin. I was afraid of sa… by Bobbi on this entry

Tags

January 2022

Mo Tu We Th Fr Sa Su
Dec |  Today  |
               1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31                  

Galleries

Blog archive

Loading…
RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder
© MMXXII