Go heap vs stack — escape analysis with slices

go学习之堆栈中的变量

Stack versus heap allocation in Go stayed fuzzy until I read the articles in the references at the end. Here is a condensed version.

What the Go FAQ says

How do I know whether a variable is allocated on the heap or the stack?

From a correctness standpoint, you do not need to know. Each variable in Go exists as long as there are references to it. The storage location chosen by the implementation is irrelevant to the semantics of the language.

The storage location does affect efficient code. When possible, the Go compilers allocate variables that are local to a function in that function’s stack frame. If the compiler cannot prove that the variable is not referenced after the function returns, it must allocate on the garbage-collected heap to avoid dangling pointer errors. If a local variable is very large, the heap may be a better choice than the stack.

In current compilers, taking the address of a variable makes heap allocation likely. Escape analysis can still keep some of those variables on the stack when they clearly do not outlive the function.

Read More

Go concurrency patterns — Rob Pike talks and advanced techniques

Advanced Go Concurrency Patterns

Go’s concurrency patterns are worth collecting in one place. Mostly notes from official talks: definitions, idioms, and a few advanced techniques.

Sources:
Advanced Go Concurrency Patterns

Go Concurrency Patterns

Concurrency Is Not Parallelism

Read More

Go memory model — happens-before, channels, and sync primitives

Go Memory Model

Notes on how the Go memory model orders reads and writes.

Memory model

Happens before

The model defines when reads and writes are ordered. Without a defined order, you cannot rely on result ordering.

For a write w to V to be observed by a read r:

  1. r does not happen before w, and
  2. no other write w' happens after w and before r.

For w to be the only value r can see (no concurrent conflicting writes):

  1. w happens before r, and
  2. every other write to V happens after r or before w.

Read More

MySQL — comparing dotted version strings

mysql中比较版本字符串

Use case: pick the newest row whose version is still less than or equal to the current app version.

String compare bites you:

1
select "5.1.3" > "5.1.24";

MySQL returns 1 (true) because it compares lexicographically — at the patch segment, 3 is greater than 2.

Stack Overflow has a solid thread on this:
compare version numbers in MySQL

Read More

Share   |  Updated:

跨域登录态共享

登录态共享

登录态共享?什么鬼,不知道的就可以不用看啦,需要时再说,说不定到时也用不到,笑脸~~

场景

解决多个子系统中共用模块(比方说工单系统)可以共享子系统的登录态
为简化问题,设定场景:
登录系统A,点工单操作跳转系统B,并且无需登录(至于系统登录何时失效暂不考虑)

还有:别问我为什么不用单点登录(SSO),那个,不想说,笑脸~~

先别废话,直接代码上

Read More

Share   |  Updated:

laradock开发实践

此文是我用marp展示ppt拿过来的,可能看着不太明白,抱歉,我太懒~

laradock

  • DIY(do it yourself)

  • DRY(dont repeat youself)

  • 人生苦短,我用docker


Read More

Share   |  Updated:

PHP pitfalls — mb_strlen and session file locks

php踩坑记录

Functions

mb_strlen “broken” on the server

Locally mb_strlen('哈') was 1; on the server it was 3 — almost always charset config.

mb_strlen defaults to mb_internal_encoding(). The old mbstring.internal_encoding ini directive was deprecated in PHP 5.6; use default_charset instead.

On the server default_charset was unset, so PHP fell back to ISO-8859-1.

Read More

Share   |  Updated: