Data Binding: A godsend or the devil in disguise?

原创 2004年07月01日 01:17:00


When .NET was released, many web developers were ecstatic when they saw how easy it was to use server side ASP.NET controls. Slap the control on the page, set a DataSet or ArrayList to the DataBind property and away you go, instant web pages. No more looping through RecordSets in your ASP to build your HTML. This is so easy!

It is easy, but at a cost. I recently worked on a project that used data bound ASP.NET DataList and Repeater controls extensively to build their web pages. But the performance results were very disappointing. Using ACT to run some load tests on these pages, with a small number of concurrent users (5), the page performed reasonably well. But as soon as we increased the number of users to something more realistic, like 25, the page performance went to pot. So we started using PerfMon while the tests were running, and found something pretty interesting. The % time in garbage collection for the page was averaging 30%, with a maximum spike of 45%! Also, the % Processor was pegged at 95% for the entire test run. These last two statistics were big red warning lights, because they told us that not only did our pages run slow, but they were not going to be able to scale out at all. If the site had a high load of users, it would be in serious trouble.

This was no good and was not acceptable for a release to production, so we started digging into how the data bound controls worked. What we found out was that the data binding process was doing two things that were hurting performance. First, data bound controls use reflection to find the correct property and pull the data from it. Reflection is fairly costly and if you have a repeater control that is pulling 6 properties from an array of 40 objects, the performance hit can really add up.

The second thing we noticed was that the number of objects that were being created during the data binding process was pretty high (look in Anakrino at the DataGrid, DataList and Repeater class’ CreateControlHierarchy to see how it does its binding). This high number of objects being created was what was kicking the % time in garbage collection so high.

So we had to find a way to create web pages without using data binding. We tried using ASP.NET server controls and manually pushing the data, but this didn’t really change our statistics very much. Then we got desperate, and really started brainstorming. We tried placing one Literal control on each page and used a StringBuilder in the code behind’s PageLoad event to build the HTML structure for the page and then slapping the HTML into the Literal control’s text property. This technique performed amazingly well and the % time in garbage collection went down to almost nothing. But the maintainability of the HTML would have been a nightmare.

We then decided to try mixing ASP.NET code behind with an ASP style of HTML building. We created and populated all our data objects, as well as put any business logic the page needed in the aspx’s code behind PageLoad event. Then in the aspx file, we went back to ASP style HTML building, using old fashioned <%=(C# code)%> to actually inset data from our data objects into the HTML. This technique performed just as good as the string builder technique, but the maintainability of the code was much better.

The only problem with this ASP style of HTML rendering is that you are back to writing the HTML to a forward only stream, just like ASP. When using ASP.NET controls, you can update the value of any control, during any stage in the code. But web developers have been doing this since the beginning of ASP, so in extreme situations where ASP.NET controls just won’t perform, then this is a workable option.

Once we had our test page coded this way, I ran ACT on the data bound version and the new ASP style version, and compared the results. During a 15 minute run with 10 users, the ASP style page was able to run iterations as many times as the data bound version. The average requests per second jumped from 72.55 to 152.44 requests per second. The average time to last byte went from 21.79 milliseconds down to an amazing 2.57 milliseconds! But the best statistics came from the % time in garbage collection and processor %. The average % time in garbage collection went from 30% down to .79%, and the average processor % went from 95% down to 10%! This meant that our ASP style pages would scale out to a higher number of users with very little trouble.

Android Data Binding Library 官方文档(译)

Data Binding Library (数据绑定库),旨在减少绑定应用程序逻辑和布局所需的一些耦合性代码 最低支持Android 2.1 (API Level 7)...
  • jjwwmlp456
  • jjwwmlp456
  • 2017年02月07日 21:01
  • 2165

The Binding of a Data Member

《深度探索C++对象模型》第三章“Data语意学”  3.1 The Binding of a Data Member 下面一段代码的输出是什么? [xiaochu.yh@OceanBase c...
  • maray
  • maray
  • 2013年04月05日 11:50
  • 854

Data Binding 从入门到精通

原文出自: 薛瑄的博客虽然官方给出了教程,Data Binding Library(...
  • xx326664162
  • xx326664162
  • 2017年03月15日 19:06
  • 2707

Jackson应用一 Simple data binding

Jackson 是一个高性能的JSON Java 库,网址参考:先看看最简单的Simple data binding怎么使用。...
  • sheismylife
  • sheismylife
  • 2012年04月06日 10:23
  • 8098


OpenIL(DevIL)CxImage和OpenIL都两个对图片读取的库,前者是一个开源库,功能也比较强大,可以读取很多类型的图像文件,比如bmp,jpg,png ,tiff等等。后者也是一个对图片...
  • pizi0475
  • pizi0475
  • 2010年03月16日 14:25
  • 4256

【MVVM】 Android Data Binding语法解析(二)

上篇我们知道了Data Binding的最简单的用法,那么Data Binding其中最为重要也是最复杂的其实就是在xml布局文件中给对应的控件进行数据绑定了,接下来就一一说明Data Binding...
  • u013378580
  • u013378580
  • 2016年09月22日 17:57
  • 202

Data Binding 的使用之一:简单的数据绑定

android中Data Binding的简单使用
  • yeah0126
  • yeah0126
  • 2017年02月06日 17:00
  • 788

Data binding的使用(二)-----事件的绑定监听

上一篇文章Data binding的使用(一)讲了一些基础的关于如何开始使用Data Binding做了一些介绍,这篇文章继续讲解使用Data Binding的一些其他操作,比如点击事件,或者监听对象...
  • xiaxiayige
  • xiaxiayige
  • 2017年04月12日 16:29
  • 1508

Android Data Binding实战(一)

在今年Google I/O大会上,Google推出Design Library库的同时也推出了Android Data Binding,那么什么是Data Binding?其名曰数据绑定,使用它我们可...
  • u010687392
  • u010687392
  • 2015年08月05日 16:07
  • 3191

Android Data Binding 技术

Data Binding即数据绑定,Data Binding 库实现在布局文件中实现数据绑定申明,使数据的变化引起视图的自动更新,减少了逻辑代码,在Android中可以很方便的实现MVVM的开发模式。...
  • jmq_0000
  • jmq_0000
  • 2015年06月28日 00:21
  • 8321
您举报文章:Data Binding: A godsend or the devil in disguise?