win32, MFC, WPF, winforms… vs Java/swing

Earliest/lowest support is win32api — a platform specific C [1] API. No counterpart in Java because JVM hides all platform-specific complexities, though the win32 JVM is written against win32api.

[1] C with Microsoft extensions, but not C++. I guess It’s a proprietary language based on ANSI-C.

win32api functions are like system calls AND standard library functions in unix. Plain old C functions, as low-level functions must be. All windows/unix applications rely on system API in C as not every app uses C++.

MFC is a c++ thin object wrapper over win32api functions, therefore platform-specific too. No counterpart in java. Borland used to offer a MFC competitor (viz OWL), but it also relies on win32api. So win32api is a choke-point, but MFC isn’t.

Similarly, winforms is also a wrapper over win32api (not over MFC). Unlike MFC, winforms was created for the dotnet virtual machine.

Someone on Stackoverflow said — Most application frameworks for Windows (at least partially) wrap the win32api. Thus, the .NET and JVM, likewise any other programming languages under Windows, are (or contain) wrapper libraries over win32api.

Winforms and WPF built on top of a virtual machine — the dotnet framework, and are (partially?) shielded from platform-specific complexities. I think these are like the swing library.

However, winforms is a dotnet wrapper over win32api, therefore faster than WPF.

———– gcc ————–
http://gcc.gnu.org/wiki/WindowsBuilding

GCC also goes through the choke point viz win32api. Mingw includes
* w32api (not spelt “win..”): This is a bunch of files, a free (compatible) implementation of the MS win32api, equivalent to the Windows SDK, which provides the system-level (OS level?) interface needed by mingw-runtime. It is possible, with some modifications, to use Microsoft’s Platform-SDK instead.

* mingw-runtime (version 3.12 or higher): This is a bunch of files, the equivalent of libc on Unix which provides an interface to Microsoft’s C runtime that is shipped with Windows.

——— how many ways to write an equivalent GUI app ——-
* write a C app against win32api direclty
* write a c++ app against MFC (wrapper over win32api) — no virtual machine at all
* write a c#/c++ winforms app, running in the virtual machine
* write a c# WPF app, running in the virtual machine
* write a mingw C/C++ app, against w32api — no virtual machine at all
* write a swing app, running in the JVM

However, if you want a windows app without a GUI, then you need a console app. You can use C++ without any of these complexities. Just use STL/boost. Windows programming != GUI programming.

shared_ptr: shared-exclusive ownership

Background — Like any useful software component, shared_ptr offers a clean api as facade in front of a sophisticated implementation, and involves many non-trivial interactions when integrated into an application — Complexity.

Many ways to wrap your mind around the complexity. This is one of the best Perspectives.

The various ways to construct a shared_ptr clearly illustrates the fundamental use cases. An experienced user is probably familiar with all these use cases. These use cases are so important that you should be able to explain each constructor. Here’s what I remember.

1) pass in a raw ptr — starting a new club as the first member.
2) copier — joining an existing club.
3) pass in a weak_ptr
) pass in an auto_ptr

Note the good old auto_ptr is a club of 1. When “joining” a club you must remove the existing “owner”.

Shared_ptr implements shared yet exclusive ownership within an exclusive club of equals. No other (club of) owners allowed.

nested class having ptr to outer-class Object]java,c#,c++

Usually java is cleaner than c++ and c#. However, in this case I believe java is the Least clean.

Java “non-static nested class” feature is not “embraced” by Microsoft. All c# nested classes are static and can’t access non-static fields of the enclosing class.

C++ doesn’t support java style inner class either. See http://bigblog.tanbin.com/2009/03/nonstatic-nested-class-has-no-pointer.html

b4 and af select() syscall

Note select() is usually used on the server-side. It allows a /single/ server thread to handle hundreds of concurrent clients.
— B4 —
open the sockets. Each socket is represented by an integer file descriptor. It can be saved in an int array. (A vector would be better, but in C the array also looks like an int pointer).

FD_SET(socketDes1, &readfds); /* add socketDes1 to the readfds */
——–

select() function argument includes readfds — the list of existing sockets[1]. Select will test each socket.

— After —
check the set of incoming sockets and see which socket is “ready”.

FD_ISSET(socketDes1, &readfds)

If ready, Then you can either read() or recvfrom()

http://www.cs.cmu.edu/afs/cs/academic/class/15441-f01/www/lectures/lecture03.ppt has sample code.

[1] Actually Three independent sets of file descriptors are watched, but for now let’s focus on the first — the incoming sockets

&& and || together

