The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Archive for the ‘.NET’ Category

Checking KeyPress is not the place to do your input validation

Posted by jpluimers on 2017/09/19

I have seen too many projects over the years trying to do input validation by checking KeyPress. This is not limited to Delphi projects (C#, VB and other projects suffer from this just as much). Most of these projects suffer from these::

  • Much of the KeyPress logic logic in the UI byusing half-baked copy-pasted code fragments.
  • They all fail missing important functionality (like paste, clear, Ctrl-key handling and such) either supporting or suppressing that functionality where needed

If doing Delphi, then that code should be rewritten in a generic way based on examples like like these:

–jeroen

Read the rest of this entry »

Posted in .NET, Delphi, Development, Software Development, Windows Development | Leave a Comment »

Great diagram on composing a LINQ query – via Mastering C# – CodeProject

Posted by jpluimers on 2017/08/17

One of the best to graphs diagrams of LINQ I know is in Mastering C# – Lecture Notes Part 2 of 4 – CodeProject [WayBack]

The LINQ explanation in that article [WayBack] is top notch as well. Thanks Florian Rappl [WayBack]!

–jeroen

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C# 6 (Roslyn), Development, Software Development | Leave a Comment »

Closed: HDD Guardian – Home

Posted by jpluimers on 2017/08/16

Too bad: it was fun while it lasted.

Mid april 2017 [WayBackHDD Guardian – Home closed down, so the latest commit removed all the [WayBackHDD Guardian – Source Code.

HDD Guardian provides a Windows front-end for smartctl, a utility which monitors your hard drive(s) and SSD(s) for health status, taking advantage of S.M.AR.T.

The WayBack machine and Archive.is have archived some links though:

Anyone interested in getting it before codeplex itself shuts down:

git svn clone https://hddguardian.svn.codeplex.com/svn

Edit 20210909:

Note that a while ago, the salvaged source code got pushed to GitHub: [Wayback/Archive.is] native-api/hddguardian: A GUI app to watch and manage HDDs’ S.M.A.R.T., based on smartmontools. Salvaged from https://hddguardian.codeplex.com

–jeroen

Posted in .NET, Development, Power User, Software Development, VB.NET, Windows | Leave a Comment »

On List growth strategies and memory managers

Posted by jpluimers on 2017/08/03

Interesting for anybody working on list growth strategies.

In this case with some Delphi background information and in depth coverage of FastMM memory (re)allocation strategies.

[WayBack] Stefan Glienke (of [WayBack] Spring4D fame) needed some help with allocation strategies and observed the difference between:

  • TList.Grow (and TStringList.Grow) growing like this: 4, 8, 12, 28, 44, 60, 76, 95, 118, 147, 183, 228, 285, 356, 445, 556, 695, 868, 1085
  • Generic TList<T> growing  the same way as the .NET List<T>: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024

There is this nice [WayBackDynamic array – Growth factor – Wikipedia, the free encyclopedia mentioning this table:

Implementation Growth factor (a)
Java ArrayList[1] 1.5 (3/2)
Python PyListObject[7] 1.125 (9/8)
Microsoft Visual C++ 2013[8] 1.5 (3/2)
G++ 5.2.0[5] 2
Clang 3.6[5] 2
Facebook folly/FBVector[9] 1.5 (3/2)

[WayBack] Javier Hernández mentioned he doesn’t think exponential is better than n^2.

[WayBack] Eric Grange (of [WayBackDWS and [WayBackbeginend.net fame) mentions he tends to use 1.5, it is about as good as 2 for small lists, but reduces waste for large ones. He also uses a constant delta to accelerate growth early on, so something like:

n := n + (n div 2) + 8

Since allocation strategies highly depend on the memory allocator as well, I was glad [WayBackPrimož Gabrijelčič (of [WayBackOnmiThreadLibrary and [WayBackSmart Mobile Studio fame) elaborated on FastMM:

  • FastMM small block allocator sizes (size includes the leading header) are: 8, 16, 24, 32, … 160 (in +8 steps), 176, 192, … 320 (+16), 352, 384 … 480 (+32), 528, 576, … 672 (+48), 736, 800, 880, 960, 1056, 1152, 1264, 1376, 1504, 1648, 1808 , 1984, 2176, 2384. [FastMM4.pas, SmallBlockTypes global var]
  • While the size of reallocated region fits inside a small block (with a different size than a previous block), the data is moved around (new block is allocated from a new suballocator). If it is too big (>2384 bytes), it gets allocated from the medium block allocator (which handles all block sizes up to 264752 bytes; larger blocks come directly from VirtualAlloc).
  • When small blocks are reallocated (to a larger size), allocator always allocates at least 100% + 32 bytes larger block, even if less is requested by the RTL (example: 8 bytes will grow to 2*8 + 32 = 48 bytes). When medium blocks are reallocated, allocator always allocates at least 125% of the old size. This boosts the performance when blocks are enlarged by small values as they can be enlarged “in place” (no data moved around, just the header is adjusted).

Stefan Glienke and Primož Gabrijelčič then concluded that:

  • Resizing an array from say 4 elements (pointer size) to 1000 (in multiple steps) will for sure move several times when jumping from one region into the next larger one.
  • Changing to a growth factor of 1.5 vs 2 won’t change anything in terms of memory fragmentation in FastMM4.

Source: [WayBack] I was just looking at TList.Grow (and TStringList.Grow) and I realized that the…

Edit 20181127

Delphi 10.3 Rio makes this configurable in a global way for all threads at the same time (#facepalm! as it is the 1980s Turbo Pascal ExitProc mess all over again): [WayBack] Delphi RTL Improvements in 10.3 Rio via [WayBack] +Marco Cantù is unstoppable. I can’t keep up LOL  – Clement Doss – Google+

The SetGrowCollectionFunc is of course not documented in the RTL, only in the [WayBack] What’s New – RAD Studio 10.3 Rio: [WayBack] Search results for “SetGrowCollectionFunc” – RAD Studio 10.3 Rio.

Stefan Glienke commented in that G+ thread:

I recently experimented with different grow factors and while the memory fragmentation can only mitigated for medium and large blocks (where it actually matters imo) it might be beneficial to only grow by 1.5 at that point. But that has yet to be tested.
What I liked so far is the grow strategy that Go uses (2x until 1024, 1.25x after that) – see https://golang.org/src/runtime/slice.go#L115

Since you usually set the size upfront if you add many elements at once (well, if you know how many beforehand) the grow strategy only matters in the long run. You want to balance speed (too many realloc might slow things down unnecessarily), memory overhead (if you are overallocating much you risk wasting too much memory) and memory fragmentation (which might happen with a grow factor bigger than the golden ratio)

–jeroen

Posted in .NET, Delphi, Development, Java, Java Platform, Software Development | 7 Comments »

Microsoft Research’s manual memory management for .NET: exactly one owner which provides shields for accessing the objects

Posted by jpluimers on 2017/08/01

A very interesting piece of research, in which I see a very familiar concept of single owners and I new concept of them providing shields for accessing the manually managed memory. I do miss mentions of Anders Hejlsberg, Chuck (Charles) Jazdzewski, or others that lay the foundation of ownership in the [WayBackTComponent Branch.

Microsoft Research’s manual memory management for .NET: https://www.microsoft.com/en-us/research/wp-content/uploads/2017/07/snowflake-extended.pdf

Interesting concept of manual but safe memory management with exactly one owner of an object at any given moment and shields that prevent an object’s destruction while it’s still in use by other threads.

Source: [WayBackChristopher Wosinski – Google+

–jeroen

Posted in .NET, Delphi, Development, History, Software Development | Leave a Comment »

Anyone having a solution for “Microsoft Visual Studio” throwing “The operation could not be completed” when including a file in a PowerShell project?

Posted by jpluimers on 2017/06/22

The operation could not be completed.

The operation could not be completed.

Include In Project

Include In Project

I’ve got a bunch of PowerShell projects in a solution. In some of them, I can include new files, in others I get the below error.

The diff of a good/bad project is below as well.

Two questions:

  1. Does anybody know how to work around this?
  2. Does anybody know how to find the actual error for this?

---------------------------
Microsoft Visual Studio
---------------------------
The operation could not be completed
---------------------------
OK
---------------------------

Good file: WindowsLogsCbsInquiry.pssproj

Bad file: WindowsTempInquiry.pssproj

–jeroen

Read the rest of this entry »

Posted in CommandLine, Development, PowerShell, Software Development, Visual Studio 2015, Visual Studio and tools | Leave a Comment »

Keep consistent formatting – via: The Oracle at Delphi: Code is the language, formatting is the dialect.

Posted by jpluimers on 2017/06/13

When this developer finally went to commit his/her changes, they had also reformatted most of the codebase into his/her preferred coding style/format. This was even for files for which no other changes had been made!Imagine the next developer coming along and pulling down the latest changes from the source control system and trying to merge them into their own local changes.

Because of all these code-format-only changes, it became nearly impossible to merge any changes without going through every conflicted file and painstakingly reconcile the changes.

I’ve seen this happen on a few projects where there have been sequential single developers some of which reformatted the whole code base within a few days of taking over.

It made it impossible to perform a “blame” or proper history tracking of feature changes.

That increased the cost of maintenance a lot.

I’ve been on several teams that enforced a pre-checkin standardised formatting of the code. Only rarely that causes problems, usually it’s a blessing to as it makes for a consistent formatting of the code-base where it is much easier to cut the crap and focus on what the real problem is.

–jeroen

Source: The Oracle at Delphi: Code is the language, formatting is the dialect.

Posted in .NET, C#, Delphi, Development, Software Development | 2 Comments »

KirillOsenkov/MSBuildStructuredLog: A logger for MSBuild that records a structured representation of executed targets, tasks, property and item values

Posted by jpluimers on 2017/05/31

Cool: KirillOsenkov/MSBuildStructuredLog: A logger for MSBuild that records a structured representation of executed targets, tasks, property and item values

Basically it parses the XML output into something manageable.

Via Matthijs ter Woord.

–jeroen

Posted in .NET, Continuous Integration, Development, msbuild, Software Development | Leave a Comment »

MiloszKrajewski/LibZ: the alternative to ILMerge (Resolve instead of merge assemblies)

Posted by jpluimers on 2017/05/30

ILMerge has all sorts of drawbacks with things like XAML, WPF, NHibernate, dynamically loaded assemblies and reflection.

Jeffrey Richter: Excerpt #2 from CLR via C#, Third Edition | Microsoft Press blog has an interesting approach based on adding a callback to the AppDomain’s ResolveAssembly event with some steps so you can embed assemblies as resources which you then – unlike ILmerge- dynamically resolve.

Those steps require a bit of manual labour which is taken away by MiloszKrajewski/LibZ: LibZ, the alternative to ILMerge.

The repository on github even compresses your assembly resources.

–jeroen

Posted in .NET, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, C# 6 (Roslyn), Development, Software Development | Leave a Comment »

Anders Hejlsberg on Modern Compiler Construction | Seth Juarez | Channel 9

Posted by jpluimers on 2017/05/18

One of those “must watch” videos if you are remotely or more interested in how compilers influence our day to day coding activities.

The Red Dragon Book, first edition: Compilers. Principles, Techniques and Tools.

The Red Dragon Book, first edition: Compilers. Principles, Techniques and Tools.

It starts out with referring to the Dragon Book (well, actually the first edition of the Red Dragon Book, as there are three) describing the compilers as having front-ends consisting of a Lexer, Parser and Type Checker and back-ends consisting of Code Generator and Emitter. A full compilation is going through all five stages and there is an increasing cost using these traditional stages when going from syntax highlighting via collapsible regions to red squiggles and code completion will need to go further along those stages taking an increasing time – like seconds or even longer – whereas the user experience requires responses in ~ 100 milliseconds where his code might not even compile in the first place.

Then Anders goes on describing Roslyn, TypeScript and Compiler API JSON interfaces to them so you can run them as a service and keep compiler state, rebuilding just enough of the state on source code changes. He goes on talking about how Visual Studio, Visual Studio Code, Command-Line Compiler, Sublime Text and other tools (can) use these APIs to interact with the compiler so it keeps state of slowly evolving code of which the tools than can emit what they need.

Anders explains this much better and much more visually than I do: so it’s a highly recommended video.

Read the rest of this entry »

Posted in .NET, C#, Delphi, Development, Software Development | 2 Comments »