Curiosity and knowledge

There is no end to learning, especially in the computer industry. Curiosity and knowledge expand our expertise in both breadth and depth. Two examples:

Copy an array

Our implementation copies the elements of an array to another array. You may choose to write your own loop to do this, but there is a built-in function in Java called System.arrayCopy(), and there are many experiments on the web that prove that the latter is faster than the former. Why is it fast? Take a look at the source code:

    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
Copy the code

If you call a local method internally, is calling a local method necessarily faster than writing your own loop? Let’s look at this local method

JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
                               jobject dst, jint dst_pos, jint length))
  JVMWrapper("JVM_ArrayCopy");
  // Check if we have null pointers
  if (src == NULL || dst == NULL) {
    THROW(vmSymbols::java_lang_NullPointerException());
  }
  arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
  arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
  assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
  assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
  // Do copy
  Klass::cast(s->klass())->copy_array(s, src_pos, d, dst_pos, length, thread);
JVM_END
Copy the code

The actual copy action is on the last line, the corresponding code

void typeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { assert(s->is_typeArray(), "must be type array"); // Check destination if (! d->is_typeArray() || element_type() ! = typeArrayKlass::cast(d->klass())->element_type()) { THROW(vmSymbols::java_lang_ArrayStoreException()); } // Check is all offsets and lengths are non negative if (src_pos < 0 || dst_pos < 0 || length < 0) { THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); } // Check if the ranges are valid if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) { THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); } // Check zero copy if (length == 0) return; // This is an attempt to make the copy_array fast. int l2es = log2_element_size(); int ihs = array_header_in_bytes() / wordSize; char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es); char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es); Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es); // copy}Copy the code

This method is preceded by all kinds of judgments, and the actual copy action is also in the last line, and then it goes down layer by layer until it finds the following method

void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) { if (from > to) { jint *end = from + count; while (from < end) *(to++) = *(from++); } else if (from < to) { jint *end = from; from += count - 1; to += count - 1; while (from >= end) *(to--) = *(from--); }}Copy the code

Here I see that its input argument contains the first address of the two arrays, and then copy it term by term. Details of the intermediate process of c + + code can refer to this link: www.cnblogs.com/yakovchang/…

I might be a little confused by this, but isn’t it still copying item by item? Is indeed, but it saves each loop problem to position the two arrays when addressing (array), we write a cycle of every time to reposition the first address of the two arrays, and then copy item by item, and this method has two arrays passed over the first address, every time without addressing, direct memory copy.

System performance tuning

This is my personal experience of jingdong interview questions, how to carry out system tuning. At that time, the answer to this question was not good, and then I consulted the interviewer, and he gave an idea, from the bottom to the front each level has different methods and parameters

  1. Operating system level tuning
  2. Container layer tuning
  3. Database tuning
  4. Application layer JVM tuning
  5. Server component tuning, for exampleTomcatorNgix
  6. The front-end optimization

Each level can be asked in detail, because I am also in the learning process, so I can not give a detailed answer for the moment, but the idea is like this. If which god understand, but also hope in the message area not stingy comments.

The above examples are technology-related, but they can also be extended to other aspects. For example, we can go to the user site to observe and understand how the user uses them. It is likely that they are not quite the same as we imagined. In companies like mine, there are all kinds of technology, business and other forms of knowledge sharing, and we can choose to listen to what interests us, which greatly expands our knowledge.

conclusion

No matter which industry is developing towards specialization and diversification, we must keep a curious and knowledge-seeking attitude so as to continuously expand the breadth and depth of our knowledge, so as to adapt to the development of the industry.