ACE封装——针对效率实施优化

避免使用虚函数和动态内存。

 我在前一篇里提到,ace有的时候避免使用bridge模式,这是因为动态内存的分配会降低效率,同时,为了消除动态类型对效率的影响,ace的wrapper facade避免使用虚方法。

多考虑使用内联函数。

ace wrapper多是对c函数的封装,为了避免封装降低效率,ace在关键路径上使用内联函数(另外,在性能关键位置避免使用虚函数)。

没有异常处理。

其实ace之初异常还没有成为C++标准,ace没有异常也不是什么弊端,而且,某种程度上说,C++的异常又是额外开销的代名词,从效率上来说,避免在系统工具包中使用异常处理也是一个正确的取舍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装 vue3-ace-editor ```shell npm install vue3-ace-editor ``` 2. 在需要使用的组件中引入 ace-editor 组件 ```html <template> <AceEditor v-model="code" :options="options" mode="myCustomLanguage" :highlightActiveLine="true" :tabSize="2" theme="twilight" style="height: 500px;" /> </template> <script> import AceEditor from "vue3-ace-editor"; export default { components: { AceEditor, }, data() { return { code: "", options: { enableBasicAutocompletion: true, enableSnippets: true, enableLiveAutocompletion: true, }, }; }, mounted() { this.$nextTick(() => { this.code = "function myFunction() {\n console.log('Hello World!');\n}"; }); }, }; </script> ``` 3. 添加自定义语言 ```javascript import ace from "ace-builds"; import "ace-builds/src-noconflict/mode-javascript"; // 引入 JavaScript 语言模块 ace.define( "ace/mode/myCustomLanguage_highlight_rules", function (require, exports) { var oop = require("ace/lib/oop"); var TextHighlightRules = require("ace/mode/text_highlight_rules") .TextHighlightRules; var MyCustomLanguageHighlightRules = function () { // 注释 this.$rules["start"] = [ { token: "comment", regex: "//.*$", }, ]; // 关键字 this.$rules["start"].push({ token: "keyword", regex: "function", }); // 函数名 this.$rules["start"].push({ token: "entity.name.function", regex: "myFunction", }); // 字符串 this.$rules["start"].push({ token: "string", regex: "'|\"", next: "string", }); // 其他 this.$rules["start"].push({ defaultToken: "text", }); this.$rules.string = [ { token: "constant.language.escape", regex: /\\(?:['"\\]|[0-7]{3})/, }, { token: "string", regex: /['"]/, next: "start", }, { defaultToken: "string", }, ]; }; oop.inherits(MyCustomLanguageHighlightRules, TextHighlightRules); exports.MyCustomLanguageHighlightRules = MyCustomLanguageHighlightRules; } ); ace.define("ace/mode/myCustomLanguage", function (require, exports) { var oop = require("ace/lib/oop"); var TextMode = require("ace/mode/text").Mode; var MyCustomLanguageHighlightRules = require("./myCustomLanguage_highlight_rules") .MyCustomLanguageHighlightRules; var MyCustomLanguageMode = function () { this.HighlightRules = MyCustomLanguageHighlightRules; }; oop.inherits(MyCustomLanguageMode, TextMode); (function () { this.$id = "ace/mode/myCustomLanguage"; }).call(MyCustomLanguageMode.prototype); exports.Mode = MyCustomLanguageMode; }); ``` 4. 在组件中引入自定义语言模块 ```javascript import "ace-builds/src-noconflict/mode-myCustomLanguage"; // 引入自定义语言模块 ``` 5. 完整代码 ```html <template> <AceEditor v-model="code" :options="options" mode="myCustomLanguage" :highlightActiveLine="true" :tabSize="2" theme="twilight" style="height: 500px;" /> </template> <script> import AceEditor from "vue3-ace-editor"; import "ace-builds/src-noconflict/mode-myCustomLanguage"; // 引入自定义语言模块 export default { components: { AceEditor, }, data() { return { code: "", options: { enableBasicAutocompletion: true, enableSnippets: true, enableLiveAutocompletion: true, }, }; }, mounted() { this.$nextTick(() => { this.code = "function myFunction() {\n console.log('Hello World!');\n}"; }); }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值