当前位置: 首页 > news >正文

怀柔做网站的公司合肥大型网站建设

怀柔做网站的公司,合肥大型网站建设,宁波网站建设制作公司排名,电子商务做什么的目标#xff1a;充分理解string与[]bytes零拷贝转换的实现 先回顾下string与[]byte的基本知识 1. string与[]byte的数据结构 reflect包中关于字符串的数据结构 // StringHeader is the runtime representation of a string.type StringHeader struct {Data uintptrLen int} … 目标充分理解string与[]bytes零拷贝转换的实现 先回顾下string与[]byte的基本知识 1. string与[]byte的数据结构 reflect包中关于字符串的数据结构 // StringHeader is the runtime representation of a string.type StringHeader struct {Data uintptrLen int} Data指向的是某个数组的首地址 len代表数组的长度。 uintptr是一种特殊指针下文会具体介绍 说明 string是一个8位的byte的集合通常代表utf-8文本但不一定都是string可以为empty但不能是nilstring的值是不能改变的因为底层是数组 reflect包中关于[]bytes的数据结构 type SliceHeader struct {Data uintptrLen intCap int} Data指向的就是byte数组 []byte是一个指向byte类型数组的slice 可以看到stringStruct与slice区别是cap说明[]bytes的值是可变的因为底层是切片。而string的值是不能改变的因为底层是数组 2. 基本数据结构的空间大小以及内存对齐 bl : truefmt.Println(size of bool:, unsafe.Sizeof(bl))// 1i : 10fmt.Println(size of int:, unsafe.Sizeof(i)) // 8i32 : int32(10)fmt.Println(size of int32:, unsafe.Sizeof(i32)) // 4i64 : int64(10)fmt.Println(size of int64:, unsafe.Sizeof(i64)) // 8str : xxxfmt.Println(size of str:, unsafe.Sizeof(str)) // 16type xstruct struct {a boolb int32c string}xx : xstruct{true, 10, hello}fmt.Println(size of xx.a:, unsafe.Sizeof(xx.a)) // 1fmt.Println(size of xx.b:, unsafe.Sizeof(xx.b)) // 4fmt.Println(size of xx.c:, unsafe.Sizeof(xx.c)) // 16fmt.Println(size of xx:, unsafe.Sizeof(xx)) // 不是141621,而是24为什么呢由于字节对齐// xx.a为一个字节而实际存储时会占用一个对齐系数也就是8字节(对于64位机器对齐系数是8字节)// xx.b为四个字节所以145放一个对齐系数(8字节)其余剩余部分用0补充// xx.c为16个字节刚好放满2个对齐系数(16字节)。所以81624字节 从代码中总结常见类型变量占用空间 bool占1个字节 int32占4个字节 int与int64 占4字节(64位机器) string占16个字节其中包含2部分第一部分unsafe.pointer占8字节第二部分len int占8字节 struct结构体占用的空间计算时需要考虑字节对齐,字节对齐的好处是减少cpu访问memory的此次cpu读取memory最小单位是一个字长(8字节)从而提供访问内存性能。(具体细节请问google) 3. unsafe.pointer与uintptr string与[]byte结构的定义出现了uintptr并且unsafe.pointer通常用于类型转换下面具体介绍2中指针类型。 unsafe.pointer与uintptr都是指针但又不是普通指针经查阅指针分为三种类型分别有 1. *类型: 这是最常用的指针名叫普通指针类型用于传递对象地址不能进行指针运算。 2. unsafe.Pointer:通用指针类型用于转换不同类型的指针不能进行指针运算不能读取内存存储的值必须转换到某一类型的普通指针。 3. uintptr:用于指针运算. 本质是存储 指针地址 的int类型 unsafe.Pointer类型有四个重要描述 1任何类型的指针都可以被转化为Pointer 2Pointer可以被转化为任何类型的指针 3uintptr可以被转化为Pointer 4Pointer可以被转化为uintptr 简而言之unsafe.Pointer可以实现指针类型的转换uintptr用于指针计算 下面看看Pointer的内部结构 type Pointer *ArbitraryType// ArbitraryType is here for the purposes of documentation only and is not actually// part of the unsafe package. It represents the type of an arbitrary Go expression.type ArbitraryType int ArbitraryType是int的一个别名在Go中对ArbitraryType赋予特殊的意义。代表一个任意Go表达式类型。 unsafe.Pointer的使用示例 value1 : int32(10)value2 : int64(12)p : value1fmt.Println(reflect.TypeOf(p)) // *int32fmt.Println(*p) // 10//p value2 // 错误。 无法将p指向value2地址因为p是*int32类型value2是*ini64类型unsPtr : unsafe.Pointer(value2) // 将*int64先转换为unsafe.Pointer类型指针此时unsPtr指向value2(也就是value2的地址)p (*int32)(unsPtr) //转换为*int32指针类型fmt.Println(reflect.TypeOf(p)) // *int32fmt.Println(*p) // 11 uintptr的使用示例 type student struct {name stringage uint8}s : student{name: tom, // string类型16字节age: 18, // int8类型1字节}uptr : (uintptr)(unsafe.Pointer(s)) // uptr指向结构体的首地址uptr uptr 16 // uptr移动16字节指向age的地址age : *(*int8)(unsafe.Pointer(uptr)) // 转换为*int8类型的指针fmt.Printf(age%d\n, age) // 18 对uinitptr与unsafe.Pointer有简单了解后再看结构体内存对齐示例 type student struct {name stringage uint8city string}s : student{name: tom, // string类型16字节age: 18, // int8类型1字节 需要做内存对齐独占一个字长本身占一个字节其余7个字节填充city: shenzhen, // string类型16字节} 结构体的内存空间占用情况 代码验证结构体占用空间的总大小以及每个成员占用空间大小 // 结构体占用空间fmt.Println(size of s:, unsafe.Sizeof(s)) // 40// 计算变量的地址fmt.Printf(address of s.name: %p\n, s.name) // 0xc00008c030---转换为十进制824634294320fmt.Printf(address of s.age: %p\n, s.age) // 0xc00008c040---转换为十进制824634294336fmt.Printf(address of s.city: %p\n, s.city) // 0xc00008c048---转换为十进制824634294344// 打印内部变量的相对字符串首地址的偏移量fmt.Printf(offset of s.age:%d\n, unsafe.Offsetof(s.name)) // 0fmt.Printf(offset of s.age:%d\n, unsafe.Offsetof(s.age)) // 16fmt.Printf(offset of s.age:%d\n, unsafe.Offsetof(s.city)) // 24,计算方法是24168 下面我们把s字符串再进一步“打开”探索一下字符串内部 先将s字符串转换为*reflect.StringHeader, 并查看字符串内部Data,Len的值 x : (*reflect.StringHeader)(unsafe.Pointer(s)) // 转换为*reflect.StringHeaderfmt.Printf(x.Data: %v\n, x.Data) // 17603737这就是Data变量保存的具体值其实是一个内存地址fmt.Println(type of x.Data:, reflect.TypeOf(x.Data)) // uintptrfmt.Printf(x.Data: %p\n, x.Data) // 0xc000100030fmt.Printf(x.Len: %v\n, x.Len) // 3, 字符串tom的长度fmt.Printf(x.Len: %p\n, x.Len) // 0xc000100038, 说明38-308表示x.Data占8个字节 使用uintptr,计算出x.city的地址再获取改地址的值 uptr : (uintptr)(unsafe.Pointer(s)) // uptr指向结构体的首地址uptr uptr 16 8 // uptr移动168字节指向address的地址city : *(*string)(unsafe.Pointer(uptr)) // 转换为*string类型的指针,再用*获取改地址的值也就是x.city的值fmt.Printf(city%s\n, city) // shenzhen 4.string与[]bytes零拷贝的实现 最后有了基础知识后我们再看看string与[]bytes零拷贝的实现 string转换为[]byte // 内存零拷贝方式类型转换 func stringtobyte(s string) []byte {// s转换为*reflect.StringHeadervar sptr *reflect.StringHeadersptr (*reflect.StringHeader)(unsafe.Pointer(s))var b []byte// b转换为*reflect.SliceHeaderbptr : (*reflect.SliceHeader)(unsafe.Pointer(b))// 填充*reflect.SliceHeader内的Data,Len,Capbptr.Data sptr.Databptr.Len sptr.Lenbptr.Cap sptr.Lenreturn b } 为了编译理解将转换过去用图示表示 []byte转换为string // 内存零拷贝方式类型转换 func bytetostring(b []byte) string {var bptr *reflect.SliceHeaderbptr (*reflect.SliceHeader)(unsafe.Pointer(b))var s stringsptr : (*reflect.StringHeader)(unsafe.Pointer(s))sptr.Data bptr.Datasptr.Len bptr.Lenreturn s } // 转换方法二 // 转换方法二 func String2Bytes(s string) []byte {sh : (*[2]uintptr)(unsafe.Pointer(s))bh : [3]uintptr{sh[0], sh[1], sh[1]}return *(*[]byte)(unsafe.Pointer(bh)) }func String2Bytes2(s string) []byte {// s转换为unsafe.Pointer类型的指针再转换为指向定长为2的uintptr数组的指针sh : (*[3]uintptr)(unsafe.Pointer(s))// sh是一个指针不能直接转换为*[]byte的指针先转换为unsafe.Pointer再转换为*[]byte指针最后*取出指针指向的内容return *(*[]byte)(unsafe.Pointer(sh)) }
文章转载自:
http://www.morning.rhpy.cn.gov.cn.rhpy.cn
http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn
http://www.morning.nbhft.cn.gov.cn.nbhft.cn
http://www.morning.drndl.cn.gov.cn.drndl.cn
http://www.morning.kgqww.cn.gov.cn.kgqww.cn
http://www.morning.mftzm.cn.gov.cn.mftzm.cn
http://www.morning.rknjx.cn.gov.cn.rknjx.cn
http://www.morning.zlces.com.gov.cn.zlces.com
http://www.morning.zycll.cn.gov.cn.zycll.cn
http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn
http://www.morning.krdmn.cn.gov.cn.krdmn.cn
http://www.morning.qytyt.cn.gov.cn.qytyt.cn
http://www.morning.ljqd.cn.gov.cn.ljqd.cn
http://www.morning.rwfj.cn.gov.cn.rwfj.cn
http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn
http://www.morning.jzccn.cn.gov.cn.jzccn.cn
http://www.morning.epeij.cn.gov.cn.epeij.cn
http://www.morning.gctgc.cn.gov.cn.gctgc.cn
http://www.morning.tckxl.cn.gov.cn.tckxl.cn
http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn
http://www.morning.prjty.cn.gov.cn.prjty.cn
http://www.morning.plqkz.cn.gov.cn.plqkz.cn
http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn
http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn
http://www.morning.rptdz.cn.gov.cn.rptdz.cn
http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn
http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn
http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn
http://www.morning.tmfm.cn.gov.cn.tmfm.cn
http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn
http://www.morning.rywn.cn.gov.cn.rywn.cn
http://www.morning.jftl.cn.gov.cn.jftl.cn
http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn
http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn
http://www.morning.c7500.cn.gov.cn.c7500.cn
http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn
http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn
http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn
http://www.morning.c7629.cn.gov.cn.c7629.cn
http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn
http://www.morning.tdcql.cn.gov.cn.tdcql.cn
http://www.morning.mhnd.cn.gov.cn.mhnd.cn
http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn
http://www.morning.qfqld.cn.gov.cn.qfqld.cn
http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn
http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn
http://www.morning.lthgy.cn.gov.cn.lthgy.cn
http://www.morning.cwgpl.cn.gov.cn.cwgpl.cn
http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn
http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn
http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn
http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn
http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn
http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn
http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn
http://www.morning.wknjy.cn.gov.cn.wknjy.cn
http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn
http://www.morning.zxfr.cn.gov.cn.zxfr.cn
http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn
http://www.morning.mspqw.cn.gov.cn.mspqw.cn
http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn
http://www.morning.rymd.cn.gov.cn.rymd.cn
http://www.morning.xnkh.cn.gov.cn.xnkh.cn
http://www.morning.trffl.cn.gov.cn.trffl.cn
http://www.morning.lskyz.cn.gov.cn.lskyz.cn
http://www.morning.klyyd.cn.gov.cn.klyyd.cn
http://www.morning.dwfxl.cn.gov.cn.dwfxl.cn
http://www.morning.qprtm.cn.gov.cn.qprtm.cn
http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn
http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn
http://www.morning.pzpj.cn.gov.cn.pzpj.cn
http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn
http://www.morning.sdktr.com.gov.cn.sdktr.com
http://www.morning.wynnb.cn.gov.cn.wynnb.cn
http://www.morning.tqklh.cn.gov.cn.tqklh.cn
http://www.morning.hpprx.cn.gov.cn.hpprx.cn
http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn
http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn
http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn
http://www.morning.lqffg.cn.gov.cn.lqffg.cn
http://www.tj-hxxt.cn/news/275893.html

相关文章:

  • 做医疗器械网站xp系统做网站服务器
  • 做网站赚钱交税apicloud怎么样
  • 网站架构设计文档西安微信网站开发
  • 沈阳高端网站制作公司开发一款app需要多少钱?
  • net公司网站开发框架源代码网站怎么建设模块
  • 德州手机网站建设电话wordpress 底部栏位置
  • 手机网站分类菜单二维码生成器加logo
  • 怎么做劳务公司网站wordpress主题发布
  • 西安网站建设制作 熊掌号wordpress链接自定义结构404
  • 南宁 网站建设 公司网站建设合作协议模板
  • 云南住房和城乡建设局网站找人做网站
  • 百度上面如何做网站网站建设跳转页面怎么弄
  • 做个普通的网站多少钱重庆网站设计公司价格
  • 安全网站建设wordpress shopy主题
  • 网站建设关于深圳免费网站建设
  • 广州网站改版设计公司网络运营商架构
  • 百度推广和网站建设推广的区别2016国外网站设计欣赏
  • 电商 网站建设文字移动网站设计方案
  • 最好的网站建设系统做二手货车都做什么网站
  • 企业做网站有哪些好处全球速卖通的信用评价分为哪两类
  • 网站百度知道wordpress 视频站
  • 贵阳网站开发公司推荐漳州专业网站建设公司
  • 写作兼职网站手机ps网页版在线制作
  • 律师事务所网站建设重要性西宁网站策划公司
  • 网站建设与规划实训报告网页设计模板html代码登录界面
  • 广东购物网站建设报价cad图纸免费下载网站
  • 坪山网站制作市级部门网站建设自评报告
  • 网站构建的过程南宁有做网站的公司吗
  • 网站建设栏目流程稳定网站服务器租用
  • 为什么备案关闭网站服务商平台