internals

Why is throwing exceptions so slow?

这一生的挚爱 提交于 2019-12-06 11:58:33
They are telling us not to use exceptions to control flow of our programs because throwing exceptions is slow. I have never heard any explanation why is throwing exceptions so slow. So the question is: What is the mechanism of throwing exceptions and what are particular operations involved which may have performance impact? EDIT: Some clarification: I would like to hear what extra work is need by operation system to handle throwing exceptions. Is there some switching between user and kernel mode which are that costly? Or maybe constructing exception object is costly? Or maybe there is

How does pycharm work? How did they hook into the interpreter?

陌路散爱 提交于 2019-12-06 07:24:44
I know how to use PyCharm's debugger but that has only deepened my curiosity of how it accomplishes the task of being so tightly coupled to the Python interpreter. Does cPython have some sort of intrepreter hooks buried in itself or does PyCharm somehow copy the source code, instrument the code, and then exec it? Thanks to @unholySheep I was able to go from the github src on PyDev.Debugger back to sys.settrace which lead to a post on Python Module of the week on settrace . Once the tracing script has the stack frame, it is likely a non-trivial task of inspecting the frame's stack content and

How to change the behavior of a python dictionary's __setattr__?

此生再无相见时 提交于 2019-12-06 05:21:02
问题 In Python, everything has a class. Therefore dict also has a class. So, in theory, I should be able to change the implementation of the keyvalue assignment behavior. Example: d = dict() d['first'] = 3 # Internally d['first'] is stored as 6 [i.e. value*2 if value is INT] print d['first'] # should print 6 d['second'] = 4 print d['second'] # should print 8 I noticed that most objects have attributes listed in OBJECT.__dict__ or vars(OBJECT) . But this isn’t the case for dict or list . How can I

How are categories implemented in Objective C?

纵饮孤独 提交于 2019-12-05 23:23:28
问题 I know how to use categories as a programmer, but I'm curious how they are implemented. Does the compiler compile them into calls to class_replaceMethod from a static initializer? Thanks. 回答1: New answer on topic. Each class has a list of methods, when doing a method lookup the method list is scanned from beginning to end. If no method is found the superclass' list is scanned, etc. until reaching the root class. Found methods are cached for faster lookup next time. When loading a category

How SqlDataAdapter works internally?

我怕爱的太早我们不能终老 提交于 2019-12-05 16:25:43
I wonder how SqlDataAdapter works internally, especially when using UpdateCommand for updating a huge DataTable (since it's usually a lot faster that just sending sql statements from a loop). Here is some idea I have in mind : It creates a prepared sql statement (using SqlCommand.Prepare() ) with CommandText filled and sql parameters initialized with correct sql types. Then, it loops on datarows that need to be updated, and for each record, it updates parameters values, and call SqlCommand.ExecuteNonQuery() . It creates a bunch of SqlCommand objects with everything filled inside ( CommandText

In PHP, why does “or die()” work, but “or return” doesn't?

非 Y 不嫁゛ 提交于 2019-12-05 09:40:03
问题 In PHP, you can handle errors by calling or die to exit when you encounter certain errors, like this: $handle = fopen($location, "r") or die("Couldn't get handle"); Using die() isn't a great way to handle errors. I'd rather return an error code so the parent function can decide what to do, instead of just ending the script ungracefully and displaying the error to the user. However, PHP shows an error when I try to replace or die with or return , like this: $handle = fopen($location, "r") or

What's the internal format of a .NET String?

巧了我就是萌 提交于 2019-12-05 04:51:19
I'm making some pretty string-manipulation-intensive code in C#.NET and got curious about some Joel Spolsky articles I remembered reading a while back: http://www.joelonsoftware.com/articles/fog0000000319.html http://www.joelonsoftware.com/articles/Unicode.html So, how does .NET do it? Two bytes per char? There ARE some Unicode chars^H^H^H^H^H code points that need more than that. And how is the length encoded? Johnno Nolan Before Jon Skeet turns up here is a link to his excellent blog on strings in C#. In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the

Difference between taskkill and taskkill /f

别说谁变了你拦得住时间么 提交于 2019-12-05 04:29:01
On Microsoft Technet I can read that taskkill has a /f parameter to kill a process forcefully. I wonder what this does internally, to understand the impact of such an action. taskkill (without /f ) does not simply send a WM_CLOSE message to the process, otherwise my application would ask whether or not to save the open documents. This makes me assume that it already operates on a TerminateProcess (MSDN) level. However, TerminateProcess does not have a parameter for forcing a kill. So, what do taskkill and taskkill /f do internally? I read the related question Difference between C# Process.Kill

C++11 internal std::string representation (libstdc++)

一世执手 提交于 2019-12-05 04:26:25
How std::string is internally represented in c++11 (libstdc++)? While digging inside the implementation, I found: /* A string looks like this: * * [_Rep] * _M_length * [basic_string<char_type>] _M_capacity * _M_dataplus _M_refcount * _M_p ----------------> unnamed array of char_type * * Where the _M_p points to the first character in the string, and * you cast it to a pointer-to-_Rep and subtract 1 to get a * pointer to the header. * * This approach has the enormous advantage that a string object * requires only one allocation. All the ugliness is confined * within a single %pair of inline

canEqual() in the scala.Equals trait

余生颓废 提交于 2019-12-04 23:53:09
From the source code scala/Equals.scala ( here ): package scala trait Equals extends scala.Any { def canEqual(that: scala.Any): scala.Boolean def equals(that: scala.Any): scala.Boolean } In the documentation, it says: A method that should be called from every well-designed equals method that is open to be overridden in a subclass. I randomly picked a class which extends scala.Equals and which is simple enough to understand. I picked scala.Tuple2[+T1, +T2] , which extends the trait scala.Product[T1, T2] , which in turn extends the trait scala.Product , which in turn extends the trait scala