[ -f unixfile ] && rm unixfile || print “unixfile was not found, or is not a regular file” — to be tested

Finally, multiple commands can be executed based on the result of command1 by incorporating braces and semi-colons:

command1 &

If the exit status of command1 is true (zero), commands 2, 3, and 4 will be performed.

See other post(s) on exit status

perl one-liner : sum across lines

A common perl-n/perl-p challenge is to sum over loops. Solution is the END{} block.

This example below is from http://www.theperlreview.com/Articles/v0i1/one-liners.pdf

ls -lAF | perl -ne ‘   next if /^d/; $sum += (split)[4]; END{ print $sum }   ‘

(I guess this sums up all the files’… sizes?)

Note you need not pre-declare $sum as a static-local (in C lingo). I guess it’s static-local by default.

colon^semi-colon for python compound statement

Python does use semi-colon but not curly braces in compound statements.

if condition: do_something(); do_something_else() # is same as

if condition:
    do_something()
    do_something_else()

https://stackoverflow.com/questions/8236380/why-is-semicolon-allowed-in-this-python-snippet explains semicolon using official docu

was my unix cmd successful@@

This post focuses on one question:

Q: was previous cmd successful@@
A: (based on [[ teach yourself shell programming ]])

if [ $? -eq 0 ] ….

Background: i wasn’t sure if my own solutions were reliable. Now this author also believed in [ $? -eq 0 ]

cmd1 && … # tests 0. [[ learning the bash shell ]]
cmd1 || .. # tests 0.

“0 means OK” is standard convention. A script or command can break the convention, but you are allowed to assume no one does.

##classic algos using 2 cooperating data structures

* LRU cache — i.e. least-recently-used cache. requires a hash table + some kind of sequence container

Hash table is the only choice to provide constant-time lookup.

To maintain the order, we need some kind of sequence container.

* non-recursively print a directory tree breadth-first (then again depth-first). P171 [[c#precisely]] has a concise 10-line solution.

You need a queue (for breadth-first) or stack (for depth-first) to hold the nodes to print

You need a “seen ” Set to remember which nodes already printed.

* tree-print is the “most-interviewed” example of recursion-to-iteration problem. All such problems require at least 2 data structures, at least one of them a stack

* implement a queue using 2 stacks.

eg – c++ exception-passing ^ param-passing

Update — [[c++primer]] points out the key concept of ExceptionObject (exo for short) in a try/catch. This is the object thrown, assuming we don’t throw a pointer.

exo isn’t the object created (“object1”) and passed to the “throw” statement. That object1 can have a short lifespan and destroyed much sooner than the exo, which needs a long lifespan — described on P1039. Note exo is copy-constructed from object1. See R1 below.

exo isn’t the object caught either, unless you catch by reference. P1037.

exo IS the object re-thrown if you use the empty throw statement.
—–
Based on [[More effC++]] item on the difference between exception-passing and function-parameter-passing. Illustrates a few rules — R1 R2 R3

class B: public std::exception{};
class C: public B{}; //and so on
class D: public C{}; //and so on
class K: public I{}; //and so on

f(){ D* ex = new K();
  throw *ex; // Copy#1 created [R1] and thrown, sliced [1] to D
}
main(int){
  try{ f();}
  catch (exception * ex){} // silently ignored because no “address” is thrown — type mismatch [R2]

  //catch (B ex){} //sliced Copy#2a created, just like function parameter pbclone
  // we will leave the above catch clause commented without further discussion.

  catch(B & exCaught){ //Copy#1 caught without slicing
      //throw; //throws Copy#1 [R3], type D
      throw exCaught; //Copy#2b created [R1] and thrown, sliced[1] to B
}

[1] slicing by copy-ctor.

##standard methods (incl. operators) in all STL container templates — CCC

Every STL container exposes these 3 groups of non-static methods — Copy, Compare and Count — Based on ObjectSpace manual. (Note All operators except “=” are inherited, so in this article, we treat them as methods.)

Count – empty()
Count – size()
Count – max_size()

Compare – op== deep-compares 2 containers by content. Practically used — see [[STL tutorial]]
Compare – op!=
Compare – operator-less-than, greater-than etc

Copy – swap() — swap 2 containers. seem to be a non-standard feature. See http://www.cplusplus.com/reference/algorithm/swap/
Copy – copy-ctor — copy entire container
Copy – op= — assigns entire container

starting thread in a ctor – my take on a common pattern

It’s common to start a thread within a ctor, as seen in many articles and projects.

Danger – the half-cooked object under-construction should not be exposed and accessible in the child thread. Often in practice it’s not disastrous but accessing a half-cooked object is a code smell.

I’d say it’s fine if the code is simple and you know for sure the thread.start() is at end of ctor and the child thread doesn’t do anything fancy. Make sure other developers don’t mess things up.

http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html explains that constructor is single-threaded by default and is designed to run in a single-threaded “context”. Don’t break that assumption.

deferencing a perl reference #my take

This is based on perlreftut’s excellent “Use Rule 1”.

1) To get a reference on a scalar/array/hash, put a “\” in front of the $/@/%
2) To dereference a reference, surround it with
${…..} —————————- eg: ${$a}
@{…..} —————————- eg @{$b}
%{…..} —————————- eg %{$h}

==================The longer version=================
1) To get a reference on a scalar/array/hash or even a filehandle or subroutine, put a “\” in front of the complete variable — complete with those funny characters
\$a
\@b
\%c

Prefixing is cleaner and simpler than dereferencing.

2) Think of a listRef $ref holding a string “items”. @{$ref} becomes @{items}. This is actually a real scenario, known as symbolic reference.
In other words, You can always use an array reference, in curly braces, in place of the name (without titles like “@”) of an array. The titles like “@” were left there by your predecessor before you arrived in your curly braces.

subverting – multiple inheritance

!! With MI, the “this” pointer field is not always identical between the Base object and the Derived object.
** Remember a primitive technique of a home-made “class” is a C struct + a self-pointer field [2]. Similarly, In Python methods, “self” must be the first argument…

[2] I don’t think we should add a 32-bit field to each instance! I guess compiler can help make do without it.

!! In a Derived instance, there’s not always a single Base1 instance.
** Basic example — D extends C1 and C2, which both extend B.
** Even if C1 and C2 both virtually extend B, if C3 extends B without “virtual”, D instance still embeds 2 B instances.

!! Casting within inheritance hierarchy doesn’t always maintain the address held inside a Derived pointer or Base pointer. SI — always. ARM P 221 says “with MI, casting changes the value of a pointer”. See my experiment  https://github.com/tiger40490/repo1/blob/cpp1/cpp/88miscLang/ptrArithmeticMI.cpp

!! Each Derived instance uses more than one vtbl. If D inherits from B1 and B2, then that instance uses two vtbl’s. See ARM P230.

!! AOB (see other posts) assumes a 0 “delta” but in MI, delta is introduced because it’s not 0! ARM P222

— Below rules still holds,  in SI and MI —
dtor sequence is the exact reverse of ctor sequence which is BCDC (see other posts)

c# iterator – MoveNext() failure

This post is focused on a single knowledge pearl. I think nowadays most guys write yield-methods …

1) MoveNext() doesn’t throw exception when it moves OutOfRange. It return FALSE to indicate failure.

2) This is in stark contrast to Current(), which throws exception to indicate OutOfRange ( — too early or too late).

Focus on these key points first, and internalize, before we move on to the more advanced knowledge pearls —
However, MoveNext() does throw exception to indicate concurrent modification.

IV – HFT trading operation support

Q: What’s a busted trade. How can it be busted?

Q: If I send a IOC sell of 12.05 but the best bid is $12, how could this be filled?

I believe IOC / FOK are all limit orders. Market orders are implicitly IOC — if orderbook is (almost) empty on this side, the market order is (partially) cancelled.

Q: what’s a resting order?

Q: if a sell limit order is $27 and a new buy limit order comes in at $28, what would be trade price?
A: $27. new comer is given the best price available. Note the buyer doesn’t know what the best offer would become when his order hits the book, so he uses limit order not a market order.

Q: what’s away order?

Q: whats intermarket sweep order

[12]latency(additive)stat ] DMA

A 2009 book on High Frequency hedge funds says a typical (and good-quality) Direct-market-Access platform causes 10ms to 30ms of latency between the Time (A) the order is sent out from the hedge fund’s server and Time (B) the order reaches the exchange. As of 2012, a major DMA provider offers about 10ms latency, but the top tier players have sub-millis performances.

In the context of order execution, latency is always additive.Originally it takes 90 μs (microsec) to get a market order executed on the exchange. At time 0 the order leaves the broker’s network. At time x μs, it hits the exchange. The x μs is the network latency, which I will naively assume to be negligible. At time 90+x μs, the execution report leaves the exchange. At 90+2x it hits the broker.

Now with another layer (InterCeptor) sandwiched in between it looks like

  1. at time 0 the market order leaves the broker
  2. at time x the market order hits IC
  3. 50 + x the order leaves the IC [1], assuming 50 μs Processing latency
  4. 50 + 2x hits exchange
  5. 140 + 2x leaves exchange
  6. 140 + 3x hits IC on return trip
  7. 190 + 3x leaves IC
  8. 190 + 4x hits broker, so total 100 + 2x μs latency added by the IC.

Now consider another scenario, where the broker has a client-connectivity layer processing client orders, somewhat similar to an interceptor. Now it looks like

  1. at time 0 market order hits broker
  2. at time 50 μs it leaves the broker. (Instead of 50, I think MS broker engine adds 12 – 100 μs)
  3. 50 + x hits exchange
  4. 140 + x leaves exchange

.. so 50 μs latency added to the /single-trip/.

All(@@) OO methods are "static" under the hood

C++ supports “free functions”, static method and non-static method. 
– Static method are very similar to free functions. (Difference? Perhaps inheritance and access modifiers.)
– Non-static methods are implemented as free functions accepting “this” as first argument.

C++ is largely based on classic-C. Basically everything translates to classic-C code, which uses free functions only.

Once we understand the c++ world, we can look at java and c# — Essentially same as c++.

Looking deeper, any method, function or subroutine consists of a sequence of instruction — a “code object” if you like. This object is essentially singleton because there’s no reason to duplicate this object or pass it by value. This object has an address in memory. You get that address in C by taking the address of a function — so-called function pointers.

In a multi-threaded environment, this code object is kind of singleton and stateless (if you know what I mean!). Think of Math.abs(). The code object itself is never thread-unsafe, but invoking it as a non-static method often is. Indeed, Invoking it as a free function can also be thread-unsafe if the function uses a stateful object. Example — localtime().

Non-static method has to be implemented based on this code object.

most fundamental data type in c#/c++/java

Notice the inversion —
* C# — Simple types like “int” are based on struct — by aliaing
* C — struct is based on primitives like “int” — by composition
* C++ — classes are based on the C struct

See http://msdn.microsoft.com/en-us/library/s1ax56ch(v=vs.80).aspx. Ignoring enum types for a moment, all c# value types are struct.

Now we are ready to answer the big Question
Q: beside pointers, what’s the most fundamental data type in this language?
A: C# — most fundamental type is the struct. Simple ints, classes and all other types (except enums) are built on top of struct.
A: Java/C++ — most fundamental types are primitives. In C++ all other types are derived using pointer and composition.
A: Once again, java emerges as the cleanest

Now we know why c# doesn’t call int a PRIMITIVE type because it’s not monolithic nor a truly fundamental type.

python remote-debugging, introspection/reflection,

Python — One of python’s top 3 specialties is introspection. Dynamic scripting languages usually excel in this arena.

I believe python/perl offer richer (more comprehensive) introspection and live-debugging than c++/c#/java. The more experienced a programmer gets, the more she is likely to appreciate this feature. However, interviews generally don’t touch on this. Why?

(I guess these may not be well-documented or well known as other features?)

(I guess some interviewers aren’t hardcore.)

Python supports remote debugging too.

a concrete allocator@@

Every single STL container use allocators including the (implicit!) default allocators. It’s probably worthwhile to see a real allocator. [[STL tutorial]] has some incomplete code. [[effC++]] Items 8,9,10 present a customized op-new, which seems to be the basis of a reusable allocator class.

An allocator keeps its own free list, like malloc. Wholesale vs retail.

I believe an allocator is tightly integrated to customized op-new.

I was told in multi-threaded apps, the default op-new hits the same heap every time and can be a synchronization hotspot. I guess if there’s a single free-list then it needs synchronization protection. By giving a mini-heap to each thread, op-new must be customized to hit the mini-heaps.

jvm stack ^ C stakck in JNI@@

Background — we know the jvm heap and the C heap (or off-heap?) are separate. Among other things, GC can relocate and clean up stuff in jvm stack only.

Q: is the stack segment divided into the jvm stack and the C stack?

%%A: yes and no. Stack segment is naturally divided into individual call stacks. If there are 33 jvm stacks and 11 of them extend into JNI, and there are 5 threads created in C, then naturally a shared stack area and 2 unshared stack areas exist in the stack segment.

Even the heap can be divided per thread, if you realize your objects aren’t shared across threads. I was told subheap-per-thread speeds up heap memory allocation, because the default allocator (malloc and derivatives) must synchronize to access the free list

See https://pangin.pro/posts/stack-overflow-handling

tailor-made beats one-size-fits-all: STL algo

Effective STL Item 44 says “prefer member functions to algorithms with the same name”. There's a simple reason — taylor-made beats one-size-fits-all.

set.find() is taylor-made for class set (actually a class-template), but the free function (actually a function-template) find() is one-siize-fit-all — container-agnostic. All STL algos are container-agnostic because the creators made them work exclusively with iterators.

Bottom line — Such container-agnostic solutions will be less efficient than the tailor-made solutions.

method registry in a c# class – messier than java/c++

Background: perl has a symbol table for package (not lexical) variables.

Background: Python has a __dict__ to hold fields and methods — collectively known as “attributes” since python doesn’t really differentiate — a bold departure from c++/java/c# tradition. Another (bolder) departure — each Python class has a __dict__ and each class instance has __dict__ too.

Now, let’s look into a c# class MyClass implementing/inheriting a bunch of PUBLIC methods of the same name. This example is adapted from P109 [[c#precisely]]. MyClass”registry” of all available methods is complicated by the “new”/hiding feature and other features. MyClass can expose completely distinct implementations of —

* new virtual void M(){..}//declared in MyClass itself, hiding a base class virtual-void-M()
* void Interface_1.M() // same return type as above
* void Interface_2.M() // same return type as above.
//Note above Interface_2 extends Interface_1 but hides void-M() with an identical void-M()
// I think IEnumrable and IEnumerable require implementations to adopt this  technique
//———————-
//———————-
//Note below Interface_3 extends Interface_2 but overloads void-M() with int-M().
//Unlike java/c++ this overload has identical parameter set but different return type
//Note this overload uses no “new” so no hiding
* int Interface_3.M()

// regular overloads, just like c++ and java
* void M(int)
* float M(float)

double-ptr usage #3 – vector of pointers

We know the iterator in a vector is physically a raw ptr. If the elements in a vector are pointers, then the iterator is a double ptr. P238 [[STL tutorial]] has a simple yet complete example showing how to dereference the double-ptr twice.

By the way Same example also shows how to use a vector of Shape pointers to hold sub-class instances (of base class shape), like circles, squares…., then call the virtual methods defined in the class hierarchy.

function entities can be stateful in c++

Most function entities should be stateless — clean and simple, but in the real world there are exceptions —

localtime() and other standard system functions maintain local state in a local static variable (and create problem for threads). If you keep calling such a function, its internal state keeps changing

– STL generator functors — should return different values each time, so usually stateful

– Dinkumware’s hash tables have hashing traits/policies as state in the hashing functors. Does the state change from call to call? Not sure.

– P165 [[eff STL]] shows a clearly stateful functor. In practice we need such functors in special circumstances.

c++ Function hiding – free func OR member func

c++ has (non-trivial) hiding rules for member functions AND free functions …. See P37 / P236 [[effC++]].

I feel c++ method hiding is best studied along with overriding/overloading. See http://bigblog.tanbin.com/2011/12/hiding-redefining-overriding.html

For other forms of hiding, see my blog posts http://bigblog.tanbin.com/search/label/c%2B%2Bc%23java_hiding

Within an inheritance hierarchy, Java supports static method hiding; C# uses “new” to make hiding explicit.

physical vs cash settlement – option and other drv

CS vs PS is a simple concept but not trivial. As a beginner I often feel “as an option holder i will exercise and get physical settlement” but that's often impractical/unwise. In many markets, the default is cash settlement. Lower transaction cost.

In certain contexts, the holder/buyer prefers physical settlement, for specific reasons. To truly understand options and hedging, we will invariably find yourself in these contexts. Nuances that some beginners may not appreciate.

implement a thread-safe queue using 2 stacks

As far as possible, enqueue() and dequeue() should operate concurrently.

We can improve performance a bit with a proactively transfer — as soon as stack2 becomes empty. However, this must be done on another thread. My idea is basically

void enqueue(Element e){
    this->stack1.push(e);
}
Element dequeue(){
    if (this->stack2.size()){
      return this->stack2.pop();
    }
    this->transferFromStack1to2();
    if ( ! this->stack2.size()){
        //throw exception or return a special value indicating QUEUE_EMPTY;
    }
    return this->stack2.pop();
}
void transferFromStack1to2(){
    assert this->stack2.size() == 0;
    //get locks on Stack1 and Stack2
    while(this->stack1.size()){
       this->stack2.push(this->stack1.pop()); 
    }
    //release locks
}

c++ CONST methods treat *this as bitwise-constant

Item 29 of [[effC++]] shows that a const method in class C1 can return a non-const pointer field of class C1. If the pointee lives at a offsite address, then whoever using the pointer can /edit/ the pointee.

The “const” on the method tells compiler to treat “this” as a pointer-to-const. More precisely, this particular method can’t modify the bitwise state of the host object. Still more precisely, it means all the bits in the real estate of the host object must remain intact during this function call.

Therefore, all fields are implicitly converted to const variables when used in this function. See also P215 [[eff STL]]

If the host object has sizeof() of 55 bytes, then that’s the size of its real estate. All the bits in the real estate must remain intact. However, the pointee lives outside the real estate and lives unprotected by const.

In java/c#, every reference field is a pointer. In contrast, java primitives and c# structs are the primary examples of “embedded” fields in a class.

CDS pricing #hearsay

See also http://www.financial-risk-manager.com/risks/credit/edf.html

Use bid/ask from market to derive the insurer’s (not insurance buyer’s) cash flow including premium received and the “disaster” compensation amount[1]. This gives an implied hazard rate (or default density (?).

[1] related to recovery value. For physical settlement, the insurer pays the buyer the face value, i.e. the sum assured.

I was told this (implied) hazard rate is the key soft mkt data just like implied vol and implied yield. Calibration of hazard rates to CDS market quotes (spreads or upfront points with running coupons)

Plot the hazard rate along maturity. You get a credit spread curve (just like yield curve and the term structure of vol). I guess this is the term structure of hazard rate.

Using this curve you can price all other credit instruments.

Just like vol, there’s a realized default rate (default intensity), whose value is very different from the implied hazard rates backed out from the market quotes.

const T =shadow-type of T;const T&=distinct type from T&

(background: the nitty gritty of overload/overriding rules are too complicated…)

I feel overloading and overriding has consistent rules. Take 2 same-name functions (single-param functions for simplicity) and ignore their host classes. If the 2 can overload each other, then their parameters are considered distinct. That means they can’t override each other (if they were inserted into a inheritance tree).

Conversely, if the 2 can override each other, then their parameter types are considered “identical” so they can’t overload each other (if set “free”).

Q: We know function overloading forbids 2 overloads with identical parameter sets. How about const, i.e. If 2 functions’ parameters differ only in const, can they have identical names?
A: No. ARM P308 explains that compiler’s (static) “resolution” (based on argument type) will fail to pick a winner among the 2. It’s ambiguous.
A: therefore, in the overloading context, const SomeType is a “shadow type” and does NOT count as a distinct type.

However, if 2 functions’ parameters have this difference — const T& vs T&, then distinct types, so the 2 functions can have identical names. Exaplained in ARM.

Similarly, 2 overloads can overload based on const T* vs T* — distinct types.

Q: We know method overriding requires exact parameter match. How about const? Can an override add or remove const?
A: whatever the answer, this is sloppy. No justification.
A: yes according to my test. Adding the const makes no difference — runtime binding unobstructed.
A: therefore, in the overriding context, const SomeType is a “shadow type” and does NOT count as a distinct type.

However, my test shows const T& vs T& do make a difference — runtime binding disabled. These are considered 2 distinct types.

practice blind-walk on a tight rope, and grow as true geek

A fellow developer told me if a c++ class is in deployed to production, then it’s better not to rename a method in it.

In another case — an initial system release to production — I was the designer of the spring config loading mechanism. I was so confident about how my code works that I simply dropped an override config file into a special folder and I knew my code would take care of it. A colleague suggested we test it in QA environment first. I said we need to develop some level of self-confidence by deliberately taking small leaps of faith in real production environment.

I developed similar self-confidence in GS when I directly ran update/delete in production database (when no choice). My release was approved on the last day of the monthly cycle. I had just 2 hours to finish the release. So I put the entire month-end autosys batch (tens of millions of dollars of payout processing) on-hold to complete my release. However, the first run produced bad data in the production DB, so I had to clean it up, tweak the stored proc and table constraints a bit before rerun. I stayed till 11pm. If I were to test everything in QA first, it would take 3 times longer and I would lose concentration, and would not be able to react to the inevitable surprises during such a complex release.

As a developer, if you always play safe and test, test, test, and dare not trust your own memory and deductive thinking, then you will not improve your memory and deduction. You will not quickly master all the one-liners, all the IDE refactors, …

Top Lasik surgeons can perform the operation swiftly (which is good for the patient). A young tight-rope walker will one day need to practice blind-walk. Master wood-carving artists develop enough self-confidence to not need to pencil down the cuts before cutting…

populating STL containers requires copy-ctor

See also http://bigblog.tanbin.com/2012/07/insert-into-vector-copy-ctor-constantly.html for detailed experiments.

I believe STL containers generally use the payload’s copy-ctor during insertion.

vector? YES according to p21 [[effSTL]] vector example
deque at extremities? yes [1]
deque mid-stream???
list? yes
set? yes
map? yes

Let’s examine vectors. A java Vector (or arrayList) holds nothing but pointers. A 5-element vector occupies about 32bits x 5. All pointee objects live on the heap.

A stl vector, in basic configuration, holds a bunch of non-ref objects. A 5-element vector of Trade where sizeof(Trade)=20 bytes occupies exactly 100 bytes. (No additional overhead due to nodes and links. Remember a linked container consists of nodes each containing a payload object.)

P22 [[effSTL]] suggests that an empty vector can have the 100 bytes reserved but no Trade instance constructed. Item 9 in [[effSTL]] too suggest that default allocators for all STL containers grab memory from heap and return it as uncooked (i.e. uninitialized) chunk of raw memory. [[c++nutshell] says an implementation of vector must allocate an uninitialized array of objects and initialize Individual elements when needed

When you push_back a Trade that lives in Address 8123, our 5-element vector needs to somehow copy that Trade into Position #6. Assume there’s enough capacity reserved so no reallocation. Our vector can’t use assignment operator, because Position #6 is not a Trade object, but just 20 raw bytes allocated by the default vector allocator. Can it use the copy constructor? I think so, yet it doesn’t grab memory from stack or heap, but from the vector’s allocator. Item 10 of [[effC++]] has a complete allocator class. If your vector uses such an allocator, then every memory allocation “within” the vector would be intercepted by this allocator.

Down-shift after removing an element calls the assignment operator. I feel there’s no choice. Shifting Item 3 down to Position 2 — you can’t use copy-ctor or any ctor, since they use uncooked raw memory. In this case Slot 2 is already allocated and “cooked” (initialize).

I believe up-shift uses the assignment operator or copy-constructor.

[1] allocating memory from the “reserved packing lots”. See other posts on deque memory model.

overload^override – c++ (java too)

I consider overloading and overriding 2 magic tricks by the compiler. Here are a few contrasts as far as I can see.
Example of overloading —
function1(B* arg);
function1(D* arg);

– Overloading is compile-time magic; overriding is run-time magic. If you have a D object’s address in a variable myVar and you pass it function1, which function1 is chosen by compiler? Depends on the declared type of myVar. Discussed repeatedly in my blog.

– consider renaming to function1_for_Base()/function1_for_Derived() etc. It may seem cool to have a bunch of overload functions, but in one Barcap email sender utility class, there are 4 send(…) utilities each with more than 7 parameters — code smell. It’s hard for users to tell them apart. P403 [[c++TimesavingTechniques]] points out that overloading complicates debugging and maintenance.
** Readability is fine in overRiding

– Overriding is generally a best practice to be adopted whenever possible. I can’t say the same about overloading.
– c++ has hiding rule about overLoading. No such thing about overRiding.
– c++ does implicit (!) type conversion about overLoading.
– In conclusion, overriding is cleaner than overloading, less complicated, and more readable.
– In overriding There’s more magic by compiler — vptr etc. More powerful but Not more complicated
– overLoad can be applied to operators such as op=, op+. I doubt overRide can

pure virtual with/out implementation: popular@IV

Item 44 in [[EffC++]] offers a summary that basically says

1) a pure virtual means only interface is inherited (since parent provides no implementation)
2) a “simple/traditional virtual” means interface plus a default implementation is inherited
3) a non-virtual means interface plus a mandatory implementation is inherited and subclasses are advised to keep the implementation. C++ actually allows subclass to /deviate/ by redefining and hiding the parent implementation. Java has a cleaner solution in the “final” keyword.

I’d like to add

1b) a pure-virtual-with-an-implementation tells the subclass author

“Inherit this interface. By the way I also offer an implementation upon request, but uninheritable.”

This differs from (2). Both are illustrated in Item 36.

Author of a subclass of an abstract base class (featuring pure2()) can choose one of three options:

  • 1. don’t declare pure2() at all.  As the default and most popular usage, the subclass is also abstract (pun intended) by virtual of the inherited pure2().
  • 2. define pure2(), and becoming a non-abstract class
  • … so far, exactly same as java syntax
  • 3. redeclare the same pure2() without implementation — an error. See P215 [[ARM]]

mkt-risk system is nice-to-have@@ (2nd post)

Is market-risk system nice-to-have, as described in http://bigblog.tanbin.com/2010/09/risk-is-nice-to-have-to-traders.html ?

Q1: which app budget to cut when a firm must cut IT budget by half? Bang for the buck! I feel risk app stacks up well against a lot of systems like reporting, Massive and expensive market data engines, HR, mobile, algo trading, high-frequency, DMA, fancy pricers, real-time curve-builders.

Anything real time, anything high volume (to the cutting edge) tends to be expensive — developer, hardware/software/hosting. Do they give better benefit/cost ratio?

Now a slightly less important question —
Q1b: which app budget to increase when times are good?

Q: who makes the budget decisions? How powerful is the risk app’s sponsor in a budget war?

Risk apps do look like white elephants — expensive, sophisticated but unreliable analysis. See http://bigblog.tanbin.com/2011/03/limitations-of-risk-models-haitao.html Bottom line — you can criticize 99% of trading platform components as non-essential, questionable numbers, poor data quality, expensive and poor value for money. 30 years ago, successful traders flourished without custom applications. Some still do.

Recently, There is a lot of regulatory pressure on firms to be more “serious” about risk management but I always felt big banks (and big buy-sides) tend to pay lip service. True? An eq der risk system veteran gave me a few pointers. (I will add the least amount of annotation…)

* Risk, compared to other systems, has larger data volume.
* Risk uses more computation and more computer power. If you have a server farm, what are the top 2 use cases?
* Compared to cash products, Derivatives requires more risk analysis

I feel risk engine uses more math, more complicated business logic, more database, more business insight, more quant know-how than other components.

Another (management-level) veteran told me most investment banks lack the real time risk capabilities of GS and JPM.

STL algo – stringent on iterators, !! containers

STL Algorithms are agnostic about containers. STL algorithms also support arrays, string, and input/output streams.

STL Algorithms are very particular about …. iterators! Some demand specific iterator features. As P30[[ObjectSpace manual]] puts it, some algorithms require more powerful iterators than others. For example, sort() expects random-access iterators.

Some algorithms' declarations mention the expected type of iterators.

Many expect input iterators, so an output-only iterator won't do.

Some need output iterators , so an input-only iterator won't do.

I believe some expect RW iterators.

I believe some expect const iterators, i.e. read-only

I believe some expect bidirectional iterators.

1+2 big users of pricing/valuation engine

See also post on secDB usages (http://bigblog.tanbin.com/2011/05/secdb-designed-for-prop-trading.html)

—–A) pre-trade. For any large/small trader, order origination is all about timing, sizing and …YES, pricing.

A1) quote pricing – on both sell-side and buy-side
** limit order pricing (also stop/stop-limit orders)
** RFQ pricing
** market-maker’s pricing engine. Note MM can be a sell-side or buy-side

A1b) pricing other orders — not all out-going orders are quotes, but I feel quote pricing is the dominant system. If you send out a non-limit order, you can often leverage the same quote pricer. Examples —
A1b1 market orders — trader must decide at what price level to generate a mkt order
A1b2 cancellations — someone must decide at what price level to cancel an order.

A2) hedging — a composite pricing task, though it’s included in the pre-trade section. You already have positions so you must mark them and analyze their sensitivities, then price your hedging orders

A3) pre-trade valuation analysis. Very important in structured/exotic trading desk.

A4) pricing control — compliance, both internal and external (regulatory)
—–B) post-trade
B1) marking — mark to market, either real time or periodic. Basis of VaR and pre-trade pricing
** EOD/EOM/EOQ PnL
** General Ledger — I worked with KPMG auditors. They have a big responsibility to scrutinize these valuations
** regulatory reporting

B2) market-risk — most complex usage
** sensitivity — affects hedging pricing
** correlation and hedging
** VaR

B3) margin calc
B4) collateral re-valuation. Most long-term contracts are backed by collateral and need to be re-valued.
B5) market-value based fee calculation — the oxygen of portfolio managers
B6) bonus
———–
If I must name Big 3 users, I’d single out 1) quote pricer (including market-making), 2) marking and 3) VaR. 

In one major derivative desk (market leader), the same pricing model is used in 2 primary pricers — RFQ and risk. These are 2 distinct software applications, though based on the same pricing model.

select() syscall and its drv

select() is the most “special” socket kernel syscall (not a “standard library function”). It’s treated special.

– Java Non-blocking IO is related to select().
– Python has at least 3 modules — select, asyncore, asynchat all built around select()
– dotnet offers 2 solutions to the same problem:

  1. a select()-based and
  2. a newer asynchronous solution to the same problem